Skip to content

Commit

Permalink
feat(state): add state manager and adjust driver functions
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
  • Loading branch information
wzrdtales committed May 13, 2019
1 parent 7c238a4 commit 10c3f1a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 4 deletions.
103 changes: 101 additions & 2 deletions lib/state.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,104 @@
const MSTATE = '__dbmigrate_state__';

module.exports = {
update: function (driver, file, state) {
return Promise.resolve();
init: function (driver, internals) {
if (internals.dryRun) {
return Promise.resolve();
}

return driver._createKV(internals.migrationState);
},

startMigration: async function (driver, file, internals) {
let state = await driver._getKV(internals.migrationState, MSTATE);

if (internals.dryRun) {
return Promise.resolve();
}

if (!state) {
await driver._insertKV(
internals.migrationState,
MSTATE,
JSON.stringify({
s: {
step: 0,
fin: 0,
date: new Date()
}
})
);
} else {
state = JSON.parse(state);
state.s.date = new Date();
await driver._updateKV(
internals.migrationState,
MSTATE,

JSON.stringify(state)
);
}

return driver._insertKV(
internals.migrationState,
file.name,
JSON.stringify({})
);
},

update: function (driver, file, state, internals) {
if (internals.dryRun) {
return Promise.resolve();
}

return driver._updateKV(internals.migrationState, file.name, state);
},

tick: async function (driver, internals) {
if (internals.dryRun) {
return Promise.resolve();
}

let state = await driver._getKV(internals.migrationState, MSTATE);
state = JSON.parse(state);
state.s.date = new Date();
return driver._updateKV(
internals.migrationState,
MSTATE,
JSON.stringify(state)
);
},

step: async function (driver, step, internals) {
if (internals.dryRun) {
return Promise.resolve();
}

let state = await driver._getKV(internals.migrationState, MSTATE);
state = JSON.parse(state);
state.s.date = new Date();
state.s.step = step;
return driver._updateKV(
internals.migrationState,
MSTATE,
JSON.stringify(state)
);
},

endMigration: async function (driver, file, internals) {
if (internals.dryRun) {
return Promise.resolve();
}

let state = await driver._getKV(internals.migrationState, MSTATE);
state = JSON.parse(state);
state.s.date = new Date();
state.s.fin = 1;

return driver._updateKV(
internals.migrationState,
MSTATE,
JSON.stringify(state)
);
}
};
30 changes: 28 additions & 2 deletions lib/walker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const dbmUtil = require('db-migrate-shared').util;
const State = require('./state');
const log = require('db-migrate-shared').log;
const Promise = require('bluebird');
const File = require('./file.js');
Expand Down Expand Up @@ -46,8 +47,33 @@ const Walker = function (driver, directory, mode, intern, prefix) {
};

Walker.prototype = {
createMigrationsTable: function (callback) {
this._driver.createMigrationsTable(callback);
createTables: async function () {
await State.init(this._driver, this.internals.migrationState);
return this._driver._createList(this.internals.migrationTable);
},

createMigrationsTable: function () {
if (
typeof this._driver._createList !== 'function' ||
typeof this._driver._getList !== 'function' ||
typeof this._driver._createKV !== 'function' ||
typeof this._driver._getKV !== 'function' ||
typeof this._driver._deleteKV !== 'function' ||
typeof this._driver._deleteEntry !== 'function' ||
typeof this._driver._insertEntry !== 'function' ||
typeof this._driver._insertKV !== 'function'
) {
log.warn(
'The driver you are using does not support the new state management. ' +
'Please raise an issue in the repository of your driver maintainer'
);

if (typeof this._driver._createList !== 'function') {
return this._driver.createMigrationsTableAsync();
}
}

return this.createTables();
},

writeMigrationRecord: function (migration, callback) {
Expand Down

0 comments on commit 10c3f1a

Please sign in to comment.