From 37cca8b62cfef590e3aa61660caba692e03c355d Mon Sep 17 00:00:00 2001 From: Trevor Morris Date: Sat, 25 Mar 2017 03:22:02 -0400 Subject: [PATCH] add support for named db:migrate:undo (#387) --- lib/tasks/db.js | 11 +++++++++-- test/db/migrate/undo.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/tasks/db.js b/lib/tasks/db.js index ec8a5cc1..bbccdd10 100644 --- a/lib/tasks/db.js +++ b/lib/tasks/db.js @@ -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 () { @@ -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) { diff --git a/test/db/migrate/undo.test.js b/test/db/migrate/undo.test.js index 0fdc0c6e..8c7011f0 100644 --- a/test/db/migrate/undo.test.js +++ b/test/db/migrate/undo.test.js @@ -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' @@ -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'); + }); }); });