db-migrate 1.2.0 → 1.4.0
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/README.md +9 -8
- package/api.js +27 -46
- package/lib/commands/generated.js +3 -1
- package/lib/commands/helper/seeder.js +29 -0
- package/lib/commands/run.js +11 -13
- package/lib/commands/seed.js +5 -9
- package/lib/commands/set-default-argv.js +4 -1
- package/lib/commands/undo-seed.js +5 -9
- package/lib/commands/work.js +33 -0
- package/lib/dml.js +1115 -0
- package/lib/driver/index.js +2 -0
- package/lib/executors/versioned/v2.js +18 -78
- package/lib/jobs.js +177 -0
- package/lib/recovery.js +83 -0
- package/lib/seed.js +199 -0
- package/lib/walker.js +51 -3
- package/lib/work.js +260 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -10,6 +10,15 @@
|
|
|
10
10
|
|
|
11
11
|
Database migration framework for node.js
|
|
12
12
|
|
|
13
|
+
> **New: [db-migrate for meta](https://github.com/db-migrate/meta-db-migrate)**
|
|
14
|
+
>
|
|
15
|
+
> db-migrate rewritten in meta: your program ships with its migrations
|
|
16
|
+
> compiled in, as one binary, no node.js needed to migrate. It uses the same
|
|
17
|
+
> migrations table and the same state as db-migrate, v2 migrations included,
|
|
18
|
+
> so a database migrated by one of them can be carried on by the other.
|
|
19
|
+
>
|
|
20
|
+
> [Try meta-db-migrate](https://github.com/db-migrate/meta-db-migrate)
|
|
21
|
+
|
|
13
22
|
# Platinum sponsors
|
|
14
23
|
|
|
15
24
|
[<img src="https://www.wizardtales.com/wzrdtales.png" width="100">](https://www.wizardtales.com)
|
|
@@ -67,14 +76,6 @@ db-migrate to use the `tunnel` setting of your database config:
|
|
|
67
76
|
|
|
68
77
|
$ npm install db-migrate-plugin-tunnel-ssh
|
|
69
78
|
|
|
70
|
-
## db-migrate for meta
|
|
71
|
-
|
|
72
|
-
[meta-db-migrate](https://github.com/db-migrate/meta-db-migrate) is db-migrate
|
|
73
|
-
rewritten in meta: a program ships with its migrations compiled in, as one
|
|
74
|
-
binary. It uses the same migrations table and the same state as db-migrate,
|
|
75
|
-
v2 migrations included, so a database migrated by one of them can be carried
|
|
76
|
-
on by the other.
|
|
77
|
-
|
|
78
79
|
## Resources and usage instructions
|
|
79
80
|
|
|
80
81
|
Please follow the link below, for usage instructions examples and the full
|
package/api.js
CHANGED
|
@@ -422,69 +422,50 @@ dbmigrate.prototype = {
|
|
|
422
422
|
},
|
|
423
423
|
|
|
424
424
|
/**
|
|
425
|
-
*
|
|
426
|
-
* the
|
|
425
|
+
* Runs the jobs of background migrations, see lib/work.js for the
|
|
426
|
+
* options. Returns { done, stop }, done resolves with the jobs done and
|
|
427
|
+
* failed.
|
|
427
428
|
*/
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
429
|
+
executeWork: function (options) {
|
|
430
|
+
return load('work')(this.internals, this.config, options);
|
|
431
|
+
},
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Inserts the rows of the static seeds in seeds-dir, all or the one named,
|
|
435
|
+
* removing the rows of their earlier runs first.
|
|
436
|
+
*/
|
|
437
|
+
seed: function (name, callback) {
|
|
438
|
+
if (typeof name === 'function') {
|
|
439
|
+
callback = name;
|
|
440
|
+
name = undefined;
|
|
433
441
|
}
|
|
434
442
|
|
|
435
|
-
this.internals.
|
|
436
|
-
return Promise.resolve(
|
|
443
|
+
this.internals.seedName = name;
|
|
444
|
+
return Promise.resolve(load('seed')(this.internals, this.config)).asCallback(
|
|
437
445
|
callback
|
|
438
446
|
);
|
|
439
447
|
},
|
|
440
448
|
|
|
441
449
|
/**
|
|
442
|
-
*
|
|
450
|
+
* Removes the rows inserted by the static seeds, all or the one named.
|
|
443
451
|
*/
|
|
444
|
-
undoSeed: function (
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
this.internals.argv.count = specification;
|
|
449
|
-
|
|
450
|
-
if (scope) {
|
|
451
|
-
this.internals.migrationMode = scope;
|
|
452
|
-
this.internals.matching = scope;
|
|
453
|
-
}
|
|
454
|
-
} else if (typeof specification === 'string') {
|
|
455
|
-
this.internals.migrationMode = scope;
|
|
456
|
-
this.internals.matching = scope;
|
|
457
|
-
}
|
|
452
|
+
undoSeed: function (name, callback) {
|
|
453
|
+
if (typeof name === 'function') {
|
|
454
|
+
callback = name;
|
|
455
|
+
name = undefined;
|
|
458
456
|
}
|
|
459
457
|
|
|
458
|
+
this.internals.seedName = name;
|
|
460
459
|
return Promise.resolve(
|
|
461
|
-
|
|
460
|
+
load('undo-seed')(this.internals, this.config)
|
|
462
461
|
).asCallback(callback);
|
|
463
462
|
},
|
|
464
463
|
|
|
465
464
|
/**
|
|
466
|
-
*
|
|
465
|
+
* Removes the rows inserted by all static seeds.
|
|
467
466
|
*/
|
|
468
|
-
resetSeed: function (
|
|
469
|
-
|
|
470
|
-
if (arguments.length > 0) {
|
|
471
|
-
if (typeof specification === 'number') {
|
|
472
|
-
this.internals.argv.count = specification;
|
|
473
|
-
|
|
474
|
-
if (scope) {
|
|
475
|
-
this.internals.migrationMode = scope;
|
|
476
|
-
this.internals.matching = scope;
|
|
477
|
-
}
|
|
478
|
-
} else if (typeof specification === 'string') {
|
|
479
|
-
this.internals.migrationMode = scope;
|
|
480
|
-
this.internals.matching = scope;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
this.internals.argv.count = Number.MAX_VALUE;
|
|
485
|
-
return Promise.resolve(
|
|
486
|
-
executeUndoSeed(this.internals, this.config)
|
|
487
|
-
).asCallback(callback);
|
|
467
|
+
resetSeed: function (callback) {
|
|
468
|
+
return this.undoSeed(undefined, callback);
|
|
488
469
|
},
|
|
489
470
|
|
|
490
471
|
/**
|
|
@@ -17,6 +17,7 @@ const files = {
|
|
|
17
17
|
"helper/migration-hook": require('./helper/migration-hook.js'),
|
|
18
18
|
"helper/register-events": require('./helper/register-events.js'),
|
|
19
19
|
"helper/scopes": require('./helper/scopes.js'),
|
|
20
|
+
"helper/seeder": require('./helper/seeder.js'),
|
|
20
21
|
"index": require('./index.js'),
|
|
21
22
|
"on-complete": require('./on-complete.js'),
|
|
22
23
|
"run": require('./run.js'),
|
|
@@ -24,7 +25,8 @@ const files = {
|
|
|
24
25
|
"set-default-argv": require('./set-default-argv.js'),
|
|
25
26
|
"sync": require('./sync.js'),
|
|
26
27
|
"undo-seed": require('./undo-seed.js'),
|
|
27
|
-
"up": require('./up.js')
|
|
28
|
+
"up": require('./up.js'),
|
|
29
|
+
"work": require('./work.js')
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
function register (module) {
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const Seed = require('../../seed');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Connects like the migrations do, the seeds need the schema of the state
|
|
7
|
+
* to know the tables they can insert into.
|
|
8
|
+
*/
|
|
9
|
+
module.exports = async function (internals, config, action) {
|
|
10
|
+
const Migrator = require('../../walker.js');
|
|
11
|
+
const index = require('../../../connect');
|
|
12
|
+
|
|
13
|
+
const migrator = await index.connect(
|
|
14
|
+
{
|
|
15
|
+
config: config.getCurrent().settings,
|
|
16
|
+
internals: internals,
|
|
17
|
+
prefix: 'migration'
|
|
18
|
+
},
|
|
19
|
+
Migrator
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
await migrator.createMigrationsTable();
|
|
24
|
+
await Seed[action](migrator, internals.seedName);
|
|
25
|
+
return internals.onComplete(migrator, internals);
|
|
26
|
+
} catch (err) {
|
|
27
|
+
return internals.onComplete(migrator, internals, err);
|
|
28
|
+
}
|
|
29
|
+
};
|
package/lib/commands/run.js
CHANGED
|
@@ -97,21 +97,19 @@ function run (internals, config) {
|
|
|
97
97
|
report(load('db')(internals, config));
|
|
98
98
|
}
|
|
99
99
|
break;
|
|
100
|
-
case 'seed':
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
case 'seed': {
|
|
101
|
+
// seed [name], seed down [name], seed reset
|
|
102
|
+
const args = internals.argv._.map(String);
|
|
103
|
+
const undo = args[0] === 'down' || args[0] === 'reset';
|
|
103
104
|
|
|
104
|
-
if (
|
|
105
|
-
|
|
106
|
-
internals.argv.count = Number.MAX_VALUE;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
internals.argv._.shift();
|
|
110
|
-
report(load('undo-seed')(internals, config));
|
|
111
|
-
} else {
|
|
112
|
-
report(load('seed')(internals, config));
|
|
105
|
+
if (undo) {
|
|
106
|
+
args.shift();
|
|
113
107
|
}
|
|
108
|
+
|
|
109
|
+
internals.seedName = args[0];
|
|
110
|
+
report(load(undo ? 'undo-seed' : 'seed')(internals, config));
|
|
114
111
|
break;
|
|
112
|
+
}
|
|
115
113
|
|
|
116
114
|
default:
|
|
117
115
|
var plugins = internals.plugins;
|
|
@@ -125,7 +123,7 @@ function run (internals, config) {
|
|
|
125
123
|
);
|
|
126
124
|
} else {
|
|
127
125
|
log.error(
|
|
128
|
-
'Invalid Action: Must be [up|down|check|create|reset|sync|db].'
|
|
126
|
+
'Invalid Action: Must be [up|down|check|create|reset|sync|db|seed].'
|
|
129
127
|
);
|
|
130
128
|
yargs.showHelp();
|
|
131
129
|
process.exit(1);
|
package/lib/commands/seed.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const seeder = require('./helper/seeder');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
5
|
-
* finished. A new concept follows separately, see #687.
|
|
6
|
+
* Inserts the rows of the static seeds, removing those of earlier runs.
|
|
6
7
|
*/
|
|
7
|
-
module.exports = function () {
|
|
8
|
-
return
|
|
9
|
-
new Error(
|
|
10
|
-
'Seeders are not supported by db-migrate 1.0. The unfinished seeders ' +
|
|
11
|
-
'of db-migrate 0.11 are still available there.'
|
|
12
|
-
)
|
|
13
|
-
);
|
|
8
|
+
module.exports = function (internals, config) {
|
|
9
|
+
return seeder(internals, config, 'run');
|
|
14
10
|
};
|
|
@@ -16,6 +16,7 @@ module.exports = function (internals, isModule) {
|
|
|
16
16
|
'migrations-dir': internals.cwd + '/migrations',
|
|
17
17
|
'vcseeder-dir': internals.cwd + '/VCSeeder',
|
|
18
18
|
'staticseeder-dir': internals.cwd + '/Seeder',
|
|
19
|
+
'seeds-dir': internals.cwd + '/seeds',
|
|
19
20
|
'ignore-completed-migrations': false,
|
|
20
21
|
'backup-state': false,
|
|
21
22
|
'lock-timeout': 60000,
|
|
@@ -26,7 +27,7 @@ module.exports = function (internals, isModule) {
|
|
|
26
27
|
internals.argv = yargs
|
|
27
28
|
.default(defaultConfig)
|
|
28
29
|
.usage(
|
|
29
|
-
'Usage: db-migrate [up|down|check|reset|sync|create|db] ' +
|
|
30
|
+
'Usage: db-migrate [up|down|check|reset|sync|create|db|seed] ' +
|
|
30
31
|
'[[dbname/]migrationName|all] [options]'
|
|
31
32
|
)
|
|
32
33
|
.describe(
|
|
@@ -111,6 +112,8 @@ module.exports = function (internals, isModule) {
|
|
|
111
112
|
.string('vcseeder-dir')
|
|
112
113
|
.describe('staticseeder-dir', 'Set the path to the Seeder directory.')
|
|
113
114
|
.string('staticseeder-dir')
|
|
115
|
+
.describe('seeds-dir', 'Set the path to the directory of the seeds.')
|
|
116
|
+
.string('seeds-dir')
|
|
114
117
|
.describe('non-transactional', 'Explicitly disable transactions')
|
|
115
118
|
.boolean('non-transactional')
|
|
116
119
|
.describe('ignore-completed-migrations', 'Start at the first migration')
|
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const seeder = require('./helper/seeder');
|
|
4
|
+
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
5
|
-
* finished. A new concept follows separately, see #687.
|
|
6
|
+
* Removes the rows inserted by the static seeds.
|
|
6
7
|
*/
|
|
7
|
-
module.exports = function () {
|
|
8
|
-
return
|
|
9
|
-
new Error(
|
|
10
|
-
'Seeders are not supported by db-migrate 1.0. The unfinished seeders ' +
|
|
11
|
-
'of db-migrate 0.11 are still available there.'
|
|
12
|
-
)
|
|
13
|
-
);
|
|
8
|
+
module.exports = function (internals, config) {
|
|
9
|
+
return seeder(internals, config, 'undo');
|
|
14
10
|
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const migrationHook = require('./helper/migration-hook.js');
|
|
4
|
+
const executeWork = require('../work');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Runs the jobs of background migrations, returns { done, stop }.
|
|
8
|
+
*/
|
|
9
|
+
module.exports = function (internals, config, options) {
|
|
10
|
+
let worker = null;
|
|
11
|
+
let stopped = false;
|
|
12
|
+
|
|
13
|
+
const done = Promise.resolve(migrationHook(internals)).then(() => {
|
|
14
|
+
if (stopped) {
|
|
15
|
+
return { done: [], failed: [] };
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
worker = executeWork(internals, config, options);
|
|
19
|
+
return worker.done;
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
done,
|
|
24
|
+
stop: () => {
|
|
25
|
+
stopped = true;
|
|
26
|
+
if (worker) {
|
|
27
|
+
worker.stop();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return done;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
};
|