Skip to content

Commit 89c6ec3

Browse files
committed
refactor(executor): use file api
Referst to #215 Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent 349fb7b commit 89c6ec3

File tree

2 files changed

+68
-18
lines changed

2 files changed

+68
-18
lines changed

lib/executors/versioned/v1.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
1-
var log = require('db-migrate-shared').log;
2-
var Promise = require('bluebird');
1+
const Promise = require('bluebird');
2+
const { maybePromise } = require('../../temputils.js');
33

44
const execUnit = {
5-
up: function (context, driver, execUnit) {
5+
up: function (context, driver, file) {
66
return context.driver
77
.startMigration()
88
.then(() => {
9-
var setup = execUnit.setup;
9+
const _file = file.get();
1010

11-
if (typeof setup === 'function') {
12-
setup(context.internals.execUnitOptions, context.seedLink);
11+
if (typeof _file.setup === 'function') {
12+
_file.setup(context.internals.fileOptions, context.seedLink);
1313
}
1414

15-
return execUnit.up();
15+
return maybePromise(_file.up);
1616
})
1717
.then(() => {
18-
return Promise.promisify(context.writeexecUnitRecord.bind(context))(
19-
execUnit
20-
);
18+
return Promise.promisify(context.writeRecord.bind(context))(file);
2119
})
2220
.then(context.driver.endMigration.bind(context.driver));
2321
},
2422

25-
down: function (context, driver, execUnit) {
23+
down: function (context, driver, file) {
2624
return driver
2725
.startMigration()
2826
.then(() => {
29-
var setup = execUnit.setup;
27+
const _file = file.get();
3028

31-
if (typeof setup === 'function') {
32-
setup(context.internals.execUnitOptions, context.seedLink);
29+
if (typeof _file.setup === 'function') {
30+
_file.setup(context.internals.fileOptions, context.seedLink);
3331
}
3432

35-
return execUnit.down();
33+
return maybePromise(_file.down);
3634
})
3735
.then(() => {
38-
return Promise.promisify(context.deleteexecUnitRecord.bind(context))(
39-
execUnit
40-
);
36+
return Promise.promisify(context.deleteRecord.bind(context))(file);
4137
})
4238
.then(context.driver.endMigration.bind(context.driver));
4339
}

lib/temputils.js

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
function isPromise (probe) {
4+
return (
5+
probe instanceof Promise ||
6+
(probe &&
7+
probe.then &&
8+
probe.constructor &&
9+
probe.constructor.name === 'Promise')
10+
);
11+
}
12+
13+
function maybePromise (context, action, params) {
14+
let cbExecuted = false;
15+
16+
return new Promise((resolve, reject) => {
17+
const r = err => {
18+
if (cbExecuted === false) {
19+
cbExecuted = true;
20+
21+
if (err) {
22+
reject(err);
23+
} else {
24+
resolve();
25+
}
26+
}
27+
};
28+
29+
params[params.length++] = r;
30+
31+
if (typeof action === 'function') action = action.apply(context, params);
32+
else action = Promise.resolve();
33+
34+
if (isPromise(action)) {
35+
action
36+
.then(() => {
37+
if (cbExecuted === false) {
38+
cbExecuted = true;
39+
resolve();
40+
}
41+
})
42+
.catch(err => {
43+
if (cbExecuted === false) {
44+
cbExecuted = true;
45+
reject(err);
46+
}
47+
});
48+
}
49+
});
50+
}
51+
52+
module.exports = {
53+
maybePromise: maybePromise
54+
};

0 commit comments

Comments
 (0)