Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended migration validation to guard against out-of-order migrations #3824

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions lib/migrate/Migrator.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Migrator
// -------
const difference = require('lodash/difference');
const differenceWith = require('lodash/differenceWith');
const get = require('lodash/get');
const isBoolean = require('lodash/isBoolean');
const isEmpty = require('lodash/isEmpty');
const isFunction = require('lodash/isFunction');
const last = require('lodash/last');
const max = require('lodash/max');
const inherits = require('inherits');
const {
Expand Down Expand Up @@ -555,6 +557,23 @@ function validateMigrationList(migrationSource, migrations) {
)}`
);
}

// Check if there are some migrations which should've already completed
const lastCompleted = last(completed);
if (!lastCompleted) {
return;
}

const allNames = all.map(migrationSource.getMigrationName);
const newMigrations = difference(allNames, completed);

const lastMigrationIndex = allNames.indexOf(lastCompleted);
const upcomingMigrations = allNames.slice(lastMigrationIndex + 1);
const reordered = difference(newMigrations, upcomingMigrations);
if (!isEmpty(reordered)) {
const message = `The migration directory is corrupt, the following migrations should have already been run (but didn't): ${reordered.join(', ')}`;
throw new Error(message);
}
}

function getMissingMigrations(migrationSource, completed, all) {
Expand Down