diff --git a/README.md b/README.md index af57d68..a5f9ace 100644 --- a/README.md +++ b/README.md @@ -241,6 +241,10 @@ $ migrate-mongo status All actions (except ```init```) accept an optional ````-f```` or ````--file```` option to specify a path to a custom config file. By default, migrate-mongo will look for a ````migrate-mongo-config.js```` config file in of the current directory. +### Using a custom timestamp format +If you want to change the timestamp formatting, add a ````timestampFormat```` field to the config file. +The format ````yyyyMMddHHmmss```` is used by default or if none is provided. + #### Example: ````bash diff --git a/lib/actions/create.js b/lib/actions/create.js index 97530b7..bbfddd6 100644 --- a/lib/actions/create.js +++ b/lib/actions/create.js @@ -3,6 +3,7 @@ const path = require("path"); const date = require("../utils/date"); const migrationsDir = require("../env/migrationsDir"); const config = require("../env/config"); +const _ = require("lodash"); module.exports = async description => { if (!description) { @@ -21,7 +22,15 @@ module.exports = async description => { source = path.join(__dirname, `../../samples/${configContent.moduleSystem}/migration.js`); } - const filename = `${date.nowAsString()}-${description + let formatting; + try { + const configContent = await config.read(); + formatting = _.get(configContent, "timestampFormat"); + } catch (err) { + // config file could not be read, assume default formatting + } + + const filename = `${date.nowAsString(formatting)}-${description .split(" ") .join("_")}${migrationExtension}`; const destination = path.join(migrationsDirPath, filename); diff --git a/lib/utils/date.js b/lib/utils/date.js index d558288..34d9bbd 100644 --- a/lib/utils/date.js +++ b/lib/utils/date.js @@ -13,7 +13,7 @@ const now = (dateString = Date.now()) => { ); }; -const nowAsString = () => format(now(), "yyyyMMddHHmmss"); +const nowAsString = (formatting = "yyyyMMddHHmmss") => format(now(), formatting); module.exports = { now, diff --git a/test/actions/create.test.js b/test/actions/create.test.js index 7bbc42d..7d749f2 100644 --- a/test/actions/create.test.js +++ b/test/actions/create.test.js @@ -91,6 +91,35 @@ describe("create", () => { clock.restore(); }); + it("should create a new migration file and yield the filename with custom formatting", async () => { + const configWithTimestamp = { + read: sinon.stub().returns(Promise.resolve({ + moduleSystem: 'commonjs', + timestampFormat: 'yyyy-MM-dd-HH:mm' + })) + }; + + const createWithTimestamp = proxyquire("../../lib/actions/create", { + "../env/migrationsDir": migrationsDir, + "../env/config": configWithTimestamp, + "fs-extra": fs + }); + + const clock = sinon.useFakeTimers( + new Date("2016-06-09T08:07:00.077Z").getTime() + ); + const filename = await createWithTimestamp("my_description"); + expect(fs.copy.called).to.equal(true); + expect(fs.copy.getCall(0).args[0]).to.equal( + path.join(__dirname, "../../samples/commonjs/migration.js") + ); + expect(fs.copy.getCall(0).args[1]).to.equal( + path.join(process.cwd(), "migrations", "2016-06-09-08:07-my_description.js") + ); + expect(filename).to.equal("2016-06-09-08:07-my_description.js"); + clock.restore(); + }); + it("should create a new migration file and yield the filename with custom extension", async () => { const clock = sinon.useFakeTimers( new Date("2016-06-09T08:07:00.077Z").getTime()