From 6b9fb0ec01788aef2035caacd2856a2f934552d2 Mon Sep 17 00:00:00 2001 From: Nekrasov Ilya Date: Sat, 12 Oct 2019 22:45:39 +0300 Subject: [PATCH] disable_migrations_list_validation_feature (#3448) --- lib/migrate/Migrator.js | 21 +++++++++--- lib/migrate/configuration-merger.js | 1 + test/integration/migrate/index.js | 21 ++++++++++++ .../20131019235306_migration_2.js | 33 +++++++++++++++++++ types/index.d.ts | 1 + 5 files changed, 72 insertions(+), 5 deletions(-) create mode 100644 test/integration/migrate/test_with_missing_first_migration/20131019235306_migration_2.js diff --git a/lib/migrate/Migrator.js b/lib/migrate/Migrator.js index 300d84ac56..d6a7bb31cb 100644 --- a/lib/migrate/Migrator.js +++ b/lib/migrate/Migrator.js @@ -65,7 +65,9 @@ class Migrator { return migrationListResolver .listAllAndCompleted(this.config, this.knex) .then((value) => { - validateMigrationList(this.config.migrationSource, value); + if (!this.config.disableMigrationsListValidation) { + validateMigrationList(this.config.migrationSource, value); + } return value; }) .then(([all, completed]) => { @@ -104,7 +106,9 @@ class Migrator { return migrationListResolver .listAllAndCompleted(this.config, this.knex) .then((value) => { - validateMigrationList(this.config.migrationSource, value); + if (!this.config.disableMigrationsListValidation) { + validateMigrationList(this.config.migrationSource, value); + } return value; }) .then(([all, completed]) => { @@ -170,7 +174,9 @@ class Migrator { migrationListResolver .listAllAndCompleted(this.config, this.knex) .then((value) => { - validateMigrationList(this.config.migrationSource, value); + if (!this.config.disableMigrationsListValidation) { + validateMigrationList(this.config.migrationSource, value); + } return value; }) .then((val) => { @@ -198,7 +204,9 @@ class Migrator { return migrationListResolver .listAllAndCompleted(this.config, this.knex) .then((value) => { - validateMigrationList(this.config.migrationSource, value); + if (!this.config.disableMigrationsListValidation) { + validateMigrationList(this.config.migrationSource, value); + } return value; }) .then(([all, completed]) => { @@ -267,7 +275,10 @@ class Migrator { this.config, this.knex ); - validateMigrationList(this.config.migrationSource, [all, completed]); + + if (!this.config.disableMigrationsListValidation) { + validateMigrationList(this.config.migrationSource, [all, completed]); + } const newMigrations = getNewMigrations( this.config.migrationSource, diff --git a/lib/migrate/configuration-merger.js b/lib/migrate/configuration-merger.js index 6544d4ae73..2d691cbe9a 100644 --- a/lib/migrate/configuration-merger.js +++ b/lib/migrate/configuration-merger.js @@ -10,6 +10,7 @@ const CONFIG_DEFAULT = Object.freeze({ schemaName: null, directory: './migrations', disableTransactions: false, + disableMigrationsListValidation: false, sortDirsSeparately: false, }); diff --git a/test/integration/migrate/index.js b/test/integration/migrate/index.js index 011c8c8576..5d00df84c5 100644 --- a/test/integration/migrate/index.js +++ b/test/integration/migrate/index.js @@ -1036,4 +1036,25 @@ module.exports = function(knex) { }); }); }); + + describe('knex.migrate.latest with disableValidateMigrationList', function() { + it('should not fail if there is a missing migration', () => { + return knex.migrate + .latest({ + directory: 'test/integration/migrate/test', + }) + .then(() => { + return knex.migrate.latest({ + directory: + 'test/integration/migrate/test_with_missing_first_migration', + disableMigrationsListValidation: true, + }); + }) + .finally(() => { + return knex.migrate.rollback({ + directory: 'test/integration/migrate/test', + }); + }); + }); + }); }; diff --git a/test/integration/migrate/test_with_missing_first_migration/20131019235306_migration_2.js b/test/integration/migrate/test_with_missing_first_migration/20131019235306_migration_2.js new file mode 100644 index 0000000000..1411e999e7 --- /dev/null +++ b/test/integration/migrate/test_with_missing_first_migration/20131019235306_migration_2.js @@ -0,0 +1,33 @@ +'use strict'; + +exports.up = function(knex, promise) { + const tableName2 = knex.userParams.customTableName || 'migration_test_2_1'; + return knex.schema + .createTable('migration_test_2', function(t) { + t.increments(); + t.string('name'); + }) + .then(() => + knex.schema.createTable(tableName2, function(t) { + 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(() => { + return knex.schema.table('migration_test_1', function(t) { + t.dropColumn('age'); + }); + }); +}; diff --git a/types/index.d.ts b/types/index.d.ts index 3100581bfd..535e96395a 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -1810,6 +1810,7 @@ declare namespace Knex { tableName?: string; schemaName?: string; disableTransactions?: boolean; + disableMigrationsListValidation?: boolean; sortDirsSeparately?: boolean; loadExtensions?: string[]; migrationSource?: any;