Skip to content

Commit

Permalink
refactor(executor): use file api
Browse files Browse the repository at this point in the history
Refers to #215
  • Loading branch information
wzrdtales committed Feb 2, 2018
1 parent ffe135b commit d3e563d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 18 deletions.
32 changes: 14 additions & 18 deletions lib/executors/versioned/v1.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
var log = require('db-migrate-shared').log;
var Promise = require('bluebird');
const Promise = require('bluebird');
const { maybePromise } = require('../../temputils.js');

const execUnit = {
up: function (context, driver, execUnit) {
up: function (context, driver, file) {
return context.driver
.startMigration()
.then(() => {
var setup = execUnit.setup;
const _file = file.get();

if (typeof setup === 'function') {
setup(context.internals.execUnitOptions, context.seedLink);
if (typeof _file.setup === 'function') {
_file.setup(context.internals.fileOptions, context.seedLink);
}

return execUnit.up();
return maybePromise(_file.up);
})
.then(() => {
return Promise.promisify(context.writeexecUnitRecord.bind(context))(
execUnit
);
return Promise.promisify(context.writeRecord.bind(context))(file);
})
.then(context.driver.endMigration.bind(context.driver));
},

down: function (context, driver, execUnit) {
down: function (context, driver, file) {
return driver
.startMigration()
.then(() => {
var setup = execUnit.setup;
const _file = file.get();

if (typeof setup === 'function') {
setup(context.internals.execUnitOptions, context.seedLink);
if (typeof _file.setup === 'function') {
_file.setup(context.internals.fileOptions, context.seedLink);
}

return execUnit.down();
return maybePromise(_file.down);
})
.then(() => {
return Promise.promisify(context.deleteexecUnitRecord.bind(context))(
execUnit
);
return Promise.promisify(context.deleteRecord.bind(context))(file);
})
.then(context.driver.endMigration.bind(context.driver));
}
Expand Down
54 changes: 54 additions & 0 deletions lib/temputils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'use strict';

function isPromise (probe) {
return (
probe instanceof Promise ||
(probe &&
probe.then &&
probe.constructor &&
probe.constructor.name === 'Promise')
);
}

function maybePromise (context, action, params) {
let cbExecuted = false;

return new Promise((resolve, reject) => {
const r = err => {
if (cbExecuted === false) {
cbExecuted = true;

if (err) {
reject(err);
} else {
resolve();
}
}
};

params[params.length++] = r;

if (typeof action === 'function') action = action.apply(context, params);
else action = Promise.resolve();

if (isPromise(action)) {
action
.then(() => {
if (cbExecuted === false) {
cbExecuted = true;
resolve();
}
})
.catch(err => {
if (cbExecuted === false) {
cbExecuted = true;
reject(err);
}
});
}
});
}

module.exports = {
maybePromise: maybePromise
};

0 comments on commit d3e563d

Please sign in to comment.