db-migrate 1.0.0-beta.25 → 1.0.0-beta.26
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/lib/commands/fix.js +2 -1
- package/lib/commands/set-default-argv.js +4 -1
- package/lib/state.js +21 -2
- package/package.json +1 -1
package/lib/commands/fix.js
CHANGED
|
@@ -30,8 +30,9 @@ async function prepare (internals, config) {
|
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
internals.migrationsDir = migrator.directory;
|
|
33
|
+
const backupState = internals.argv['backup-state'];
|
|
33
34
|
|
|
34
|
-
await migrator.createMigrationsTable({ emptyState: true });
|
|
35
|
+
await migrator.createMigrationsTable({ emptyState: true, backupState });
|
|
35
36
|
log.verbose('migration table created');
|
|
36
37
|
|
|
37
38
|
return migrator;
|
|
@@ -16,7 +16,8 @@ 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
|
-
'ignore-completed-migrations': false
|
|
19
|
+
'ignore-completed-migrations': false,
|
|
20
|
+
'backup-state': false
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
if (!isModule) {
|
|
@@ -113,6 +114,8 @@ module.exports = function (internals, isModule) {
|
|
|
113
114
|
.boolean('ignore-completed-migrations')
|
|
114
115
|
.describe('log-level', 'Set the log-level, for example sql|warn')
|
|
115
116
|
.string('log-level')
|
|
117
|
+
.describe('backup-state', 'For use with fix, to take a backup of the current state before continuing.')
|
|
118
|
+
.boolean('backup-state')
|
|
116
119
|
.parse();
|
|
117
120
|
} else {
|
|
118
121
|
internals.argv = Object.assign(defaultConfig, internals.cmdOptions);
|
package/lib/state.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
const MSTATE = '__dbmigrate_state__';
|
|
2
2
|
const SSTATE = '__dbmigrate_schema__';
|
|
3
3
|
const log = require('db-migrate-shared').log;
|
|
4
|
+
const fs = require('fs').promises;
|
|
5
|
+
const path = require('path');
|
|
4
6
|
|
|
5
7
|
module.exports = {
|
|
6
8
|
lockState: async function (driver, state, internals) {
|
|
@@ -29,9 +31,26 @@ module.exports = {
|
|
|
29
31
|
}
|
|
30
32
|
},
|
|
31
33
|
|
|
32
|
-
init: async function (driver, internals, { emptyState }) {
|
|
34
|
+
init: async function (driver, internals, { emptyState, backupState }) {
|
|
33
35
|
await driver._createKV(internals.migrationState);
|
|
34
|
-
const
|
|
36
|
+
const _schema = await driver._getKV(internals.migrationState, SSTATE);
|
|
37
|
+
|
|
38
|
+
if (_schema && backupState) {
|
|
39
|
+
const newName = `${internals.migrationState}_b_${Math.floor((new Date() - 0) / 1000)}`;
|
|
40
|
+
log.info(`[state] Created a backup of ${internals.migrationState} by writing to file ${newName}.dbmigrate`);
|
|
41
|
+
|
|
42
|
+
await fs.writeFile(path.resolve(`${newName}.dbmigrate`), JSON.stringify(_schema), 'utf8');
|
|
43
|
+
|
|
44
|
+
if (emptyState) {
|
|
45
|
+
await driver.renameTable(internals.migrationState,
|
|
46
|
+
newName);
|
|
47
|
+
|
|
48
|
+
log.info(`[state] Created a backup of ${internals.migrationState} by renaming table to ${newName}`);
|
|
49
|
+
await driver._createKV(internals.migrationState);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const schema = emptyState !== true ? _schema : null;
|
|
35
54
|
if (schema) {
|
|
36
55
|
internals.schema = JSON.parse(schema.value);
|
|
37
56
|
} else if (!emptyState) {
|