Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for named db:migrate:undo #387

Merged
merged 1 commit into from
Mar 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/tasks/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,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 @@ -180,7 +183,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');
});
});
});