不要怂,就是干,撸起袖子干!

Commit 65518552 by Mick Hansen

Merge pull request #1229 from sequelize/hotfix/undo_migrations

Tests/Fixes for undoing migrations.
2 parents f5d6065f aff5f1c6
......@@ -185,11 +185,12 @@ if (program.migrate) {
if (key === "use_env_variable") {
if (process.env[value]) {
var db_info = process.env[value].match(
/([^:]+):\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/);
var db_info = process.env[value].match(/([^:]+):\/\/([^:]+):([^@]+)@([^:]+):(\d+)\/(.+)/);
config.database = db_info[6];
config.username = db_info[2];
config.password = db_info[3];
options = _.extend(options, {
host: db_info[4],
port: db_info[5],
......@@ -207,15 +208,17 @@ if (program.migrate) {
, migrator = sequelize.getMigrator(migratorOptions)
if (program.undo) {
sequelize.migrator.findOrCreateSequelizeMetaDAO().success(function(Meta) {
migrator.findOrCreateSequelizeMetaDAO().success(function(Meta) {
Meta.find({ order: 'id DESC' }).success(function(meta) {
if (meta) {
migrator = sequelize.getMigrator(_.extend(migratorOptions, meta), true)
}
migrator.migrate({ method: 'down' }).success(function() {
migrator = sequelize.getMigrator(_.extend(migratorOptions, meta.values), true)
migrator.migrate({ method: 'down' }).success(function() {
process.exit(0)
})
} else {
console.log("There are no pending migrations.")
process.exit(0)
})
}
})
})
} else {
......
......@@ -9,10 +9,10 @@ module.exports = (function() {
var Migrator = function(sequelize, options) {
this.sequelize = sequelize
this.options = Utils._.extend({
path: __dirname + '/../migrations',
from: null,
to: null,
logging: console.log,
path: __dirname + '/../migrations',
from: null,
to: null,
logging: console.log,
filesFilter: /\.js$/
}, options || {})
......@@ -49,7 +49,6 @@ module.exports = (function() {
, from = migrations[0]
if (options.method === 'down') {
from = migrations[0]
migrations.reverse()
}
......
......@@ -209,6 +209,106 @@ describe(Support.getTestDialectTeaser("Executable"), function() {
;(function(flags) {
flags.forEach(function(flag) {
var execBinary = function(callback, _flag) {
_flag = _flag || flag
var dialect = Support.getTestDialect()
, config = require(__dirname + '/config/config.js')
config.sqlite.storage = __dirname + "/tmp/test.sqlite"
config = _.extend(config, config[dialect], { dialect: dialect })
exec("echo '" + JSON.stringify(config) + "' > config/config.json", { cwd: __dirname + '/tmp' }, function(error, stdout) {
exec("../../bin/sequelize " + _flag, { cwd: __dirname + "/tmp" }, callback)
})
}
var prepare = function(callback, options) {
options = options || {}
exec("rm -rf ./*", { cwd: __dirname + '/tmp' }, function(error, stdout) {
exec("../../bin/sequelize --init", { cwd: __dirname + '/tmp' }, function(error, stdout) {
exec("cp ../assets/migrations/*-createPerson.js ./migrations/", { cwd: __dirname + '/tmp' }, function(error, stdout) {
exec("cat ../support.js|sed s,/../,/../../, > ./support.js", { cwd: __dirname + '/tmp' }, function(error, stdout) {
if (!options.skipExecBinary) {
execBinary(callback, options.flag)
}
})
})
})
})
}
describe(flag, function() {
it("creates a SequelizeMeta table", function(done) {
var sequelize = this.sequelize
if (this.sequelize.options.dialect === 'sqlite') {
var options = this.sequelize.options
options.storage = __dirname + "/tmp/test.sqlite"
sequelize = new Support.Sequelize("", "", "", options)
}
prepare(function() {
sequelize.getQueryInterface().showAllTables().success(function(tables) {
tables = tables.sort()
expect(tables).to.have.length(1)
expect(tables[0]).to.equal("SequelizeMeta")
done()
})
}.bind(this))
})
it("stops execution if no migrations have been done yet", function(done) {
var sequelize = this.sequelize
if (this.sequelize.options.dialect === 'sqlite') {
var options = this.sequelize.options
options.storage = __dirname + "/tmp/test.sqlite"
sequelize = new Support.Sequelize("", "", "", options)
}
prepare(function(err, output) {
expect(err).to.be.null
expect(output).to.include("There are no pending migrations.")
done()
}.bind(this))
})
it("is correctly undoing a migration if they have been done yet", function(done) {
var sequelize = this.sequelize
if (this.sequelize.options.dialect === 'sqlite') {
var options = this.sequelize.options
options.storage = __dirname + "/tmp/test.sqlite"
sequelize = new Support.Sequelize("", "", "", options)
}
prepare(function() {
sequelize.getQueryInterface().showAllTables().success(function(tables) {
tables = tables.sort()
expect(tables).to.have.length(2)
expect(tables[0]).to.equal("Person")
execBinary(function(err, output) {
sequelize.getQueryInterface().showAllTables().success(function(tables) {
expect(tables).to.have.length(1)
expect(tables[0]).to.equal("SequelizeMeta")
done()
})
})
})
}.bind(this), { flag: '-m' })
})
})
})
})(['--migrate --undo', '-mu'])
;(function(flags) {
flags.forEach(function(flag) {
var prepare = function(callback) {
exec("rm -rf ./*", { cwd: __dirname + '/tmp' }, function(error, stdout) {
exec("../../bin/sequelize --init", { cwd: __dirname + '/tmp' }, function(error, stdout) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!