Skip to content

Commit f2be116

Browse files
authored
Merge pull request #496 from gmanriqueUy/master
fix(create): Fix create when using db-migrate as module
2 parents a793f44 + 8fb4a1b commit f2be116

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

api.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,8 @@ function executeCreateMigration(internals, config, callback) {
588588

589589
var migrationsDir = internals.argv['migrations-dir'];
590590

591+
internals.runTimestamp = new Date();
592+
591593
if (internals.migrationMode && internals.migrationMode !== 'all') {
592594

593595
migrationsDir = internals.argv['migrations-dir'] + '/' +
@@ -889,8 +891,6 @@ function run(internals, config) {
889891
var action = internals.argv._.shift(),
890892
folder = action.split(':');
891893

892-
internals.runTimestamp = new Date();
893-
894894
action = folder[0];
895895

896896
switch (action) {

test/migration_test.js

+30
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var Code = require('code');
22
var Lab = require('lab');
3+
var proxyquire = require('proxyquire').noPreserveCache();
34
var lab = exports.lab = Lab.script();
45
var Migration = require('../lib/migration.js');
56

@@ -20,8 +21,21 @@ lab.experiment('migration', { parallel: true }, function() {
2021

2122
lab.experiment('get template', { parallel: true },
2223
getTemplate);
24+
25+
lab.experiment('when using db-migrate as module', { parallel: true },
26+
asModule);
2327
});
2428

29+
function asModule() {
30+
lab.test('should create migration', function (done) {
31+
32+
var dbmigrate = stubApiInstance(true, {}, {});
33+
dbmigrate.setConfigParam('_',[]);
34+
35+
dbmigrate.create('migrationName').then(done);
36+
});
37+
}
38+
2539
function newMigrationObject() {
2640

2741
lab.experiment('with 2 parameters as the complete filepath',
@@ -240,6 +254,22 @@ function getTemplate() {
240254
});
241255
}
242256

257+
function stubApiInstance(isModule, stubs, options, callback) {
258+
259+
delete require.cache[require.resolve('../api.js')];
260+
delete require.cache[require.resolve('optimist')];
261+
var mod = proxyquire('../api.js', stubs),
262+
plugins = {};
263+
options = options || {};
264+
265+
options = Object.assign(options, {
266+
throwUncatched: true,
267+
cwd: __dirname
268+
});
269+
270+
return new mod(plugins, isModule, options, callback);
271+
}
272+
243273
function createDateForTest() {
244274
var date = new Date();
245275
date.setUTCFullYear(2014);

0 commit comments

Comments
 (0)