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

Commit 95ba7620 by sdepold

up and downwards execution of migrations

1 parent c17a6c71
Showing with 43 additions and 17 deletions
...@@ -22,24 +22,28 @@ module.exports = (function() { ...@@ -22,24 +22,28 @@ module.exports = (function() {
} }
}) })
Migrator.prototype.migrate = function() { Migrator.prototype.migrate = function(options) {
var self = this var self = this
options = Utils._.extend({
method: 'up'
}, options || {})
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
self.getUndoneMigrations(function(migrations) { self.getUndoneMigrations(function(migrations) {
var chainer = new Utils.QueryChainer var chainer = new Utils.QueryChainer
if(options.method == 'down')
migrations.reverse()
migrations.forEach(function(migration) { migrations.forEach(function(migration) {
chainer.add(migration.execute()) chainer.add(migration.execute(options))
}) })
chainer chainer
.run() .run()
.success(function() { .success(function() { emitter.emit('success', null) })
emitter.emit('success', null) .error(function(err) { emitter.emit('failure', err) })
}).error(function(err) {
emitter.emit('failure', err)
})
}) })
}).run() }).run()
} }
......
...@@ -91,17 +91,10 @@ describe('Migrator', function() { ...@@ -91,17 +91,10 @@ describe('Migrator', function() {
}) })
describe('migrate', function() { describe('migrate', function() {
afterEach(function() { var migrator = null
Helpers.async(function(done) {
sequelize.getQueryInterface()
.dropAllTables()
.success(done)
.error(function(err) { console.log(err) })
})
})
it("should execute the specified migration (and e.g. create a file)", function() { beforeEach(function() {
var migrator = new Migrator(sequelize, { migrator = new Migrator(sequelize, {
path: __dirname + '/assets/migrations', path: __dirname + '/assets/migrations',
from: 20111117063700, from: 20111117063700,
to: 20111117063700 to: 20111117063700
...@@ -110,7 +103,16 @@ describe('Migrator', function() { ...@@ -110,7 +103,16 @@ describe('Migrator', function() {
Helpers.async(function(done) { Helpers.async(function(done) {
migrator.migrate().success(done).error(function(err) { console.log(err) }) migrator.migrate().success(done).error(function(err) { console.log(err) })
}) })
})
afterEach(function() {
migrator = null
Helpers.async(function(done) {
sequelize.getQueryInterface().dropAllTables().success(done).error(function(err) { console.log(err) })
})
})
it("executes migration #20111117063700 and correctly creates the table", function() {
Helpers.async(function(done) { Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) { sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.slice('SequelizeMeta', 1) tableNames = tableNames.slice('SequelizeMeta', 1)
...@@ -120,5 +122,25 @@ describe('Migrator', function() { ...@@ -120,5 +122,25 @@ describe('Migrator', function() {
}) })
}) })
}) })
it("executes migration #20111117063700 correctly up (createTable) and downwards (dropTable)", function() {
Helpers.async(function(done) {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.slice('SequelizeMeta', 1)
expect(tableNames.length).toEqual(1)
done()
})
})
Helpers.async(function(done) {
migrator.migrate({ method: 'down' }).success(function() {
sequelize.getQueryInterface().showAllTables().success(function(tableNames) {
tableNames = tableNames.slice('SequelizeMeta', 1)
expect(tableNames.length).toEqual(0)
done()
}).error(function(err){ console.log(err); done() })
}).error(function(err){ console.log(err); done() })
})
})
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!