mirror of
https://github.com/knex/knex.git
synced 2025-11-03 11:20:24 +00:00
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:
parent
2c2fe19ad8
commit
c2dff7f843
@ -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');
|
||||
});
|
||||
|
||||
@ -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
2
types/knex.d.ts
vendored
@ -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>;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user