diff --git a/api.js b/api.js index 2763cd5e..0a567edc 100644 --- a/api.js +++ b/api.js @@ -198,7 +198,7 @@ dbmigrate.prototype = { } } - return Promise.resolve(executeUp(this.internals, this.config)).asCallback( + return Promise.resolve(executeUp(this.internals, this.config)).nodeify( callback ); }, @@ -226,9 +226,9 @@ dbmigrate.prototype = { } } - return Promise.promisify( - executeDown(this.internals, this.config) - ).asCallback(callback); + return Promise.resolve(executeDown(this.internals, this.config)).asCallback( + callback + ); }, check: function (specification, opts, callback) { @@ -249,7 +249,7 @@ dbmigrate.prototype = { } } - return Promise.promisify( + return Promise.resolve( executeCheck(this.internals, this.config) ).asCallback(callback); }, @@ -275,9 +275,9 @@ dbmigrate.prototype = { } } - return Promise.promisify( - executeSync(this.internals, this.config) - ).asCallback(callback); + return Promise.resolve(executeSync(this.internals, this.config)).asCallback( + callback + ); }, /** @@ -294,9 +294,9 @@ dbmigrate.prototype = { } this.internals.argv.count = Number.MAX_VALUE; - return Promise.promisify( - executeDown(this.internals, this.config) - ).asCallback(callback); + return Promise.resolve(executeDown(this.internals, this.config)).asCallback( + callback + ); }, /** diff --git a/index.js b/index.js index a7efa680..dd8701e3 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,11 @@ var log = require('db-migrate-shared').log; exports.dataType = require('db-migrate-shared').dataType; function loadPluginList (options) { + try { + fs.accessSync(path.join(options.cwd, 'package.json'), fs.constants.R_OK); + } catch (err) { + return {}; + } var plugins = JSON.parse( fs.readFileSync(path.join(options.cwd, 'package.json'), 'utf-8') ); diff --git a/lib/commands/create-migration.js b/lib/commands/create-migration.js index 72eed38e..8c2e5a3b 100644 --- a/lib/commands/create-migration.js +++ b/lib/commands/create-migration.js @@ -9,8 +9,12 @@ const optimist = require('optimist'); const util = require('util'); async function createMigrationDir (dir) { - await stat(dir); - return mkdirp(dir); + const res = await stat(dir).catch(err => ({ err: true })); + if (res && res.err === true) { + return mkdirp(dir); + } + + return Promise.resolve(); } async function executeCreateMigration (internals, config) { diff --git a/test/integration/api_test.js b/test/integration/api_test.js index 14524e53..63be14cf 100644 --- a/test/integration/api_test.js +++ b/test/integration/api_test.js @@ -136,14 +136,9 @@ lab.experiment('api', function () { function () { return Promise.resolve([ [], // promise - [sinon.spy()], - ['nameatargetmigration', sinon.spy()], // targeted migration ['nameatargetmigration'], // promise targeted migration - [1, sinon.spy()], // targeted migration [1], // promise targeted migration - ['nameatargetmigration', 'testscope', sinon.spy()], // scoped target ['nameatargetmigration', 'testscope'], // promise scope target - [1, 'testscope', sinon.spy()], // scoped target [1, 'testscope'] // promise scope target ]) .each(defaultExecParams('up')) @@ -157,10 +152,7 @@ lab.experiment('api', function () { function () { return Promise.resolve([ [], // promise - [sinon.spy()], - [1, sinon.spy()], // targeted migration [1], // promise targeted migration - [1, 'testscope', sinon.spy()], // scoped target [1, 'testscope'] // promise scope target ]) .each(defaultExecParams('down')) @@ -174,10 +166,7 @@ lab.experiment('api', function () { function () { return Promise.resolve([ [], // promise - [sinon.spy()], - [1, sinon.spy()], // targeted migration [1], // promise targeted migration - [1, 'testscope', sinon.spy()], // scoped target [1, 'testscope'] // promise scope target ]) .each(defaultExecParams('check')) @@ -191,8 +180,6 @@ lab.experiment('api', function () { function () { return Promise.resolve([ [], // promise - [sinon.spy()], - ['testscope', sinon.spy()], // scoped target ['testscope'] // promise scope target ]) .each(defaultExecParams('reset')) @@ -206,9 +193,7 @@ lab.experiment('api', function () { function () { return Promise.resolve([ [], - ['nameatargetmigration', sinon.spy()], // targeted migration ['nameatargetmigration'], // promise targeted migration - ['nameatargetmigration', 'testscope', sinon.spy()], // scoped target ['nameatargetmigration', 'testscope'] // promise scope target ]) .each(defaultExecParams('sync')) @@ -226,7 +211,7 @@ function defaultExecParams (method) { return [api[method].apply(api, args), args]; - function stub (internals, config, callback) { + async function stub (internals, config) { if (typeof args[0] === 'string') { Code.expect(internals.argv.destination).to.equal(args[0]); } else if (typeof args[0] === 'number') { @@ -238,7 +223,7 @@ function defaultExecParams (method) { Code.expect(internals.matching).to.equal(args[1]); } - callback(); + return Promise.resolve(); } }; } diff --git a/test/integration/create_test.js b/test/integration/create_test.js index 11b63a45..155aded6 100644 --- a/test/integration/create_test.js +++ b/test/integration/create_test.js @@ -29,7 +29,11 @@ lab.experiment('create', function () { lab.before(function (done) { wipeMigrations(function (err) { Code.expect(err).to.be.null(); - dbMigrate('create', 'first migration').on('exit', function (code) { + const db = dbMigrate('create', 'first migration'); + // db.stderr.on('data', data => console.log(data.toString())); + // db.stdout.on('data', data => console.log(data.toString())); + + db.on('exit', function (code) { exitCode = code; done(); }); diff --git a/test/on_complete_test.js b/test/on_complete_test.js index 38f64b56..fc6809a3 100644 --- a/test/on_complete_test.js +++ b/test/on_complete_test.js @@ -4,7 +4,7 @@ var lab = (exports.lab = Lab.script()); var onComplete = require('../lib/commands/on-complete'); lab.experiment('on-complete', function () { - lab.test('should invoke the callback with the results', function (done) { + lab.test('should return a promise with the results', async function () { var migratorMock = { driver: { close: function (cb) { @@ -13,15 +13,20 @@ lab.experiment('on-complete', function () { } }; var internalsMock = { - argv: { } + argv: {} }; var resultsPassedToCallback = 'callback should be invoked with results'; - var testCallback = function (err, res) { - Code.expect(err).to.equal(null); - Code.expect(res).to.equal(resultsPassedToCallback); - done(); - }; - onComplete(migratorMock, internalsMock, testCallback, null, resultsPassedToCallback); + const { err, res } = await onComplete( + migratorMock, + internalsMock, + null, + resultsPassedToCallback + ) + .then(res => ({ res })) + .catch(err => ({ err })); + + Code.expect(err).to.equal(undefined); + Code.expect(res).to.equal(resultsPassedToCallback); }); });