From c2dff7f843c0ce9dd99feb0a79c2a1b5bb6148aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Cruz?= Date: Thu, 20 Dec 2018 23:05:28 +0000 Subject: [PATCH] Implemented extra boolean param for rollback() (#2968) * Implemented extra boolean param for rollback() Extra parameter allows all migrations to be rolled back. * Removed spurious log * Removed stray debugger instruction * Moved to arrow functions --- src/migrate/Migrator.js | 6 ++-- test/integration/migrate/index.js | 47 +++++++++++++++++++++++++++++++ types/knex.d.ts | 2 +- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/migrate/Migrator.js b/src/migrate/Migrator.js index b6d61b9ed..0f7dca90a 100644 --- a/src/migrate/Migrator.js +++ b/src/migrate/Migrator.js @@ -99,8 +99,8 @@ export default class Migrator { }); } - // Rollback the last "batch" of migrations that were run. - rollback(config) { + // Rollback the last "batch", or all, of migrations that were run. + rollback(config, all = false) { return Promise.try(() => { this.config = getMergedConfig(config, this.config); @@ -109,7 +109,7 @@ export default class Migrator { .tap((value) => validateMigrationList(this.config.migrationSource, value) ) - .then((val) => this._getLastBatch(val)) + .then((val) => (all ? val[0] : this._getLastBatch(val))) .then((migrations) => { return this._runBatch(migrations, 'down'); }); diff --git a/test/integration/migrate/index.js b/test/integration/migrate/index.js index 5e149040b..5f0fe73dc 100644 --- a/test/integration/migrate/index.js +++ b/test/integration/migrate/index.js @@ -331,6 +331,53 @@ module.exports = function(knex) { }); }); + describe('knex.migrate.rollback - all', () => { + before(() => { + return knex.migrate + .latest({ + directory: ['test/integration/migrate/test'], + }) + .then(function() { + return knex.migrate.latest({ + directory: [ + 'test/integration/migrate/test', + 'test/integration/migrate/test2', + ], + }); + }); + }); + + it('should delete all batches from the migration log', () => { + return knex.migrate + .rollback( + { + directory: [ + 'test/integration/migrate/test', + 'test/integration/migrate/test2', + ], + }, + true + ) + .spread(function(batchNo, log) { + expect(batchNo).to.equal(2); + expect(log).to.have.length(4); + return knex('knex_migrations') + .select('*') + .then(function(data) { + expect(data.length).to.equal(0); + }); + }); + }); + + it('should drop tables as specified in the batch', () => { + return Promise.map(tables, function(table) { + return knex.schema.hasTable(table).then(function(exists) { + expect(!!exists).to.equal(false); + }); + }); + }); + }); + after(function() { rimraf.sync(path.join(__dirname, './migration')); }); diff --git a/types/knex.d.ts b/types/knex.d.ts index a553d2a0d..95a9c9f2a 100644 --- a/types/knex.d.ts +++ b/types/knex.d.ts @@ -829,7 +829,7 @@ declare namespace Knex { interface Migrator { make(name: string, config?: MigratorConfig): Bluebird; latest(config?: MigratorConfig): Bluebird; - rollback(config?: MigratorConfig): Bluebird; + rollback(config?: MigratorConfig, all?: boolean): Bluebird; status(config?: MigratorConfig): Bluebird; currentVersion(config?: MigratorConfig): Bluebird; }