db-migrate 1.0.0-beta.25 → 1.0.0-beta.27

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.
@@ -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);
@@ -73,7 +73,7 @@ const execUnit = {
73
73
  chain.addChain(AddConventions);
74
74
  }
75
75
  chain.addChain(Learn);
76
- // chain.addChain(StateTravel);
76
+ chain.addChain(StateTravel);
77
77
 
78
78
  await State.startMigration(context._driver, file, context.internals);
79
79
  // startMigration - needs secondary instance since we can not afford to
@@ -99,7 +99,6 @@ const execUnit = {
99
99
  await execUnit.down(context, driver, file);
100
100
  throw err;
101
101
  }
102
- await Promise.promisify(context.writeMigrationRecord.bind(context))(file);
103
102
  await State.endMigration(context._driver, file, context.internals);
104
103
  log.verbose(`[fix] current schema`, util.inspect(context.internals.schema, false, null, true));
105
104
  // end migration, same as start migration
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,27 @@ 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 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
+ await driver._insertKV(internals.migrationState, SSTATE, '{}');
51
+ }
52
+ }
53
+
54
+ const schema = emptyState !== true ? _schema : null;
35
55
  if (schema) {
36
56
  internals.schema = JSON.parse(schema.value);
37
57
  } else if (!emptyState) {
package/lib/walker.js CHANGED
@@ -64,7 +64,7 @@ Walker.prototype = {
64
64
  return this._driver._createList(this.internals.migrationTable);
65
65
  },
66
66
 
67
- createMigrationsTable: function (options) {
67
+ createMigrationsTable: function (options = {}) {
68
68
  if (
69
69
  typeof this._driver._createList !== 'function' ||
70
70
  typeof this._driver._getList !== 'function' ||
package/package.json CHANGED
@@ -31,7 +31,7 @@
31
31
  "programable",
32
32
  "api"
33
33
  ],
34
- "version": "1.0.0-beta.25",
34
+ "version": "1.0.0-beta.27",
35
35
  "engines": {
36
36
  "node": ">=8.0.0"
37
37
  },