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

Feature: allows multiple different paths to sequelize migration folders to be given #106

Closed
wants to merge 11 commits into from
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ node_modules
.lock-wscript

test/tmp
test/tmp2

# User's IDE data
.vscode
35 changes: 28 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,24 @@ var Umzug = module.exports = redefine.Class(/** @lends Umzug.prototype */ {
throw new Error('The logging-option should be either a function or false');
}

let defaultPattern = /^\d+[\w-]+\.js$/

this.options.migrations = _.assign({
params: [],
path: path.resolve(process.cwd(), 'migrations'),
pattern: /^\d+[\w-]+\.js$/,
pattern: defaultPattern,
wrap: function (fun) { return fun; }
}, this.options.migrations);

// this ensures that the default pattern is used when pattern is undefined, but multiple paths are defined
let patternArr = _.flatten([this.options.migrations.pattern])
let pathArr = _.flatten([this.options.migrations.path])
if(patternArr.length < pathArr.length) {
this.options.migrations.pattern = patternArr.concat(
_().range(pathArr.length-patternArr.length).fill(defaultPattern).value()
)
}

this.storage = this._initStorage();

EventEmitter.call(this);
Expand Down Expand Up @@ -412,14 +423,24 @@ var Umzug = module.exports = redefine.Class(/** @lends Umzug.prototype */ {
* @private
*/
_findMigrations: function () {
return Bluebird
.promisify(fs.readdir)(this.options.migrations.path)
let readDirP = Bluebird.promisify(fs.readdir);
let paths = _.flatten([this.options.migrations.path])
let patterns = _.flatten([this.options.migrations.pattern])
return Bluebird.resolve(
paths // ensures always an []
).reduce((fileAccumulator, currentPath) => {
return readDirP(currentPath).then((files) => {
let pathFiles = files.map(file => [currentPath, file])
return fileAccumulator.concat(pathFiles)
})
}, [])
.bind(this)
.filter(function (file) {
return this.options.migrations.pattern.test(file);
.filter(function (pathFile) {
return patterns[paths.indexOf(pathFile[0])] // choose the pattern that relates to the source of this file
.test(pathFile[1]); // regex test
})
.map(function (file) {
return path.resolve(this.options.migrations.path, file);
.map(function (pathFile) {
return path.resolve(pathFile[0], pathFile[1]);
})
.map(function (path) {
return new Migration(path, this.options);
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": "1.11.0",
"version": "1.11.2",
"description": "Framework agnostic migration tool for Node.JS",
"main": "index.js",
"dependencies": {
Expand Down
12 changes: 7 additions & 5 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ var Bluebird = require('bluebird');
var fs = require('fs');

var helper = module.exports = {
clearTmp: function () {
var files = fs.readdirSync(__dirname + '/tmp');

clearTmp: function (tmpNum) {
tmpNum = tmpNum || ''
var files = fs.readdirSync(__dirname + `/tmp${tmpNum}`);

files.forEach(function (file) {
if (file.match(/\.(js|json|sqlite)$/)) {
fs.unlinkSync(__dirname + '/tmp/' + file);
if (file.match(/\.(js|json|sqlite|coffee)$/)) {
fs.unlinkSync(__dirname + `/tmp${tmpNum}/` + file);
}
});
},
Expand Down Expand Up @@ -41,6 +42,7 @@ var helper = module.exports = {
var num = 0;

helper.clearTmp();
helper.clearTmp(2);

_.times(count, function (i) {
num++;
Expand Down
66 changes: 66 additions & 0 deletions test/index/constructor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var expect = require('expect.js');
var Umzug = require('../../index');
var sinon = require('sinon');
var helper = require('../helper');

describe('constructor', function () {
it('exposes some methods', function () {
Expand Down Expand Up @@ -39,4 +40,69 @@ describe('constructor', function () {
umzug.log();
expect(spy.called).to.be(true);
});

it('can accept multiple directories for migrations', function() {
let umzug;
return helper.prepareMigrations(2, { names: ['1111-migration', '../tmp2/234-migration'] })
.then(() => {
umzug = new Umzug({
migrations: { path: [__dirname + '/../tmp/', __dirname + '/../tmp2/'] },
storageOptions: { path: __dirname + '/../tmp/umzug.json' },
logging: this.logSpy
});
return umzug._findMigrations()
}).then((migrationsFound) => {
expect(migrationsFound.length).to.be(2)
})
})

it('can accept multiple directories and patterns for migrations', function() {
let umzug;
return helper.prepareMigrations(5, { names: [
'1111-foo-migration',
'1111-bar-migration',
'../tmp2/234-foo-migration',
'../tmp2/2345-bar-migration',
'../tmp2/23456-zar-migration'
] })
.then(() => {
umzug = new Umzug({
migrations: {
path: [__dirname + '/../tmp/', __dirname + '/../tmp2/'],
pattern: [/^.*\.js$/, /-foo-|-zar-/]
},
storageOptions: { path: __dirname + '/../tmp/umzug.json' },
logging: this.logSpy
});
return umzug._findMigrations()
}).then((migrationsFound) => {
expect(migrationsFound.length).to.be(4)
})
})

it('can use defaults for multiple directories and patterns for migrations', function() {
let umzug;
return helper.prepareMigrations(6, { names: [
'1111-foo-migration',
'1211-bar-migration',
'1311-to-foo-migration',
'../tmp2/234-foo-migration',
'../tmp2/2345-bar-migration',
'../tmp2/23456-zar-migration'
] })
.then(() => {
umzug = new Umzug({
migrations: {
path: [__dirname + '/../tmp/', __dirname + '/../tmp2/'],
pattern: [/-foo-/]
},
storageOptions: { path: __dirname + '/../tmp/umzug.json' },
logging: this.logSpy
});
return umzug._findMigrations()
}).then((migrationsFound) => {
expect(migrationsFound.length).to.be(5)
})
})

});
Empty file added test/tmp2/.gitkeep
Empty file.