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
This commit is contained in:
André Cruz 2018-12-20 23:05:28 +00:00 committed by Igor Savin
parent 2c2fe19ad8
commit c2dff7f843
3 changed files with 51 additions and 4 deletions

View File

@ -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');
});

View File

@ -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'));
});

2
types/knex.d.ts vendored
View File

@ -829,7 +829,7 @@ declare namespace Knex {
interface Migrator {
make(name: string, config?: MigratorConfig): Bluebird<string>;
latest(config?: MigratorConfig): Bluebird<any>;
rollback(config?: MigratorConfig): Bluebird<any>;
rollback(config?: MigratorConfig, all?: boolean): Bluebird<any>;
status(config?: MigratorConfig): Bluebird<number>;
currentVersion(config?: MigratorConfig): Bluebird<string>;
}