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

support passing an array of Migrations #164

Merged
merged 1 commit into from
Mar 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "umzug",
"version": "2.1.0",
"version": "2.2.0",
"description": "Framework agnostic migration tool for Node.JS",
"keywords": [
"migrate",
Expand Down
24 changes: 15 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions test/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down