Skip to content

Commit

Permalink
Added option timestamps to sequelize storage to activate createdAt co…
Browse files Browse the repository at this point in the history
…lumn
  • Loading branch information
Americas committed Nov 3, 2016
1 parent 4c6ecf9 commit c66c3be
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/storages/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ module.exports = redefine.Class(/** @lends SequelizeStorage.prototype*/ {
* @param {String} [options.storageOptions.columnType=Sequelize.STRING] - type
* of the column. For utf8mb4 charsets under InnoDB, you may need to set
* this <= 190.
* @param {Boolean} [options.storageOptions.timestamps=false] - option to add
* timestamps to model table
*
* @constructs SequelizeStorage
*/
constructor: function (options) {
Expand All @@ -48,7 +51,8 @@ module.exports = redefine.Class(/** @lends SequelizeStorage.prototype*/ {
// note 'sequelize' or 'model' is required
modelName: 'SequelizeMeta',
// note 'tableName' (optional) also supported
columnName: 'name'
columnName: 'name',
timestamps: false
}, this.options.storageOptions || {});

if (!this.options.storageOptions.model && !this.options.storageOptions.sequelize) {
Expand Down Expand Up @@ -81,7 +85,7 @@ module.exports = redefine.Class(/** @lends SequelizeStorage.prototype*/ {
{
tableName: this.options.storageOptions.tableName,
schema: this.options.storageOptions.schema,
timestamps: false,
timestamps: this.options.storageOptions.timestamps,
charset: 'utf8',
collate: 'utf8_unicode_ci'
}
Expand Down
78 changes: 78 additions & 0 deletions test/storages/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,22 @@ describe('sequelize', function () {
});
});

it('accepts a "timestamps" option', function () {
var storage = new Storage({
storageOptions: {
sequelize: this.sequelize,
timestamps: true
}
});
return storage.options.storageOptions.model.sync()
.then(function (model) {
return model.describe();
})
.then(function(description) {
expect(description).to.only.have.keys(['name','createdAt','updatedAt']);
});
});

it('accepts a "columnType" option', function () {
var storage = new Storage({
storageOptions: {
Expand Down Expand Up @@ -204,6 +220,25 @@ describe('sequelize', function () {
expect(migrations[0].customColumnName).to.be('asd.js');
});
});

it('writes the migration to the database with timestamps', function () {
var storage = new Storage({
storageOptions: {
sequelize: this.sequelize,
timestamps: true
}
});

return storage.logMigration('asd.js')
.then(function() {
return storage.options.storageOptions.model.findAll();
})
.then(function(migrations) {
expect(migrations.length).to.be(1);
expect(migrations[0].name).to.be('asd.js');
expect(new Date() - migrations[0].createdAt).to.be.lessThan(100);
});
});
}); //end describe('logMigration', function() {

describe('unlogMigration', function () {
Expand Down Expand Up @@ -289,6 +324,32 @@ describe('sequelize', function () {
});
});

it('deletes the migration from the database with timestamps', function () {
var storage = new Storage({
storageOptions: {
sequelize: this.sequelize,
timestamps: true
}
});

return storage.logMigration('asd.js')
.then(function() {
return storage.options.storageOptions.model.findAll();
})
.then(function(migrations) {
expect(migrations.length).to.be(1);
})
.then(function() {
return storage.unlogMigration('asd.js');
})
.then(function() {
return storage.options.storageOptions.model.findAll();
})
.then(function(migrations) {
expect(migrations).to.be.empty();
});
});

});

describe('executed', function () {
Expand Down Expand Up @@ -359,5 +420,22 @@ describe('sequelize', function () {
expect(migrations).to.be.eql(['asd.js']);
});
});

it('returns executed migrations with timestamps', function () {
var storage = new Storage({
storageOptions: {
sequelize: this.sequelize,
timestamps: true
}
});

return storage.logMigration('asd.js')
.then(function() {
return storage.executed();
})
.then(function(migrations) {
expect(migrations).to.be.eql(['asd.js']);
});
});
}); //end describe('executed', function() {
}); //end describe('sequelize', function() {

0 comments on commit c66c3be

Please sign in to comment.