db-migrate 1.0.0-beta.2 → 1.0.0-beta.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +26 -0
- package/README.md +1 -15
- package/api.js +22 -15
- package/connect.js +6 -6
- package/index.js +10 -13
- package/lib/chain.js +66 -11
- package/lib/commands/check.js +3 -3
- package/lib/commands/create-migration.js +121 -28
- package/lib/commands/db.js +2 -0
- package/lib/commands/down.js +1 -1
- package/lib/commands/dump.js +22 -0
- package/lib/commands/generated.js +32 -0
- package/lib/commands/helper/register-events.js +2 -0
- package/lib/commands/run.js +4 -9
- package/lib/commands/set-default-argv.js +13 -15
- package/lib/commands/sync.js +3 -3
- package/lib/commands/up.js +3 -3
- package/lib/config.js +2 -2
- package/lib/driver/index.js +37 -11
- package/lib/executors/versioned/v2.js +13 -3
- package/lib/file.js +2 -4
- package/lib/learn.js +64 -31
- package/lib/methods/v2/conventions.js +47 -0
- package/lib/methods/v2/statetravel.js +11 -9
- package/lib/methods/v2/translatestate.js +45 -12
- package/lib/state.js +5 -5
- package/lib/template.js +34 -4
- package/lib/templates/v2.js +12 -0
- package/lib/temputils.js +8 -0
- package/lib/walker.js +17 -2
- package/package.json +29 -24
- package/.eslintrc +0 -8
- package/.jscsrc +0 -0
- package/.jshintignore +0 -2
- package/.jshintrc +0 -14
- package/.prettierrc +0 -3
- package/.tern-project +0 -7
- package/.travis.yml +0 -47
- package/CHANGELOG.md +0 -474
- package/CODE_OF_CONDUCT.md +0 -46
- package/CONTRIBUTING.md +0 -81
- package/Makefile +0 -8
- package/commitlint.config.js +0 -1
- package/lib/commands/transition.js +0 -5
- package/lib/transitions/1.js +0 -30
- package/lib/transitions/ask.js +0 -22
- package/lib/transitions/snippets/setup.sjs +0 -8
- package/lib/transitions/transitioner.js +0 -78
- package/lib/transitions/try-require.js +0 -113
- package/lib/transitions/update-version.js +0 -48
package/LICENSE
CHANGED
|
@@ -20,3 +20,29 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
20
20
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
21
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
22
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
23
|
+
|
|
24
|
+
AI OSS License
|
|
25
|
+
|
|
26
|
+
Copyright (c) 2015 db-migrate
|
|
27
|
+
|
|
28
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
29
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
30
|
+
in the Software without restriction, including without limitation the rights
|
|
31
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
32
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
33
|
+
furnished to do so, subject to the following conditions:
|
|
34
|
+
|
|
35
|
+
The above copyright notice and this permission notice shall be included in all
|
|
36
|
+
copies or substantial portions of the Software, as well as in results by
|
|
37
|
+
AI models, which result from the source code this License applies to.
|
|
38
|
+
The source code is not allowed to be used for training except for
|
|
39
|
+
models that itself are open source.
|
|
40
|
+
|
|
41
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
42
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
43
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
44
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
45
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
46
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
47
|
+
SOFTWARE.
|
|
48
|
+
|
package/README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
[](#backers) [](#sponsors)
|
|
2
|
-
[](https://github.com/db-migrate/node-db-migrate/actions/workflows/ci.yml)
|
|
3
3
|
[](https://david-dm.org/db-migrate/node-db-migrate)
|
|
4
4
|
[](https://david-dm.org/db-migrate/node-db-migrate#info=devDependencies)
|
|
5
5
|
[](https://readthedocs.org/projects/db-migrate/?badge=latest)
|
|
@@ -31,20 +31,6 @@ Support this project by becoming a sponsor. Your logo will show up here with a l
|
|
|
31
31
|
DB-Migrate is now available to you via:
|
|
32
32
|
|
|
33
33
|
$ db-migrate
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
## Live support
|
|
37
|
-
|
|
38
|
-
### Get help
|
|
39
|
-
|
|
40
|
-
If you have an urgent problem, hire a mentor for a 1:1 live session on Git-Start:
|
|
41
|
-
[](https://git-start.com/help-request/create/db-migrate%2Fnode-db-migrate)
|
|
42
|
-
|
|
43
|
-
### Provide help
|
|
44
|
-
|
|
45
|
-
Help others in paid 1:1 live sessions to get started.
|
|
46
|
-
[](https://git-start.com/help-request/overview/db-migrate%2Fnode-db-migrate)
|
|
47
|
-
|
|
48
34
|
|
|
49
35
|
### As local module
|
|
50
36
|
|
package/api.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module.exports.version = require('./package.json').version;
|
|
4
|
+
|
|
5
|
+
let load;
|
|
4
6
|
var log = require('db-migrate-shared').log;
|
|
5
|
-
require('pkginfo')(module, 'version'); // jshint ignore:line
|
|
6
7
|
var Promise;
|
|
7
|
-
|
|
8
|
+
let onComplete;
|
|
9
|
+
var config = require('rc')('db-migrate');
|
|
8
10
|
|
|
9
11
|
// constant hooks for this file
|
|
10
12
|
var APIHooks = {
|
|
@@ -17,12 +19,18 @@ var APIHooks = {
|
|
|
17
19
|
};
|
|
18
20
|
|
|
19
21
|
function dbmigrate (plugins, isModule, options, callback) {
|
|
22
|
+
if (!options.staticLoader) load = require('./lib/commands');
|
|
23
|
+
else load = require('./lib/commands/generated.js');
|
|
24
|
+
|
|
25
|
+
onComplete = load('on-complete');
|
|
26
|
+
|
|
20
27
|
var dotenv = require('dotenv');
|
|
21
28
|
var setDefaultArgv = load('set-default-argv');
|
|
22
29
|
|
|
23
30
|
this.internals = {
|
|
24
31
|
onComplete: onComplete,
|
|
25
|
-
migrationProtocol: 1
|
|
32
|
+
migrationProtocol: 1,
|
|
33
|
+
load
|
|
26
34
|
};
|
|
27
35
|
if (typeof isModule !== 'function') {
|
|
28
36
|
this.internals.isModule = isModule;
|
|
@@ -37,9 +45,12 @@ function dbmigrate (plugins, isModule, options, callback) {
|
|
|
37
45
|
this.internals.dbm = require('./');
|
|
38
46
|
this.dataType = this.internals.dbm.dataType;
|
|
39
47
|
this.version = this.internals.dbm.version;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
48
|
+
|
|
49
|
+
var dotenvConfig = { silent: true };
|
|
50
|
+
if (config.dotenvCustomPath) {
|
|
51
|
+
dotenvConfig.path = config.dotenvCustomPath;
|
|
52
|
+
}
|
|
53
|
+
dotenv.load(dotenvConfig);
|
|
43
54
|
|
|
44
55
|
/* $lab:coverage:off$ */
|
|
45
56
|
if (!options || !options.throwUncatched) load('helper/register-events')();
|
|
@@ -214,6 +225,9 @@ dbmigrate.prototype = {
|
|
|
214
225
|
if (arguments.length > 0) {
|
|
215
226
|
if (typeof specification === 'number') {
|
|
216
227
|
this.internals.argv.count = arguments[0];
|
|
228
|
+
} else if (typeof specification === 'string') {
|
|
229
|
+
this.internals.argv.destination = specification;
|
|
230
|
+
this.internals.argv.count = Number.MAX_VALUE;
|
|
217
231
|
} else if (typeof specification === 'function') {
|
|
218
232
|
callback = specification;
|
|
219
233
|
}
|
|
@@ -306,13 +320,6 @@ dbmigrate.prototype = {
|
|
|
306
320
|
return log.silence(isSilent);
|
|
307
321
|
},
|
|
308
322
|
|
|
309
|
-
/**
|
|
310
|
-
* Transition migrations to the latest defined protocol.
|
|
311
|
-
*/
|
|
312
|
-
transition: function () {
|
|
313
|
-
load('transition')(this.internals);
|
|
314
|
-
},
|
|
315
|
-
|
|
316
323
|
/**
|
|
317
324
|
* Creates a correctly formatted migration
|
|
318
325
|
*/
|
|
@@ -325,7 +332,7 @@ dbmigrate.prototype = {
|
|
|
325
332
|
this.internals.matching = scope;
|
|
326
333
|
}
|
|
327
334
|
|
|
328
|
-
this.internals.argv._.
|
|
335
|
+
this.internals.argv._.unshift(migrationName);
|
|
329
336
|
return Promise.resolve(
|
|
330
337
|
executeCreateMigration(this.internals, this.config)
|
|
331
338
|
).asCallback(callback);
|
package/connect.js
CHANGED
|
@@ -25,10 +25,10 @@ exports.connect = function (config, PassedClass) {
|
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
+
var dirPath = path.resolve(
|
|
29
|
+
internals.argv['migrations-dir'] || 'migrations'
|
|
30
|
+
);
|
|
28
31
|
if (internals.migrationMode) {
|
|
29
|
-
var dirPath = path.resolve(
|
|
30
|
-
internals.argv['migrations-dir'] || 'migrations'
|
|
31
|
-
);
|
|
32
32
|
if (internals.migrationMode !== 'all') {
|
|
33
33
|
var switched = false;
|
|
34
34
|
var newConf;
|
|
@@ -56,7 +56,7 @@ exports.connect = function (config, PassedClass) {
|
|
|
56
56
|
null,
|
|
57
57
|
new PassedClass(
|
|
58
58
|
db,
|
|
59
|
-
|
|
59
|
+
dirPath,
|
|
60
60
|
internals.mode !== 'static',
|
|
61
61
|
internals,
|
|
62
62
|
prefix
|
|
@@ -69,7 +69,7 @@ exports.connect = function (config, PassedClass) {
|
|
|
69
69
|
null,
|
|
70
70
|
new PassedClass(
|
|
71
71
|
db,
|
|
72
|
-
|
|
72
|
+
dirPath,
|
|
73
73
|
internals.mode !== 'static',
|
|
74
74
|
internals,
|
|
75
75
|
prefix
|
|
@@ -112,7 +112,7 @@ exports.connect = function (config, PassedClass) {
|
|
|
112
112
|
null,
|
|
113
113
|
new PassedClass(
|
|
114
114
|
db,
|
|
115
|
-
|
|
115
|
+
dirPath,
|
|
116
116
|
internals.mode !== 'static',
|
|
117
117
|
internals,
|
|
118
118
|
prefix
|
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
require('
|
|
1
|
+
module.exports.version = require('./package.json').version;
|
|
2
|
+
|
|
2
3
|
var fs = require('fs');
|
|
3
4
|
var path = require('path');
|
|
4
5
|
var log = require('db-migrate-shared').log;
|
|
@@ -46,15 +47,14 @@ function loadPlugins (options) {
|
|
|
46
47
|
for (; i < length; ++i) {
|
|
47
48
|
var plugin = require(path.join(options.cwd, 'node_modules', plugins[i]));
|
|
48
49
|
|
|
49
|
-
if (
|
|
50
|
-
typeof plugin.name !== 'string' ||
|
|
51
|
-
!plugin.hooks ||
|
|
52
|
-
!plugin.loadPlugin
|
|
53
|
-
) {
|
|
50
|
+
if (!plugin.hooks || !plugin.loadPlugin) {
|
|
54
51
|
continue;
|
|
55
52
|
}
|
|
56
53
|
|
|
57
|
-
|
|
54
|
+
// name is now derived from package name
|
|
55
|
+
plugin.name = plugins[i];
|
|
56
|
+
|
|
57
|
+
plugin.hooks.forEach(function (hook) {
|
|
58
58
|
hooks[hook] = hooks[hook] || [];
|
|
59
59
|
hooks[hook].push(plugin);
|
|
60
60
|
});
|
|
@@ -63,15 +63,12 @@ function loadPlugins (options) {
|
|
|
63
63
|
return hooks;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
module.exports.getInstance = function (
|
|
67
|
-
isModule,
|
|
68
|
-
options = { cwd: process.cwd() },
|
|
69
|
-
callback
|
|
70
|
-
) {
|
|
66
|
+
module.exports.getInstance = function (isModule, options = {}, callback) {
|
|
71
67
|
delete require.cache[require.resolve('./api.js')];
|
|
72
|
-
delete require.cache[require.resolve('
|
|
68
|
+
delete require.cache[require.resolve('yargs')];
|
|
73
69
|
var Mod = require('./api.js');
|
|
74
70
|
var plugins = {};
|
|
71
|
+
options.cwd = options.cwd || process.cwd();
|
|
75
72
|
|
|
76
73
|
try {
|
|
77
74
|
if (!options || !options.noPlugins) plugins = loadPlugins(options);
|
package/lib/chain.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
const Promise = require('bluebird');
|
|
2
1
|
const DEFAULT = [
|
|
3
2
|
'renameCollection',
|
|
4
3
|
'dropCollection',
|
|
@@ -16,12 +15,65 @@ const DEFAULT = [
|
|
|
16
15
|
'removeForeignKey'
|
|
17
16
|
];
|
|
18
17
|
|
|
18
|
+
let rollbackCounter = 0;
|
|
19
|
+
let rollbackSignal = null;
|
|
20
|
+
let signal = null;
|
|
21
|
+
let counter = 0;
|
|
22
|
+
|
|
23
|
+
const dbmControl = {
|
|
24
|
+
inc: () => {
|
|
25
|
+
++counter;
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
signal: () => {
|
|
29
|
+
signal = true;
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
hasSignaled: () => {
|
|
33
|
+
return signal;
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
previousSignal: () => {
|
|
37
|
+
const r = rollbackSignal;
|
|
38
|
+
// we reset on retrieval and mark as retrieved
|
|
39
|
+
rollbackSignal = rollbackSignal === null ? null : false;
|
|
40
|
+
return r;
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
get: () => {
|
|
44
|
+
return counter;
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
getPrevious: () => {
|
|
48
|
+
const r = rollbackCounter;
|
|
49
|
+
|
|
50
|
+
// we reset on retrieval and mark as retrieved
|
|
51
|
+
rollbackCounter = rollbackCounter > 0 ? false : 0;
|
|
52
|
+
|
|
53
|
+
return r;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
19
57
|
class Chain {
|
|
20
58
|
constructor (context, file, driver, internals) {
|
|
21
59
|
this.file = file;
|
|
22
60
|
this.context = context;
|
|
23
61
|
this.driver = context;
|
|
24
62
|
this.udriver = driver;
|
|
63
|
+
if (this.udriver._dbmControl !== true) {
|
|
64
|
+
this.udriver._counter = dbmControl;
|
|
65
|
+
this.udriver._dbmControl = true;
|
|
66
|
+
const runSql = this.udriver.runSql;
|
|
67
|
+
this.udriver._dbmControlRSQL = runSql;
|
|
68
|
+
|
|
69
|
+
// couple runSql
|
|
70
|
+
this.udriver.runSql = function (...args) {
|
|
71
|
+
return runSql.apply(this, args).then(x => {
|
|
72
|
+
++counter;
|
|
73
|
+
return x;
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
}
|
|
25
77
|
this.internals = internals;
|
|
26
78
|
this.chains = [];
|
|
27
79
|
this.interfaces = [];
|
|
@@ -32,20 +84,23 @@ class Chain {
|
|
|
32
84
|
DEFAULT.concat(Object.keys(this.context.learnable || [])).forEach(
|
|
33
85
|
method => {
|
|
34
86
|
this[method] = function (...args) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
return Promise.resolve(
|
|
42
|
-
this.exec.apply(this, [method].concat(args))
|
|
43
|
-
).asCallback(cb);
|
|
87
|
+
// we don't handle any callbacks here, v2 ultimately deprecates them
|
|
88
|
+
|
|
89
|
+
this.transferInt();
|
|
90
|
+
|
|
91
|
+
return this.exec.apply(this, [method].concat(args));
|
|
44
92
|
};
|
|
45
93
|
}
|
|
46
94
|
);
|
|
47
95
|
}
|
|
48
96
|
|
|
97
|
+
transferInt () {
|
|
98
|
+
rollbackCounter = counter;
|
|
99
|
+
counter = 0;
|
|
100
|
+
rollbackSignal = signal;
|
|
101
|
+
signal = null;
|
|
102
|
+
}
|
|
103
|
+
|
|
49
104
|
addChain (chain) {
|
|
50
105
|
this.chains.push(chain);
|
|
51
106
|
}
|
|
@@ -72,7 +127,7 @@ class Chain {
|
|
|
72
127
|
|
|
73
128
|
async exec (m, ...args) {
|
|
74
129
|
let ret;
|
|
75
|
-
|
|
130
|
+
const stat = [];
|
|
76
131
|
while (this.step()) {
|
|
77
132
|
ret = await this._interface[m].apply(this._interface, args);
|
|
78
133
|
stat.push(ret);
|
package/lib/commands/check.js
CHANGED
|
@@ -21,15 +21,15 @@ async function prepare (internals, config) {
|
|
|
21
21
|
);
|
|
22
22
|
|
|
23
23
|
if (internals.locTitle) {
|
|
24
|
-
migrator.
|
|
24
|
+
migrator.directory = path.resolve(
|
|
25
25
|
internals.argv['migrations-dir'],
|
|
26
26
|
internals.locTitle
|
|
27
27
|
);
|
|
28
28
|
} else {
|
|
29
|
-
migrator.
|
|
29
|
+
migrator.directory = path.resolve(internals.argv['migrations-dir']);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
internals.migrationsDir = migrator.
|
|
32
|
+
internals.migrationsDir = migrator.directory;
|
|
33
33
|
|
|
34
34
|
await migrator.createMigrationsTable();
|
|
35
35
|
log.verbose('migration table created');
|
|
@@ -5,23 +5,47 @@ const log = require('db-migrate-shared').log;
|
|
|
5
5
|
const mkdirp = Promise.promisify(require('mkdirp'));
|
|
6
6
|
const fs = require('fs');
|
|
7
7
|
const stat = Promise.promisify(fs.stat);
|
|
8
|
-
const
|
|
8
|
+
const writeFile = Promise.promisify(fs.writeFile);
|
|
9
|
+
const yargs = require('yargs');
|
|
9
10
|
const util = require('util');
|
|
11
|
+
const path = require('path');
|
|
10
12
|
|
|
11
13
|
async function createMigrationDir (dir) {
|
|
12
14
|
const res = await stat(dir).catch(_ => {
|
|
13
15
|
return { err: true };
|
|
14
16
|
});
|
|
15
17
|
if (res && res.err === true) {
|
|
16
|
-
|
|
18
|
+
await mkdirp(dir);
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
|
|
21
|
+
// Create migrations/package.json to ensure migration files are
|
|
22
|
+
// executed as CJS and not ESM
|
|
23
|
+
// https://github.com/db-migrate/node-db-migrate/issues/721
|
|
24
|
+
const packageJsonPath = path.resolve(dir, 'package.json');
|
|
25
|
+
|
|
26
|
+
await stat(packageJsonPath).then(
|
|
27
|
+
async () => {
|
|
28
|
+
const packageJson = require(packageJsonPath);
|
|
29
|
+
packageJson.type = 'commonjs';
|
|
30
|
+
const packageJsonStr = JSON.stringify(packageJson, null, 2);
|
|
31
|
+
await writeFile(packageJsonPath, packageJsonStr, 'utf-8');
|
|
32
|
+
},
|
|
33
|
+
async (err) => {
|
|
34
|
+
const packageJson = JSON.stringify({
|
|
35
|
+
type: 'commonjs'
|
|
36
|
+
}, null, 2)
|
|
37
|
+
await writeFile(packageJsonPath, packageJson, 'utf-8');
|
|
38
|
+
}
|
|
39
|
+
)
|
|
20
40
|
}
|
|
21
41
|
|
|
22
42
|
async function executeCreateMigration (internals, config) {
|
|
23
43
|
let migrationsDir = internals.argv['migrations-dir'];
|
|
24
|
-
let
|
|
44
|
+
let path;
|
|
45
|
+
let hooks = false;
|
|
46
|
+
let pluginTemplate = false;
|
|
47
|
+
let customWrite = false;
|
|
48
|
+
const { plugins } = internals;
|
|
25
49
|
|
|
26
50
|
internals.runTimestamp = new Date();
|
|
27
51
|
|
|
@@ -33,25 +57,18 @@ async function executeCreateMigration (internals, config) {
|
|
|
33
57
|
if (internals.argv._.length === 0) {
|
|
34
58
|
log.error("'migrationName' is required.");
|
|
35
59
|
if (!internals.isModule) {
|
|
36
|
-
|
|
60
|
+
yargs.showHelp();
|
|
37
61
|
}
|
|
38
62
|
|
|
39
63
|
throw new Error("'migrationName' is required.");
|
|
40
64
|
}
|
|
41
65
|
|
|
42
|
-
try {
|
|
43
|
-
await createMigrationDir(migrationsDir);
|
|
44
|
-
} catch (err) {
|
|
45
|
-
log.error('Failed to create migration directory at ', migrationsDir, err);
|
|
46
|
-
throw new Error('Failed to create migration directory.');
|
|
47
|
-
}
|
|
48
|
-
|
|
49
66
|
const Migration = require('../template.js');
|
|
50
67
|
|
|
51
68
|
internals.argv.title = internals.argv._.shift();
|
|
52
|
-
folder = internals.argv.title.split('/');
|
|
69
|
+
const folder = internals.argv.title.toString().split('/');
|
|
53
70
|
|
|
54
|
-
internals.argv.title = folder[folder.length -
|
|
71
|
+
internals.argv.title = folder[folder.length - 1] || folder[0];
|
|
55
72
|
path = migrationsDir;
|
|
56
73
|
|
|
57
74
|
if (folder.length > 1) {
|
|
@@ -61,6 +78,14 @@ async function executeCreateMigration (internals, config) {
|
|
|
61
78
|
path += folder[i] + '/';
|
|
62
79
|
}
|
|
63
80
|
}
|
|
81
|
+
internals.argv['migrations-dir'] = path;
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
await createMigrationDir(path);
|
|
85
|
+
} catch (err) {
|
|
86
|
+
log.error('Failed to create migration directory at ', migrationsDir, err);
|
|
87
|
+
throw new Error('Failed to create migration directory.');
|
|
88
|
+
}
|
|
64
89
|
|
|
65
90
|
let templateType = Migration.TemplateType.DEFAULT_JS;
|
|
66
91
|
if (
|
|
@@ -77,19 +102,81 @@ async function executeCreateMigration (internals, config) {
|
|
|
77
102
|
templateType = Migration.TemplateType.SQL_FILE_LOADER;
|
|
78
103
|
} else if (shouldCreateCoffeeFile(internals, config)) {
|
|
79
104
|
templateType = Migration.TemplateType.DEFAULT_COFFEE;
|
|
105
|
+
} else if (shouldCreateV2File(internals, config)) {
|
|
106
|
+
templateType = Migration.TemplateType.V2_DEFAULT;
|
|
80
107
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
108
|
+
if (plugins) {
|
|
109
|
+
hooks = plugins.hook('create:template') || [];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (hooks !== false) {
|
|
113
|
+
for (const plugin of hooks) {
|
|
114
|
+
const template = plugin['init:template']();
|
|
115
|
+
|
|
116
|
+
if (internals.argv[template.option] || config[template.option]) {
|
|
117
|
+
pluginTemplate = plugin;
|
|
118
|
+
customWrite = plugin['create:template:custom:write'] === true;
|
|
119
|
+
|
|
120
|
+
if (customWrite) {
|
|
121
|
+
templateType = `${plugin.name}${templateType}`;
|
|
122
|
+
} else {
|
|
123
|
+
templateType = template.type;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// no reason to continue processing here
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (!pluginTemplate && !customWrite) {
|
|
133
|
+
const migration = new Migration(
|
|
134
|
+
internals.argv.title +
|
|
135
|
+
(shouldCreateCoffeeFile(internals, config) ? '.coffee' : '.js'),
|
|
136
|
+
path,
|
|
137
|
+
internals.runTimestamp,
|
|
138
|
+
templateType,
|
|
139
|
+
internals.plugins
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
await migration.write();
|
|
143
|
+
log.info(util.format('Created migration at %s', migration.file.path));
|
|
144
|
+
if (shouldCreateSqlFiles(internals, config)) {
|
|
145
|
+
return createSqlFiles(internals, config);
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
const plugin = pluginTemplate;
|
|
88
149
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
150
|
+
if (typeof plugin['write:template'] !== 'function') {
|
|
151
|
+
log.error(`Plugin ${plugin.name} does not have function write:template`);
|
|
152
|
+
throw new Error(`write:template not existent!`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
await plugin['write:template'](
|
|
156
|
+
{ argv: { ...internals.argv }, config: { ...config } },
|
|
157
|
+
async opts => {
|
|
158
|
+
let title = internals.argv.title;
|
|
159
|
+
let _path = path;
|
|
160
|
+
const extension = opts.extension || '.js';
|
|
161
|
+
|
|
162
|
+
if (opts.suffix) {
|
|
163
|
+
title += opts.suffix;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
if (opts.pathExtension) {
|
|
167
|
+
_path += opts.pathExtension;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const migration = new Migration(
|
|
171
|
+
title + extension,
|
|
172
|
+
_path,
|
|
173
|
+
internals.runTimestamp,
|
|
174
|
+
opts.type,
|
|
175
|
+
internals.plugins
|
|
176
|
+
);
|
|
177
|
+
await migration.write();
|
|
178
|
+
}
|
|
179
|
+
);
|
|
93
180
|
}
|
|
94
181
|
|
|
95
182
|
return Promise.resolve();
|
|
@@ -107,6 +194,10 @@ function shouldCreateCoffeeFile (internals, config) {
|
|
|
107
194
|
return internals.argv['coffee-file'] || config['coffee-file'];
|
|
108
195
|
}
|
|
109
196
|
|
|
197
|
+
function shouldCreateV2File (internals, config) {
|
|
198
|
+
return internals.argv['v2-file'] || config['v2-file'];
|
|
199
|
+
}
|
|
200
|
+
|
|
110
201
|
async function createSqlFiles (internals, config) {
|
|
111
202
|
let migrationsDir = internals.argv['migrations-dir'];
|
|
112
203
|
|
|
@@ -126,12 +217,13 @@ async function createSqlFiles (internals, config) {
|
|
|
126
217
|
|
|
127
218
|
const Migration = require('../template.js');
|
|
128
219
|
|
|
129
|
-
|
|
220
|
+
const templateTypeDefaultSQL = Migration.TemplateType.DEFAULT_SQL;
|
|
130
221
|
const migrationUpSQL = new Migration(
|
|
131
222
|
internals.argv.title + '-up.sql',
|
|
132
223
|
sqlDir,
|
|
133
224
|
internals.runTimestamp,
|
|
134
|
-
templateTypeDefaultSQL
|
|
225
|
+
templateTypeDefaultSQL,
|
|
226
|
+
internals.plugins
|
|
135
227
|
);
|
|
136
228
|
await migrationUpSQL.write();
|
|
137
229
|
|
|
@@ -143,7 +235,8 @@ async function createSqlFiles (internals, config) {
|
|
|
143
235
|
internals.argv.title + '-down.sql',
|
|
144
236
|
sqlDir,
|
|
145
237
|
internals.runTimestamp,
|
|
146
|
-
templateTypeDefaultSQL
|
|
238
|
+
templateTypeDefaultSQL,
|
|
239
|
+
internals.plugins
|
|
147
240
|
);
|
|
148
241
|
migrationDownSQL.write();
|
|
149
242
|
log.info(
|
package/lib/commands/db.js
CHANGED
package/lib/commands/down.js
CHANGED
|
@@ -22,7 +22,7 @@ async function prepare (internals, config) {
|
|
|
22
22
|
Migrator
|
|
23
23
|
);
|
|
24
24
|
|
|
25
|
-
migrator.
|
|
25
|
+
migrator.directory = path.resolve(internals.argv['migrations-dir']);
|
|
26
26
|
|
|
27
27
|
await migrator.createMigrationsTable();
|
|
28
28
|
log.verbose('migration table created');
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const log = require('db-migrate-shared').log;
|
|
3
|
+
|
|
4
|
+
async function prepare (internals, config) {
|
|
5
|
+
const index = require('../../connect');
|
|
6
|
+
|
|
7
|
+
return index.driver({
|
|
8
|
+
config: config.getCurrent().settings,
|
|
9
|
+
internals: internals
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = async function (internals, config) {
|
|
14
|
+
const driver = await prepare(internals, config);
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const res = await driver.backupGet(internals.argv);
|
|
18
|
+
return internals.onComplete(driver, internals, null, res);
|
|
19
|
+
} catch (err) {
|
|
20
|
+
return internals.onComplete(driver, internals, err);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const path = require('path');
|
|
5
|
+
|
|
6
|
+
const files = {
|
|
7
|
+
"check": require('./check.js'),
|
|
8
|
+
"create-migration": require('./create-migration.js'),
|
|
9
|
+
"db": require('./db.js'),
|
|
10
|
+
"down": require('./down.js'),
|
|
11
|
+
"dump": require('./dump.js'),
|
|
12
|
+
"fn/plugin": require('./fn/plugin.js'),
|
|
13
|
+
"generated": require('./generated.js'),
|
|
14
|
+
"helper/assert": require('./helper/assert.js'),
|
|
15
|
+
"helper/load-config": require('./helper/load-config.js'),
|
|
16
|
+
"helper/migration-hook": require('./helper/migration-hook.js'),
|
|
17
|
+
"helper/register-events": require('./helper/register-events.js'),
|
|
18
|
+
"index": require('./index.js'),
|
|
19
|
+
"on-complete": require('./on-complete.js'),
|
|
20
|
+
"run": require('./run.js'),
|
|
21
|
+
"seed": require('./seed.js'),
|
|
22
|
+
"set-default-argv": require('./set-default-argv.js'),
|
|
23
|
+
"sync": require('./sync.js'),
|
|
24
|
+
"undo-seed": require('./undo-seed.js'),
|
|
25
|
+
"up": require('./up.js')
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function register (module) {
|
|
29
|
+
return files[module];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = register;
|
|
@@ -4,11 +4,13 @@ var log = require('db-migrate-shared').log;
|
|
|
4
4
|
|
|
5
5
|
function registerEvents () {
|
|
6
6
|
process.on('uncaughtException', function (err) {
|
|
7
|
+
log.error('uncaughtException');
|
|
7
8
|
log.error(err.stack || err);
|
|
8
9
|
process.exit(1);
|
|
9
10
|
});
|
|
10
11
|
|
|
11
12
|
process.on('unhandledRejection', function (reason) {
|
|
13
|
+
log.error('unhandledRejection');
|
|
12
14
|
log.error(reason.stack || reason);
|
|
13
15
|
process.exit(1);
|
|
14
16
|
});
|