Skip to content

Commit

Permalink
add support for named db:migrate:undo (#387)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trevor Morris authored and sushantdhiman committed Mar 25, 2017
1 parent dfe14c8 commit 37cca8b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/tasks/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ module.exports = {

'db:migrate:undo': {
descriptions: {
'short': 'Revert the last migration run.'
'short': 'Reverts a migration.',
options: {
'--name': 'Name of the migration to undo.'
}
},

task: function () {
Expand All @@ -211,7 +214,11 @@ module.exports = {
process.exit(0);
}
}).then(function () {
return migrator.down();
if (args.name) {
return migrator.down(args.name);
} else {
return migrator.down();
}
}).then(function () {
process.exit(0);
}).catch(function (err) {
Expand Down
32 changes: 32 additions & 0 deletions test/db/migrate/undo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var expect = require('expect.js');
var Support = require(__dirname + '/../../support');
var helpers = require(__dirname + '/../../support/helpers');
var gulp = require('gulp');
var fs = require('fs');

([
'db:migrate:undo'
Expand Down Expand Up @@ -63,5 +64,36 @@ var gulp = require('gulp');
});
}, 'db:migrate');
});

it('correctly undoes a named migration', function (done) {
var self = this;

prepare(function () {
var migrationsPath = Support.resolveSupportPath('tmp', 'migrations');
var migrations = fs.readdirSync(migrationsPath);
var createPersonMigration = migrations[0];

helpers.readTables(self.sequelize, function (tables) {
expect(tables).to.have.length(2);
expect(tables[0]).to.equal('Person');

gulp
.src(Support.resolveSupportPath('tmp'))
.pipe(helpers.copyMigration('emptyMigration.js'))
.pipe(helpers.runCli('db:migrate'))
.pipe(helpers.runCli(flag + ' --name ' + createPersonMigration, { pipeStdout: true }))
.pipe(helpers.teardown(function () {
helpers.readTables(self.sequelize, function (tables) {
expect(tables).to.have.length(1);
expect(tables[0]).to.equal('SequelizeMeta');
helpers.countTable(self.sequelize, 'SequelizeMeta', function (count) {
expect(count).to.eql([{ count: 1 }]);
done();
});
});
}));
});
}, 'db:migrate');
});
});
});

0 comments on commit 37cca8b

Please sign in to comment.