From 28c89321ba14c77311579ec7e6d9eb0ed1d19fc5 Mon Sep 17 00:00:00 2001 From: Andy Edwards Date: Mon, 12 Feb 2018 11:11:45 -0600 Subject: [PATCH] feat: support passing an array of Migrations --- README.md | 1 + package.json | 2 +- src/index.js | 24 +++++++++++++++--------- test/fixtures/index.js | 31 +++++++++++++++++++++++++++++++ yarn.lock | 2 +- 5 files changed, 49 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 4b79554e..4191d450 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,7 @@ It is possible to configure *umzug* instance via passing an object to the constr // The name of the negative method in migrations. downName: 'down', + // (advanced) you can pass an array of Migration instances instead of the options below migrations: { // The params that gets passed to the migrations. // Might be an array or a synchronous function which returns an array. diff --git a/package.json b/package.json index a4d1e1d5..45e539fe 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umzug", - "version": "2.1.0", + "version": "2.2.0", "description": "Framework agnostic migration tool for Node.JS", "keywords": [ "migrate", diff --git a/src/index.js b/src/index.js index c5cca753..b2601d63 100644 --- a/src/index.js +++ b/src/index.js @@ -29,7 +29,8 @@ module.exports = class Umzug extends EventEmitter { * in migrations. * @param {Object} [options.storageOptions] - The options for the storage. * Check the available storages for further details. - * @param {Object} [options.migrations] - + * @param {Object|Array} [options.migrations] - options for loading migration + * files, or (advanced) an array of Migration instances * @param {Array} [options.migrations.params] - The params that gets passed to * the migrations. Might be an array or a synchronous function which returns * an array. @@ -62,14 +63,16 @@ module.exports = class Umzug extends EventEmitter { throw new Error('The logging-option should be either a function or false'); } - this.options.migrations = { - params: [], - path: path.resolve(process.cwd(), 'migrations'), - pattern: /^\d+[\w-]+\.js$/, - traverseDirectories: false, - wrap: fun => fun, - ...this.options.migrations, - }; + if (!Array.isArray(this.options.migrations)) { + this.options.migrations = { + params: [], + path: path.resolve(process.cwd(), 'migrations'), + pattern: /^\d+[\w-]+\.js$/, + traverseDirectories: false, + wrap: fun => fun, + ...this.options.migrations, + }; + } this.storage = this._initStorage(); } @@ -442,6 +445,9 @@ module.exports = class Umzug extends EventEmitter { * @private */ _findMigrations (migrationPath) { + if (Array.isArray(this.options.migrations)) { + return Bluebird.resolve(this.options.migrations); + } let isRoot = !migrationPath; if (isRoot) { migrationPath = this.options.migrations.path; diff --git a/test/fixtures/index.js b/test/fixtures/index.js index ef84d75e..21649285 100644 --- a/test/fixtures/index.js +++ b/test/fixtures/index.js @@ -6,6 +6,7 @@ import typescript from 'typescript'; import coffeescript from 'coffee-script'; import helper from '../helper'; import Umzug from '../../src'; +import Migration from '../../src/migration'; describe('custom resolver', () => { beforeEach(function () { @@ -56,6 +57,36 @@ describe('custom resolver', () => { await this.verifyTables(); }); + it('an array of migrations created manually can be passed in', async function () { + const umzug = new Umzug({ + migrations: [ + new Migration(require.resolve('./javascript/1.users'), { + upName: 'up', + downName: 'down', + migrations: { + wrap: fn => () => fn(this.sequelize.getQueryInterface(), this.sequelize.constructor), + }, + }), + new Migration(require.resolve('./javascript/2.things'), { + upName: 'up', + downName: 'down', + migrations: { + wrap: fn => () => fn(this.sequelize.getQueryInterface(), this.sequelize.constructor), + }, + }), + ], + storage: 'sequelize', + storageOptions: { + path: this.storagePath, + sequelize: this.sequelize, + }, + }); + + await umzug.up(); + + await this.verifyTables(); + }); + it('can resolve sql files', async function () { this.pattern = /\.sql$/; this.path = resolve(__dirname, 'sql'); diff --git a/yarn.lock b/yarn.lock index 0bd54e0e..a20968a0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3140,7 +3140,7 @@ sprintf-js@~1.0.2: sqlite3@^3.1.13: version "3.1.13" - resolved "https://registry.yarnpkg.com/sqlite3/-/sqlite3-3.1.13.tgz#d990a05627392768de6278bafd1a31fdfe907dd9" + resolved "https://registry.npmjs.org/sqlite3/-/sqlite3-3.1.13.tgz#d990a05627392768de6278bafd1a31fdfe907dd9" dependencies: nan "~2.7.0" node-pre-gyp "~0.6.38"