Skip to content

Commit 37cca8b

Browse files
Trevor Morrissushantdhiman
authored andcommitted
add support for named db:migrate:undo (#387)
1 parent dfe14c8 commit 37cca8b

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

lib/tasks/db.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,10 @@ module.exports = {
198198

199199
'db:migrate:undo': {
200200
descriptions: {
201-
'short': 'Revert the last migration run.'
201+
'short': 'Reverts a migration.',
202+
options: {
203+
'--name': 'Name of the migration to undo.'
204+
}
202205
},
203206

204207
task: function () {
@@ -211,7 +214,11 @@ module.exports = {
211214
process.exit(0);
212215
}
213216
}).then(function () {
214-
return migrator.down();
217+
if (args.name) {
218+
return migrator.down(args.name);
219+
} else {
220+
return migrator.down();
221+
}
215222
}).then(function () {
216223
process.exit(0);
217224
}).catch(function (err) {

test/db/migrate/undo.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var expect = require('expect.js');
44
var Support = require(__dirname + '/../../support');
55
var helpers = require(__dirname + '/../../support/helpers');
66
var gulp = require('gulp');
7+
var fs = require('fs');
78

89
([
910
'db:migrate:undo'
@@ -63,5 +64,36 @@ var gulp = require('gulp');
6364
});
6465
}, 'db:migrate');
6566
});
67+
68+
it('correctly undoes a named migration', function (done) {
69+
var self = this;
70+
71+
prepare(function () {
72+
var migrationsPath = Support.resolveSupportPath('tmp', 'migrations');
73+
var migrations = fs.readdirSync(migrationsPath);
74+
var createPersonMigration = migrations[0];
75+
76+
helpers.readTables(self.sequelize, function (tables) {
77+
expect(tables).to.have.length(2);
78+
expect(tables[0]).to.equal('Person');
79+
80+
gulp
81+
.src(Support.resolveSupportPath('tmp'))
82+
.pipe(helpers.copyMigration('emptyMigration.js'))
83+
.pipe(helpers.runCli('db:migrate'))
84+
.pipe(helpers.runCli(flag + ' --name ' + createPersonMigration, { pipeStdout: true }))
85+
.pipe(helpers.teardown(function () {
86+
helpers.readTables(self.sequelize, function (tables) {
87+
expect(tables).to.have.length(1);
88+
expect(tables[0]).to.equal('SequelizeMeta');
89+
helpers.countTable(self.sequelize, 'SequelizeMeta', function (count) {
90+
expect(count).to.eql([{ count: 1 }]);
91+
done();
92+
});
93+
});
94+
}));
95+
});
96+
}, 'db:migrate');
97+
});
6698
});
6799
});

0 commit comments

Comments
 (0)