diff --git a/changelog.md b/changelog.md index 71641b72adc5..6ff63f47da89 100644 --- a/changelog.md +++ b/changelog.md @@ -2,7 +2,8 @@ - [INTERNALS] Update `inflection` dependency to v1.5.3 - [FEATURE] Replaced string error messages for connection errors with error objects. [#2576](https://github.com/sequelize/sequelize/pull/2576) - [FEATURE] Support for updating fields on duplicate key in bulk update (mysql only) [#2692](https://github.com/sequelize/sequelize/pull/2692) -- [FEATURE] Basic support for Microsoft SQL Server +- [FEATURE] Basic support for Microsoft SQL Server +- [INTERNALS] Deprecate migration logic. This is now implemented in [umzug](https://github.com/sequelize/umzug) and the [CLI](https://github.com/sequelize/cli). #### Backwards compatability changes - Some of the string error messages for connection errors have been replaced with actual error instances. Checking for connection errors should now be more consistent. diff --git a/lib/sequelize.js b/lib/sequelize.js index e1c16d469112..e1e1116916d3 100755 --- a/lib/sequelize.js +++ b/lib/sequelize.js @@ -400,6 +400,15 @@ module.exports = (function() { * @return {Migrator} An instance of Migrator. */ Sequelize.prototype.getMigrator = function(options, force) { + deprecated([ + 'Using the migration engine of the sequelize core is deprecated and will be removed in an', + 'upcoming version of sequelize. Please note that migration engine has been replaced with', + 'umzug (https://github.com/sequelize/umzug) and that the previously available migrator logic', + 'was basically just a wrapper around the query interface of sequelize.', + 'The sequelize CLI was rewritten accordingly and is backwards compatible with your existing', + 'migrations.' + ].join(' ')); + var Migrator = require('./migrator'); if (force) { diff --git a/test/assets/migrations/20111117063700-createPerson.coffee b/test/assets/migrations/20111117063700-createPerson.coffee deleted file mode 100644 index 6db03c0509ea..000000000000 --- a/test/assets/migrations/20111117063700-createPerson.coffee +++ /dev/null @@ -1,14 +0,0 @@ -module.exports = - up: (migration, DataTypes, done) -> - migration - .createTable 'Person', - name: DataTypes.STRING - isBetaMember: - type: DataTypes.BOOLEAN - defaultValue: false - allowNull: false - .complete done - down: (migration, DataTypes, done) -> - migration - .dropTable 'Person' - .complete done diff --git a/test/assets/migrations/20111117063700-createPerson.js b/test/assets/migrations/20111117063700-createPerson.js deleted file mode 100644 index 27e3d775c998..000000000000 --- a/test/assets/migrations/20111117063700-createPerson.js +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration - .createTable('Person', { - name: DataTypes.STRING, - isBetaMember: { - type: DataTypes.BOOLEAN, - defaultValue: false, - allowNull: false - } - }) - .complete(done) - }, - down: function(migration, DataTypes, done) { - migration.dropTable('Person').complete(done) - } -} diff --git a/test/assets/migrations/20111130161100-emptyMigration.js b/test/assets/migrations/20111130161100-emptyMigration.js deleted file mode 100644 index 06a589c73233..000000000000 --- a/test/assets/migrations/20111130161100-emptyMigration.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { done() }, - down: function(migration, DataTypes, done) { done() } -} diff --git a/test/assets/migrations/20111205064000-renamePersonToUser.js b/test/assets/migrations/20111205064000-renamePersonToUser.js deleted file mode 100644 index 931eea18546a..000000000000 --- a/test/assets/migrations/20111205064000-renamePersonToUser.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.renameTable('Person', 'User').complete(done) - }, - - down: function(migration, DataTypes, done) { - migration.renameTable('User', 'Person').complete(done) - } -} diff --git a/test/assets/migrations/20111205162700-addSignatureColumnToUser.js b/test/assets/migrations/20111205162700-addSignatureColumnToUser.js deleted file mode 100644 index 064e75bb3304..000000000000 --- a/test/assets/migrations/20111205162700-addSignatureColumnToUser.js +++ /dev/null @@ -1,39 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration - .addColumn('User', 'isAdmin', { type: DataTypes.BOOLEAN, defaultValue: false, allowNull: false }) - .complete(function(err) { - if (err) { - done(err) - } else { - migration - .addColumn('User', 'signature', DataTypes.TEXT) - .complete(function(err) { - if (err) { - done(err) - } else { - migration.addColumn('User', 'shopId', { type: DataTypes.INTEGER, allowNull: true }).complete(done) - } - }) - } - }) - - - }, - - down: function(migration, DataTypes, done) { - migration.removeColumn('User', 'signature').complete(function(err) { - if (err) { - done(err) - } else { - migration.removeColumn('User', 'shopId').complete(function(err) { - if (err) { - done(err) - } else { - migration.removeColumn('User', 'isAdmin').complete(done) - } - }) - } - }) - } -} diff --git a/test/assets/migrations/20111205163000-addColumnLevelAndChangeToEnumToUser.js b/test/assets/migrations/20111205163000-addColumnLevelAndChangeToEnumToUser.js deleted file mode 100644 index 177d141ce86e..000000000000 --- a/test/assets/migrations/20111205163000-addColumnLevelAndChangeToEnumToUser.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.addColumn('User', 'level', { type: DataTypes.STRING }).complete(function() { - migration.changeColumn('User', 'level', { type: DataTypes.ENUM, allowNull: false, values: ['basic', 'advanced'] }).complete(done); - }); - }, - - down: function(migration, DataTypes, done) { - migration.removeColumn('User', 'level').complete(done); - } -}; diff --git a/test/assets/migrations/20111205167000-addUniqueNameColumnToUser.js b/test/assets/migrations/20111205167000-addUniqueNameColumnToUser.js deleted file mode 100644 index 64bdaf07992d..000000000000 --- a/test/assets/migrations/20111205167000-addUniqueNameColumnToUser.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.addColumn('User', 'uniqueName', { type: DataTypes.STRING }).complete(function() { - migration.changeColumn('User', 'uniqueName', { type: DataTypes.STRING, allowNull: false, unique: true }).complete(done) - }) - }, - - down: function(migration, DataTypes, done) { - migration.removeColumn('User', 'uniqueName').complete(done) - } -} diff --git a/test/assets/migrations/20111206061400-removeShopIdColumnFromUser.js b/test/assets/migrations/20111206061400-removeShopIdColumnFromUser.js deleted file mode 100644 index c97f2c9ab328..000000000000 --- a/test/assets/migrations/20111206061400-removeShopIdColumnFromUser.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.removeColumn('User', 'shopId').complete(done) - }, - - down: function(migration, DataTypes, done) { - migration.addColumn('User', 'shopId', { type: DataTypes.INTEGER, allowNull: true }).complete(done) - } -} diff --git a/test/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js b/test/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js deleted file mode 100644 index 3c0cf78b62d8..000000000000 --- a/test/assets/migrations/20111206063000-changeSignatureColumnOfUserToMendatory.js +++ /dev/null @@ -1,11 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.changeColumn('User', 'signature', { - type: DataTypes.STRING, - allowNull: false, - defaultValue: 'Signature' - }).complete(done) - }, - - down: function(migration, DataTypes, done) { done() } -} diff --git a/test/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js b/test/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js deleted file mode 100644 index 67fe1b80634f..000000000000 --- a/test/assets/migrations/20111206163300-renameSignatureColumnOfUserToSig.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.renameColumn('User', 'signature', 'sig').complete(done) - }, - - down: function(migration, DataTypes, done) { - migration.renameColumn('User', 'sig', 'signature').complete(done) - } -} diff --git a/test/assets/migrations/20130909174103-createFunctionGetAnAnswer.js b/test/assets/migrations/20130909174103-createFunctionGetAnAnswer.js deleted file mode 100644 index fa81605f4448..000000000000 --- a/test/assets/migrations/20130909174103-createFunctionGetAnAnswer.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.createFunction('get_an_answer', [], 'int', 'plpgsql', - 'RETURN 42;' - ).complete(done); - }, - down: function(migration, DataTypes, done) { - migration.dropFunction('get_an_answer', []).complete(done); - } -} diff --git a/test/assets/migrations/20130909174253-renameFunctionGetAnAnswerGetTheAnswer.js b/test/assets/migrations/20130909174253-renameFunctionGetAnAnswerGetTheAnswer.js deleted file mode 100644 index 10b6954ca5ef..000000000000 --- a/test/assets/migrations/20130909174253-renameFunctionGetAnAnswerGetTheAnswer.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.renameFunction('get_an_answer', [], 'get_the_answer').complete(done); - }, - down: function(migration, DataTypes, done) { - migration.renameFunction('get_the_answer', [], 'get_an_answer').complete(done); - } -} diff --git a/test/assets/migrations/20130909175000-deleteFunctionGetTheAnswer.js b/test/assets/migrations/20130909175000-deleteFunctionGetTheAnswer.js deleted file mode 100644 index 4f89ff9691d1..000000000000 --- a/test/assets/migrations/20130909175000-deleteFunctionGetTheAnswer.js +++ /dev/null @@ -1,10 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.dropFunction('get_the_answer', []).complete(done); - }, - down: function(migration, DataTypes, done) { - migration.createFunction('get_the_answer', 'int', 'plpgsql', - 'RETURN 42;' - ).complete(done); - } -} diff --git a/test/assets/migrations/20130909175939-createTestTableForTrigger.js b/test/assets/migrations/20130909175939-createTestTableForTrigger.js deleted file mode 100644 index b832f7305cf9..000000000000 --- a/test/assets/migrations/20130909175939-createTestTableForTrigger.js +++ /dev/null @@ -1,17 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration - .createTable('trigger_test', { - name: DataTypes.STRING, - updated_at: { - type: DataTypes.DATE, - defaultValue: DataTypes.NOW, - allowNull: false - } - }) - .complete(done) - }, - down: function(migration, DataTypes, done) { - migration.dropTable('trigger_test').complete(done) - } -} diff --git a/test/assets/migrations/20130909180846-createTriggerOnTriggerTestTable.js b/test/assets/migrations/20130909180846-createTriggerOnTriggerTestTable.js deleted file mode 100644 index aadc7a8a853e..000000000000 --- a/test/assets/migrations/20130909180846-createTriggerOnTriggerTestTable.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.createTrigger('trigger_test', 'updated_at', 'before', {update: true}, - 'bump_updated_at', []).complete(done); - }, - down: function(migration, DataTypes, done) { - migration.dropTrigger('trigger_test', 'updated_at').complete(done); - } -} diff --git a/test/assets/migrations/20130909181148-renameTriggerUpdatedAtToUpdateUpdatedAt.js b/test/assets/migrations/20130909181148-renameTriggerUpdatedAtToUpdateUpdatedAt.js deleted file mode 100644 index 891238af70f4..000000000000 --- a/test/assets/migrations/20130909181148-renameTriggerUpdatedAtToUpdateUpdatedAt.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.renameTrigger('trigger_test', 'updated_at', 'update_updated_at').complete(done); - }, - down: function(migration, DataTypes, done) { - migration.renameTrigger('trigger_test', 'update_updated_at', 'updated_at').complete(done); - } -} diff --git a/test/assets/migrations/20130909185621-deleteTriggerUpdateUpdatedAt.js b/test/assets/migrations/20130909185621-deleteTriggerUpdateUpdatedAt.js deleted file mode 100644 index 7fd0d75e79d5..000000000000 --- a/test/assets/migrations/20130909185621-deleteTriggerUpdateUpdatedAt.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = { - up: function(migration, DataTypes, done) { - migration.dropTrigger('trigger_test', 'update_updated_at').complete(done); - }, - down: function(migration, DataTypes, done) { - migration.createTrigger('trigger_test', 'update_updated_at', 'before', {update: true}, - 'bump_updated_at', []).complete(done); - } -} diff --git a/test/migrator.test.js b/test/migrator.test.js deleted file mode 100644 index e93e44798552..000000000000 --- a/test/migrator.test.js +++ /dev/null @@ -1,597 +0,0 @@ -var chai = require('chai') - , expect = chai.expect - , Support = require(__dirname + '/support') - , Migrator = require("../lib/migrator") - , DataTypes = require("../lib/data-types") - , dialect = Support.getTestDialect() - , current = Support.sequelize; - -chai.config.includeStack = true - -if (current.dialect.supports.migrations) { - -describe(Support.getTestDialectTeaser("Migrator"), function() { - beforeEach(function() { - this.init = function(options, callback) { - options = Support.Sequelize.Utils._.extend({ - path: __dirname + '/assets/migrations', - logging: function(){} - }, options || {}) - - //this.sequelize.options.logging = console.log - var migrator = new Migrator(this.sequelize, options) - - migrator - .findOrCreateSequelizeMetaDAO({ force: true }) - .success(function(SequelizeMeta) { - callback && callback(migrator, SequelizeMeta) - }) - .error(function(err) { console.log(err) }) - }.bind(this) - }) - - describe('getCompletedMigrations', function(){ - it("supports coffee files", function(done) { - this.init({ filesFilter: /\.coffee$/, to: 20111117063700 }, function(migrator, SequelizeMeta) { - SequelizeMeta.create({ from: null, to: 20111117063700 }).success(function() { - migrator.getCompletedMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(1) - done() - }) - }) - }) - }) - - it("only returns migrations if timestamps in db are after timestamps of files", function(done) { - this.init({ to: 20111117063700 }, function(migrator, SequelizeMeta) { - SequelizeMeta.create({ from: null, to: 20111117063700 }).success(function() { - migrator.getCompletedMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(1) - done() - }) - }) - }) - }) - - it("returns no migrations if timestamps in db are before timestamps of files", function(done) { - this.init({ to: 20111117063700 }, function(migrator, SequelizeMeta) { - SequelizeMeta.create({ from: null, to: 20101117063700 }).success(function() { - migrator.getCompletedMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(0) - done() - }) - }) - }) - }) - }) - - describe('getUndoneMigrations', function() { - it("supports coffee files", function(done) { - this.init({ - filesFilter: /\.coffee$/, - to: 20111130161100 - }, function(migrator) { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(1) - done() - }) - }) - }) - - it("returns no files if timestamps are after the files timestamp", function(done) { - this.init({ from: 20140101010101 }, function(migrator) { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations.length).to.equal(0) - done() - }) - }) - }) - - it("returns only files between from and to", function(done) { - this.init({ from: 19700101000000, to: 20111117063700 }, function(migrator) { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations.length).to.equal(1) - expect(migrations[migrations.length - 1].filename).to.equal('20111117063700-createPerson.js') - done() - }) - }) - }) - - it("returns exactly the migration which is defined in from and to", function(done) { - this.init({ from: 20111117063700, to: 20111117063700 }, function(migrator) { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations.length).to.equal(1) - expect(migrations[migrations.length - 1].filename).to.equal('20111117063700-createPerson.js') - done() - }) - }) - }) - - it("returns also the file which is exactly options.from or options.to", function(done) { - this.init({ from: 20111117063700, to: 20111130161100 }, function(migrator) { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(2) - expect(migrations[0].filename).to.equal('20111117063700-createPerson.js') - expect(migrations[1].filename).to.equal('20111130161100-emptyMigration.js') - done() - }) - }) - }) - - it("returns all files to options.to if no options.from is defined", function(done) { - this.init({ to: 20111130161100 }, function(migrator) { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(2) - done() - }) - }) - }) - - it("returns all files from last migration id stored in database", function(done) { - this.init(undefined, function(migrator, SequelizeMeta) { - SequelizeMeta.create({ from: null, to: 20111117063700 }).success(function() { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(15) - expect(migrations[0].filename).to.equal('20111130161100-emptyMigration.js') - done() - }) - }) - }) - }) - - it("returns pending migrations", function(done) { - this.init(undefined, function(migrator, SequelizeMeta) { - SequelizeMeta.create({ from: 20111117063700, to: 20111117063700 }).success(function() { - SequelizeMeta.create({ from: 20111117063700, to: 20130909185621 }).success(function() { - migrator.getUndoneMigrations(function(err, migrations) { - expect(err).to.be.null - expect(migrations).to.have.length(14) - expect(migrations[0].filename).to.equal('20111130161100-emptyMigration.js') - done() - }) - }) - }) - }) - }) - }) - - describe('migrations', function() { - beforeEach(function(done) { - var self = this - - this.init({ from: 20111117063700, to: 20111117063700 }, function(migrator) { - self.migrator = migrator - self.migrator.migrate().success(done) - }) - }) - - describe('executions', function() { - it("executes migration #20111117063700 and correctly creates the table", function(done) { - this.sequelize.getQueryInterface().showAllTables().success(function(tableNames) { - tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' }) - expect(tableNames).to.eql([ 'Person' ]) - done() - }) - }) - - it("executes migration #20111117063700 and correctly adds isBetaMember", function(done) { - this.sequelize.getQueryInterface().describeTable('Person').success(function(data) { - var fields = Object.keys(data).sort() - expect(fields).to.eql([ 'isBetaMember', 'name' ]) - done() - }) - }) - - it("executes migration #20111117063700 correctly up (createTable) and downwards (dropTable)", function(done) { - var self = this - - this.sequelize.getQueryInterface().showAllTables().success(function(tableNames) { - tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' }) - expect(tableNames).to.eql([ 'Person' ]) - - self.migrator.migrate({ method: 'down' }).success(function() { - self.sequelize.getQueryInterface().showAllTables().success(function(tableNames) { - tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' }) - expect(tableNames).to.eql([]) - done() - }) - }) - }) - }) - - it("supports coffee files", function(done) { - var self = this - - this.init({ - filesFilter: /\.coffee$/, - to: 20111130161100 - }, function(migrator) { - self.migrator = migrator - self.migrator.migrate().success(function() { - self.sequelize.getQueryInterface().showAllTables().success(function(tableNames) { - tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' }) - expect(tableNames).to.eql([ 'Person' ]) - done() - }) - }) - }) - }) - it("executes the empty migration #20111130161100", function(done) { - this.init({ from: 20111130161100, to: 20111130161100 }, function(migrator) { - // this migration isn't actually testing anything but - // should not timeout - - // expect(1).to.equal(1) - - migrator - .migrate() - .success(done) - .error(function(err) { console.log(err) }) - }) - }) - - it("executes pending migrations", function(done) { - var self = this; - var options = { - path: __dirname + '/assets/migrations', - to: 20111117063700 - }; - - var migrator = new Migrator(this.sequelize, options) - - migrator - .migrate() - .success(function() { - - self.init({ to: 20111205064000 }, function(migrator, SequelizeMeta) { - SequelizeMeta.create({ from: 20111205064000, to: 20111205064000 }).success(function() { - migrator - .migrate() - .success(function() { - SequelizeMeta.findAll().success(function(meta) { - expect(meta.length).to.equal(3); - done(); - }); - }) - .error(function(err) { console.log(err) }) - }) - }) - - }) - .error(function(err) { console.log(err) }) - - }) - }) - - describe('renameTable', function() { - beforeEach(function(done) { - var self = this - - this.init({ from: 20111117063700, to: 20111117063700 }, function(migrator) { - self.migrator = migrator - self.migrator.migrate().success(done) - }) - }) - - it("executes migration #20111205064000 and renames a table", function(done) { - var self = this - - this.sequelize.getQueryInterface().showAllTables().success(function(tableNames) { - tableNames = tableNames.filter(function(e) { return e != 'SequelizeMeta' }) - expect(tableNames).to.include('Person') - - self.init({ from: 20111205064000, to: 20111205064000 }, function(migrator) { - migrator.migrate().success(function() { - self.sequelize.getQueryInterface().showAllTables().success(function(tableNames) { - tableNames = tableNames.filter(function(e){ return e != 'SequelizeMeta' }) - expect(tableNames).to.eql([ 'User' ]) - done() - }) - }) - }) - }) - }) - }) - - describe('addColumn', function() { - it('adds a unique column to the user table', function(done) { - var self = this - - this.init({ from: 20111117063700, to: 20111205167000 }, function(migrator) { - migrator.migrate().complete(function(err) { - expect(err).not.to.be.ok; - self.sequelize.getQueryInterface().describeTable('User').complete(function(err, data) { - expect(err).not.to.be.ok; - var signature = data.signature - , isAdmin = data.isAdmin - , shopId = data.shopId - - expect(signature.allowNull).to.be.true - expect(isAdmin.allowNull).to.be.false - if (dialect === "postgres" || dialect === "postgres-native" || dialect === "sqlite") { - expect(isAdmin.defaultValue).to.be.false - } else { - expect(isAdmin.defaultValue).to.equal("0") - } - expect(shopId.allowNull).to.be.true - - done() - }) - }) - }) - }) - - it('adds a column to the user table', function(done) { - var self = this - - this.init({ from: 20111117063700, to: 20111205162700 }, function(migrator) { - migrator.migrate().complete(function(err) { - self.sequelize.getQueryInterface().describeTable('User').complete(function(err, data) { - var signature = data.signature - , isAdmin = data.isAdmin - , shopId = data.shopId - - expect(signature.allowNull).to.be.true - expect(isAdmin.allowNull).to.be.false - if (dialect === "postgres" || dialect === "postgres-native" || dialect === "sqlite") { - expect(isAdmin.defaultValue).to.be.false - } else { - expect(isAdmin.defaultValue).to.equal("0") - } - expect(shopId.allowNull).to.be.true - - done() - }) - }) - }) - }) - }) - - describe('removeColumn', function() { - it('removes the shopId column from user', function(done) { - var self = this - - this.init({ to: 20111206061400 }, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.getQueryInterface().describeTable('User').success(function(data) { - var signature = data.signature - , isAdmin = data.isAdmin - , shopId = data.shopId - - expect(signature.allowNull).to.be.true - expect(isAdmin.allowNull).to.be.false - if (dialect === "postgres" || dialect === "postgres-native" || dialect === "sqlite") { - expect(isAdmin.defaultValue).to.be.false - } else { - expect(isAdmin.defaultValue).to.equal("0") - } - expect(shopId).to.be.not.ok - - done() - }) - }) - }) - }) - }) - - describe('changeColumn', function() { - it('changes the signature column from user to default "signature" + notNull', function(done) { - var self = this - - this.init({ to: 20111206063000 }, function(migrator) { - migrator.migrate().success(function() { - self.sequelize.getQueryInterface().describeTable('User').success(function(data) { - var signature = data.signature - - if (dialect === 'postgres') { - expect(signature.type).to.equal('CHARACTER VARYING') - } else { - expect(signature.type).to.equal('VARCHAR(255)') - } - expect(signature.allowNull).to.equal(false) - expect(signature.defaultValue).to.equal('Signature') - - done() - }) - }) - }) - }) - - it('changes the level column from user and casts the data to the target enum type', function(done) { - var self = this - - this.init({ to: 20111205163000 }, function(migrator) { - migrator.migrate().success(function() { - self.sequelize.getQueryInterface().describeTable('User').complete(function(err, data) { - var level = data.level; - - if (dialect === 'postgres') { - expect(level.type).to.equal('USER-DEFINED'); - } else if (dialect === 'sqlite') { - expect(level.type).to.equal('TEXT'); - } else { - expect(level.type).to.equal('ENUM(\'BASIC\',\'ADVANCED\')'); - } - - done() - }) - }) - }) - }) - }) - - }) - - describe('renameColumn', function() { - it("renames the signature column from user to sig", function(done) { - var self = this - - this.init({ to: 20111206163300 }, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.getQueryInterface().describeTable('User').success(function(data) { - var signature = data.signature - , sig = data.sig - - expect(signature).to.not.be.ok - expect(sig).to.be.ok - - done() - }) - }) - }) - }) - }) - - if (dialect.match(/^postgres/)) { - - describe('function migrations', function() { - var generateFunctionCountQuery = function generateFunctionCountQuery(functionName, langName) { - return [ - 'SELECT * FROM pg_proc p LEFT OUTER JOIN pg_language l ON (l.oid = p.prolang)', - 'WHERE p.proname = \'' + functionName + '\' AND l.lanname = \'' + langName + '\';' - ].join('\n') - } - var FUNC_NAME = 'get_an_answer' - var RENAME_FUNC_NAME = 'get_the_answer' - - // Set up the table and trigger - before(function(done){ - this.init({ from: 20130909174103, to: 20130909174103}, function(migrator) { - migrator.migrate().success(function(){ - done() - }) - }) - }) - - - it("creates a function " + FUNC_NAME + "()", function(done) { - this.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 'plpgsql')).success(function(rows){ - expect(rows.length).to.equal(1) - done() - }) - }) - - it("renames a function " + FUNC_NAME + "() to " + RENAME_FUNC_NAME + "()", function(done) { - var self = this - this.init({ from: 20130909174253, to: 20130909174253 }, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.query(generateFunctionCountQuery(FUNC_NAME, 'plpgsql')).success(function(rows){ - expect(rows.length).to.equal(0) - self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 'plpgsql')).success(function(rows){ - expect(rows.length).to.equal(1) - done() - }) - }) - }) - }) - }) - - it("deletes a function " + RENAME_FUNC_NAME + "()", function(done) { - var self = this - this.init({ from: 20130909175000, to: 20130909175000 }, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.query(generateFunctionCountQuery(RENAME_FUNC_NAME, 'plpgsql')).success(function(rows){ - expect(rows.length).to.equal(0) - done() - }) - }) - }) - }) - }) - - describe('test trigger migrations', function() { - var generateTriggerCountQuery = function generateTriggerCountQuery(triggerName) { - return 'SELECT * FROM pg_trigger where tgname = \'' + triggerName + '\'' - } - var generateTableCountQuery = function generateTableCountQuery(functionName, schemaName) { - return 'SELECT * FROM pg_tables where tablename = \'' + functionName + '\' and schemaname = \'' + schemaName + '\'' - } - var TRIGGER_NAME = 'updated_at' - var RENAME_TRIGGER_NAME = 'update_updated_at' - var TABLE_NAME = 'trigger_test' - var CATALOG_NAME = 'public' - - // Make sure the function is present - before(function(done){ - this.sequelize.query("CREATE FUNCTION bump_updated_at()\n" + - "RETURNS TRIGGER AS $$\n" + - "BEGIN\n" + - "NEW.updated_at = now();\n" + - "RETURN NEW;\n" + - "END;\n" + - "$$ language 'plpgsql';" - ).success(function() {done()}) - - }) - - // Clean up the function - after(function(done){ - this.sequelize.query("DROP FUNCTION IF EXISTS bump_updated_at()").success(function(){ done(); }) - }) - - it("creates a trigger updated_at on trigger_test", function(done) { - var self = this - this.init({ from: 20130909175939, to: 20130909180846}, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.query(generateTableCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){ - expect(rows.length).to.equal(1) - self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){ - expect(rows.length).to.equal(1) - done() - }) - }) - }) - }) - }) - - it("renames a trigger on " + TABLE_NAME + " from " + TRIGGER_NAME + " to " + RENAME_TRIGGER_NAME, function(done){ - var self = this - this.init({ from: 20130909175939, to: 20130909181148}, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.query(generateTableCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){ - expect(rows.length).to.equal(1) - self.sequelize.query(generateTriggerCountQuery(RENAME_TRIGGER_NAME)).success(function(rows){ - expect(rows.length).to.equal(1) - self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){ - expect(rows.length).to.equal(0) - done() - }) - }) - }) - }) - }) - }) - - it("deletes a trigger " + TRIGGER_NAME + " on trigger_test", function(done) { - var self = this - this.init({ from: 20130909175939, to: 20130909185621}, function(migrator) { - migrator.migrate().success(function(){ - self.sequelize.query(generateTriggerCountQuery(TRIGGER_NAME)).success(function(rows){ - expect(rows.length).to.equal(0) - migrator.migrate({method: 'down'}).success(function(){ - self.sequelize.query(generateTableCountQuery(TABLE_NAME, CATALOG_NAME)).success(function(rows){ - expect(rows.length).to.equal(0) - done() - }) - }) - }) - }) - }) - }) - - }) - - } // if dialect postgres -}) - -}