Skip to content

Commit

Permalink
Bugfix/rollback all wrong order (#3172)
Browse files Browse the repository at this point in the history
* Fix rollback all bug attempting to perform rollback in chronological order

* Set up second migration to depend on first migration so when rolling back order is tests

* Add test for rollback all only running completed tests in reverse chronological order
  • Loading branch information
leeallen337 authored and kibertoad committed Apr 30, 2019
1 parent 70712f6 commit a2ab754
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
12 changes: 11 additions & 1 deletion src/migrate/Migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,17 @@ export default class Migrator {
.tap((value) =>
validateMigrationList(this.config.migrationSource, value)
)
.then((val) => (all ? val[0] : this._getLastBatch(val)))
.then((val) => {
const [allMigrations, completedMigrations] = val;

return all
? allMigrations
.filter((migration) => {
return completedMigrations.includes(migration.file);
})
.reverse()
: this._getLastBatch(val);
})
.then((migrations) => {
return this._runBatch(migrations, 'down');
});
Expand Down
46 changes: 46 additions & 0 deletions test/integration/migrate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'use strict';

const equal = require('assert').equal;
const fs = require('fs');
const path = require('path');
const rimraf = require('rimraf');
const Promise = require('bluebird');
Expand Down Expand Up @@ -461,6 +462,51 @@ module.exports = function(knex) {
});
});

describe('knex.migrate.rollback - all', () => {
before(() => {
return knex.migrate.latest({
directory: ['test/integration/migrate/test'],
});
});

it('should only rollback migrations that have been completed and in reverse chronological order', () => {
return knex.migrate
.rollback(
{
directory: [
'test/integration/migrate/test',
'test/integration/migrate/test2',
],
},
true
)
.spread(function(batchNo, log) {
expect(batchNo).to.equal(1);
expect(log).to.have.length(2);

fs.readdirSync('test/integration/migrate/test')
.reverse()
.forEach((fileName, index) => {
expect(fileName).to.equal(log[index]);
});

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
14 changes: 12 additions & 2 deletions test/integration/migrate/test/20131019235306_migration_2.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,22 @@ exports.up = function(knex, promise) {
t.increments();
t.string('name');
})
);
)
.then(() => {
return knex.schema.table('migration_test_1', function(t) {
t.integer('age');
});
});
};

exports.down = function(knex, promise) {
const tableName2 = knex.userParams.customTableName || 'migration_test_2_1';
return knex.schema
.dropTable('migration_test_2')
.then(() => knex.schema.dropTable(tableName2));
.then(() => knex.schema.dropTable(tableName2))
.then(() => {
return knex.schema.table('migration_test_1', function(t) {
t.dropColumn('age');
});
});
};

0 comments on commit a2ab754

Please sign in to comment.