From 856c07216964a5c05cfe2cce63c753ecec9f65d3 Mon Sep 17 00:00:00 2001 From: renewooller Date: Tue, 13 Dec 2016 13:00:00 +1000 Subject: [PATCH 01/11] updates to optionally include multiple migration paths. adds tests. --- .gitignore | 3 +++ index.js | 17 +++++++++++------ test/index/constructor.test.js | 17 +++++++++++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 2f9917b9..c73d986a 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,6 @@ node_modules .lock-wscript test/tmp + +# User's IDE data +.vscode diff --git a/index.js b/index.js index de5bea4e..3f668273 100644 --- a/index.js +++ b/index.js @@ -412,14 +412,19 @@ 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); + return Bluebird.resolve(_.flatten([this.options.migrations.path])).reduce((p, c, i, a) => { + return readDirP(c).then((arr) => { + arr = arr.map(v => [c, v]) + return p.concat(arr) + }) + }, []) .bind(this) - .filter(function (file) { - return this.options.migrations.pattern.test(file); + .filter(function (pathFile) { + return this.options.migrations.pattern.test(pathFile[1]); }) - .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); diff --git a/test/index/constructor.test.js b/test/index/constructor.test.js index c7d9c2b8..b8b81194 100644 --- a/test/index/constructor.test.js +++ b/test/index/constructor.test.js @@ -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 () { @@ -39,4 +40,20 @@ describe('constructor', function () { umzug.log(); expect(spy.called).to.be(true); }); + + it('can accept multiple directories for migrations', function() { + let umzug; + return helper.prepareMigrations(1, { names: ['1111-migration', 'tmp2/234-migration'] }) + .then(() => { + umzug = new Umzug({ + migrations: { path: [__dirname + '/../tmp/', __dirname + '/../tmp/tmp2/'] }, + storageOptions: { path: __dirname + '/../tmp/umzug.json' }, + logging: this.logSpy + }); + return umzug._findMigrations() + }).then((migrationsFound) => { + expect(migrationsFound.length).to.be(2) + }) + }) + }); From 308865ba1e9d8e7f78b4125945bd1c1a1f175636 Mon Sep 17 00:00:00 2001 From: renewooller Date: Tue, 13 Dec 2016 14:08:21 +1000 Subject: [PATCH 02/11] updates names to make more readable --- index.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3f668273..8c36e33a 100644 --- a/index.js +++ b/index.js @@ -413,10 +413,12 @@ var Umzug = module.exports = redefine.Class(/** @lends Umzug.prototype */ { */ _findMigrations: function () { let readDirP = Bluebird.promisify(fs.readdir); - return Bluebird.resolve(_.flatten([this.options.migrations.path])).reduce((p, c, i, a) => { - return readDirP(c).then((arr) => { - arr = arr.map(v => [c, v]) - return p.concat(arr) + return Bluebird.resolve( + _.flatten([this.options.migrations.path]) // ensures always an [] + ).reduce((fileAccumulator, currentPath) => { + return readDirP(currentPath).then((files) => { + let pathFiles = files.map(file => [currentPath, file]) + return fileAccumulator.concat(pathFiles) }) }, []) .bind(this) From 7892ff8bc512c3f752ab09c9eba88ab73cc02870 Mon Sep 17 00:00:00 2001 From: renewooller Date: Tue, 13 Dec 2016 14:14:00 +1000 Subject: [PATCH 03/11] unnests path of second migration directory --- .gitignore | 1 + test/index/constructor.test.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c73d986a..9291863c 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ node_modules .lock-wscript test/tmp +test/tmp2 # User's IDE data .vscode diff --git a/test/index/constructor.test.js b/test/index/constructor.test.js index b8b81194..af0a5841 100644 --- a/test/index/constructor.test.js +++ b/test/index/constructor.test.js @@ -43,10 +43,10 @@ describe('constructor', function () { it('can accept multiple directories for migrations', function() { let umzug; - return helper.prepareMigrations(1, { names: ['1111-migration', 'tmp2/234-migration'] }) + return helper.prepareMigrations(2, { names: ['1111-migration', '../tmp2/234-migration'] }) .then(() => { umzug = new Umzug({ - migrations: { path: [__dirname + '/../tmp/', __dirname + '/../tmp/tmp2/'] }, + migrations: { path: [__dirname + '/../tmp/', __dirname + '/../tmp2/'] }, storageOptions: { path: __dirname + '/../tmp/umzug.json' }, logging: this.logSpy }); From a4979d869deabf9d7d4eb7d37eaf330f4cc1ccb7 Mon Sep 17 00:00:00 2001 From: renewooller Date: Tue, 13 Dec 2016 14:15:23 +1000 Subject: [PATCH 04/11] adds second temp migration dir --- test/tmp2/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/tmp2/.gitkeep diff --git a/test/tmp2/.gitkeep b/test/tmp2/.gitkeep new file mode 100644 index 00000000..e69de29b From 81660f99e35c9613bdf9fd92dd6b71ccd22d26c1 Mon Sep 17 00:00:00 2001 From: renewooller Date: Tue, 13 Dec 2016 14:27:35 +1000 Subject: [PATCH 05/11] 1.11.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index d4d7ddf0..5f7adf61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umzug", - "version": "1.11.0", + "version": "1.11.1", "description": "Framework agnostic migration tool for Node.JS", "main": "index.js", "dependencies": { From 008afdf8fbd9b66c8ce1381e561162335966f473 Mon Sep 17 00:00:00 2001 From: renewooller Date: Wed, 14 Dec 2016 11:42:56 +1000 Subject: [PATCH 06/11] adds multiple patterns to config --- index.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 8c36e33a..07446c33 100644 --- a/index.js +++ b/index.js @@ -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); @@ -413,8 +424,10 @@ var Umzug = module.exports = redefine.Class(/** @lends Umzug.prototype */ { */ _findMigrations: function () { let readDirP = Bluebird.promisify(fs.readdir); + let paths = _.flatten([this.options.migrations.path]) + let patterns = _.flatten([this.options.migrations.pattern]) return Bluebird.resolve( - _.flatten([this.options.migrations.path]) // ensures always an [] + paths // ensures always an [] ).reduce((fileAccumulator, currentPath) => { return readDirP(currentPath).then((files) => { let pathFiles = files.map(file => [currentPath, file]) @@ -423,7 +436,8 @@ var Umzug = module.exports = redefine.Class(/** @lends Umzug.prototype */ { }, []) .bind(this) .filter(function (pathFile) { - return this.options.migrations.pattern.test(pathFile[1]); + patterns[paths.indexOf(pathFile[0])] // choose the pattern that relates to the source of this file + .test(pathFile[1]); // regex test }) .map(function (pathFile) { return path.resolve(pathFile[0], pathFile[1]); From a13dd6a652dfb768caeaf3146af1427e0277a484 Mon Sep 17 00:00:00 2001 From: renewooller Date: Wed, 14 Dec 2016 11:43:14 +1000 Subject: [PATCH 07/11] updates test to include multiple patterns --- test/index/constructor.test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/index/constructor.test.js b/test/index/constructor.test.js index af0a5841..4c641625 100644 --- a/test/index/constructor.test.js +++ b/test/index/constructor.test.js @@ -56,4 +56,28 @@ describe('constructor', function () { }) }) + it('can accept multiple directories and patterns for migrations', function() { + let umzug; + return helper.prepareMigrations(4, { 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: [/.*/, /-foo-|-zar-/] + }, + storageOptions: { path: __dirname + '/../tmp/umzug.json' }, + logging: this.logSpy + }); + return umzug._findMigrations() + }).then((migrationsFound) => { + expect(migrationsFound.length).to.be(4) + }) + }) + }); From fc0fc0e7110b6664b90d2738d443478bc99f523b Mon Sep 17 00:00:00 2001 From: renewooller Date: Wed, 14 Dec 2016 11:52:56 +1000 Subject: [PATCH 08/11] updates helper clear to deal with multiple tmp dirs --- test/helper.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/test/helper.js b/test/helper.js index a06e0a5d..97bb3b19 100644 --- a/test/helper.js +++ b/test/helper.js @@ -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); } }); }, @@ -41,6 +42,7 @@ var helper = module.exports = { var num = 0; helper.clearTmp(); + helper.clearTmp(2); _.times(count, function (i) { num++; From 8abd5879cfd36bd8c983f31dbcd290589f699a91 Mon Sep 17 00:00:00 2001 From: renewooller Date: Wed, 14 Dec 2016 11:53:11 +1000 Subject: [PATCH 09/11] fixes bug in filter of patterns --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 07446c33..09b3d96d 100644 --- a/index.js +++ b/index.js @@ -436,7 +436,7 @@ var Umzug = module.exports = redefine.Class(/** @lends Umzug.prototype */ { }, []) .bind(this) .filter(function (pathFile) { - patterns[paths.indexOf(pathFile[0])] // choose the pattern that relates to the source of this file + return patterns[paths.indexOf(pathFile[0])] // choose the pattern that relates to the source of this file .test(pathFile[1]); // regex test }) .map(function (pathFile) { From 7b4e6079481560e5d3bb5a2007c673bbfb432125 Mon Sep 17 00:00:00 2001 From: renewooller Date: Wed, 14 Dec 2016 11:56:08 +1000 Subject: [PATCH 10/11] 1.11.2 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5f7adf61..09ae8a0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "umzug", - "version": "1.11.1", + "version": "1.11.2", "description": "Framework agnostic migration tool for Node.JS", "main": "index.js", "dependencies": { From a8f6a214226b98f443978f92319cbcf56909d8c0 Mon Sep 17 00:00:00 2001 From: renewooller Date: Thu, 15 Dec 2016 08:53:38 +1000 Subject: [PATCH 11/11] updates test to flex default patterns --- test/index/constructor.test.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/test/index/constructor.test.js b/test/index/constructor.test.js index 4c641625..2506e93e 100644 --- a/test/index/constructor.test.js +++ b/test/index/constructor.test.js @@ -58,7 +58,7 @@ describe('constructor', function () { it('can accept multiple directories and patterns for migrations', function() { let umzug; - return helper.prepareMigrations(4, { names: [ + return helper.prepareMigrations(5, { names: [ '1111-foo-migration', '1111-bar-migration', '../tmp2/234-foo-migration', @@ -69,7 +69,7 @@ describe('constructor', function () { umzug = new Umzug({ migrations: { path: [__dirname + '/../tmp/', __dirname + '/../tmp2/'], - pattern: [/.*/, /-foo-|-zar-/] + pattern: [/^.*\.js$/, /-foo-|-zar-/] }, storageOptions: { path: __dirname + '/../tmp/umzug.json' }, logging: this.logSpy @@ -80,4 +80,29 @@ describe('constructor', function () { }) }) + 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) + }) + }) + });