diff --git a/README.md b/README.md index 746e1983..12d5dff5 100644 --- a/README.md +++ b/README.md @@ -251,6 +251,17 @@ $ migrate-mongo status -f '~/configs/albums-migrations.js' │ 20160608155948-blacklist_the_beatles.js │ PENDING │ └─────────────────────────────────────────┴────────────┘ +```` +### Using a custom migrations directory +All actions (except ```init```) accept an optional ````-md```` or ````--migrations-dir```` option to specify a path to a custom migrations directory. This will override the migrationsDir property in the config file. + +By default, migrate-mongo will look for a ````migrations```` directory in the current directory. + +#### Example: + +````bash +$ migrate-mongo create blacklist_the_doors -md ~/migrations/albums +Created: ~/migrations/albums/20160608155949-blacklist_the_doors.js ```` ### Using npm packages in your migration scripts diff --git a/bin/migrate-mongo.js b/bin/migrate-mongo.js index ac05bfc6..2d0d2298 100755 --- a/bin/migrate-mongo.js +++ b/bin/migrate-mongo.js @@ -49,6 +49,7 @@ program .command("create [description]") .description("create a new database migration with the provided description") .option("-f --file ", "use a custom config file") + .option("-md --migrationsDir ", "use a custom migrations directory") .action((description, options) => { global.options = options; migrateMongo @@ -68,6 +69,7 @@ program .command("up") .description("run all pending database migrations") .option("-f --file ", "use a custom config file") + .option("-md --migrationsDir ", "use a custom migrations directory") .action(options => { global.options = options; migrateMongo.database @@ -87,6 +89,7 @@ program .command("down") .description("undo the last applied database migration") .option("-f --file ", "use a custom config file") + .option("-md --migrationsDir ", "use a custom migrations directory") .action(options => { global.options = options; migrateMongo.database @@ -107,6 +110,7 @@ program .command("status") .description("print the changelog of the database") .option("-f --file ", "use a custom config file") + .option("-md --migrationsDir ", "use a custom migrations directory") .action(options => { global.options = options; migrateMongo.database diff --git a/lib/env/config.js b/lib/env/config.js index 5ff4a303..5caa667a 100644 --- a/lib/env/config.js +++ b/lib/env/config.js @@ -20,6 +20,24 @@ function getConfigPath() { return path.join(process.cwd(), fileOptionValue); } +function setMigrationsDirCommandLineParamInConfig(config) { + const getMigrationDirCommandLineParameter = () => { + const args = process.argv.slice(2); + const mdIndex = args.indexOf('-md'); + return mdIndex !== -1 ? args[mdIndex + 1] : null; + }; + + // deep copy of config + const rtn = JSON.parse(JSON.stringify(config)); + + const md = getMigrationDirCommandLineParameter(); + if (md) { + rtn.migrationsDir = md; + } + + return rtn; +} + module.exports = { DEFAULT_CONFIG_FILE_NAME, @@ -63,7 +81,11 @@ module.exports = { } const configPath = getConfigPath(); try { - return await Promise.resolve(moduleLoader.require(configPath)); + let config = moduleLoader.require(configPath); + + config = setMigrationsDirCommandLineParamInConfig(config); + + return config; } catch (e) { if (e.code === 'ERR_REQUIRE_ESM') { const loadedImport = await moduleLoader.import(url.pathToFileURL(configPath)); diff --git a/test/env/config.test.js b/test/env/config.test.js index bacd7dcb..a5ee42b2 100644 --- a/test/env/config.test.js +++ b/test/env/config.test.js @@ -142,5 +142,33 @@ describe("config", () => { await config.read(); expect(moduleLoader.import.called).to.equal(true); }); + + it("should return an object with the migrationsDir property set if the command line parameter is set", async () => { + + const expected = { migrationsDir: "some/other/dir" }; + moduleLoader.require = sinon.stub().returns({migrationsDir: "the/initial/dir"}); + + // set process.argv to simulate the -md command line parameter + const originalArgv = process.argv; + process.argv = [...originalArgv, "-md", expected.migrationsDir]; + + const actual = await config.read(); + expect(actual).to.deep.equal(expected); + }); + + it("should use the config file migrationsDir property if the command line parameter is not set", async () => { + + const origConfig = { migrationsDir: "the/orig/path" }; + moduleLoader.require = sinon.stub().returns(origConfig); + + // set process.argv to simulate the -md command line parameter + const originalArgv = process.argv; + // originalArgv, filter the -md command line parameter and its value + process.argv = originalArgv.filter(arg => arg !== "-md" && arg !== origConfig.migrationsDir); + + const actual = await config.read(); + expect(actual).to.deep.equal(origConfig); + }); + }); });