Skip to content

Commit d8bd797

Browse files
committed
add backup state
Signed-off-by: Tobias Gurtzick <[email protected]>
1 parent 75555ec commit d8bd797

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

lib/commands/fix.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ async function prepare (internals, config) {
3030
}
3131

3232
internals.migrationsDir = migrator.directory;
33+
const backupState = internals.argv['backup-state'];
3334

34-
await migrator.createMigrationsTable({ emptyState: true });
35+
await migrator.createMigrationsTable({ emptyState: true, backupState });
3536
log.verbose('migration table created');
3637

3738
return migrator;

lib/commands/set-default-argv.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ module.exports = function (internals, isModule) {
1616
'migrations-dir': internals.cwd + '/migrations',
1717
'vcseeder-dir': internals.cwd + '/VCSeeder',
1818
'staticseeder-dir': internals.cwd + '/Seeder',
19-
'ignore-completed-migrations': false
19+
'ignore-completed-migrations': false,
20+
'backup-state': false
2021
};
2122

2223
if (!isModule) {
@@ -113,6 +114,8 @@ module.exports = function (internals, isModule) {
113114
.boolean('ignore-completed-migrations')
114115
.describe('log-level', 'Set the log-level, for example sql|warn')
115116
.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')
116119
.parse();
117120
} else {
118121
internals.argv = Object.assign(defaultConfig, internals.cmdOptions);

lib/state.js

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const MSTATE = '__dbmigrate_state__';
22
const SSTATE = '__dbmigrate_schema__';
33
const log = require('db-migrate-shared').log;
4+
const fs = require('fs').promises;
5+
const path = require('path');
46

57
module.exports = {
68
lockState: async function (driver, state, internals) {
@@ -29,9 +31,26 @@ module.exports = {
2931
}
3032
},
3133

32-
init: async function (driver, internals, { emptyState }) {
34+
init: async function (driver, internals, { emptyState, backupState }) {
3335
await driver._createKV(internals.migrationState);
34-
const schema = emptyState !== true ? await driver._getKV(internals.migrationState, SSTATE) : null;
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;
3554
if (schema) {
3655
internals.schema = JSON.parse(schema.value);
3756
} else if (!emptyState) {

0 commit comments

Comments
 (0)