Skip to content

Commit

Permalink
Merge b9e778c into 2c2fe19
Browse files Browse the repository at this point in the history
  • Loading branch information
edevil committed Dec 20, 2018
2 parents 2c2fe19 + b9e778c commit b8bddee
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/migrate/Migrator.js
Expand Up @@ -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);

Expand All @@ -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');
});
Expand Down
47 changes: 47 additions & 0 deletions test/integration/migrate/index.js
Expand Up @@ -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'));
});
Expand Down
2 changes: 1 addition & 1 deletion types/knex.d.ts
Expand Up @@ -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>;
}
Expand Down

0 comments on commit b8bddee

Please sign in to comment.