Skip to content

Commit

Permalink
feat(statemanager): add first edition of state manager
Browse files Browse the repository at this point in the history
refers to #538

Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
  • Loading branch information
wzrdtales committed May 14, 2019
1 parent 1013c84 commit 6dc4d3b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
4 changes: 3 additions & 1 deletion lib/commands/create-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const optimist = require('optimist');
const util = require('util');

async function createMigrationDir (dir) {
const res = await stat(dir).catch(err => ({ err: true }));
const res = await stat(dir).catch(_ => {
return { err: true };
});
if (res && res.err === true) {
return mkdirp(dir);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/methods/v2/statetravel.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class StateTravel {

Object.keys(DEFAULT).forEach(m => {
StateTravel.prototype[m] = async function (...args) {
await State.step(this.driver, this._counter++, this.internals);
await State.step(this.driver, ++this._counter, this.internals);
return State.update(
this.driver,
this.file,
Expand Down
60 changes: 40 additions & 20 deletions lib/state.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
const MSTATE = '__dbmigrate_state__';
const log = require('db-migrate-shared').log;

module.exports = {
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();
}

lockState: async function (driver, state, internals) {
if (!state) {
await driver._insertKV(
return driver._insertKV(
internals.migrationState,
MSTATE,
JSON.stringify({
Expand All @@ -29,15 +16,36 @@ module.exports = {
})
);
} else {
state = state.value;
state = JSON.parse(state);
state.s.date = new Date();
await driver._updateKV(
return driver._updateKV(
internals.migrationState,
MSTATE,

JSON.stringify(state)
);
}
},

init: async function (driver, internals) {
await driver._createKV(internals.migrationState);
if (!internals.dryRun) {
let state = await driver._getKV(internals.migrationState, MSTATE);
return module.exports.lockState(driver, state, internals);
}

return Promise.resolve();
},

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

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

await module.exports.lockState(driver, state, internals);

return driver._insertKV(
internals.migrationState,
Expand All @@ -47,7 +55,9 @@ module.exports = {
},

update: function (driver, file, state, internals) {
console.log('called');
if (internals.dryRun) {
log.info(`[state] update state`);
return Promise.resolve();
}

Expand All @@ -59,7 +69,10 @@ module.exports = {
return Promise.resolve();
}

let state = await driver._getKV(internals.migrationState, MSTATE);
let { value: state } = await driver._getKV(
internals.migrationState,
MSTATE
);
state = JSON.parse(state);
state.s.date = new Date();
return driver._updateKV(
Expand All @@ -70,11 +83,15 @@ module.exports = {
},

step: async function (driver, step, internals) {
log.verbose(`[state] proceeded to step ${step}`);
if (internals.dryRun) {
return Promise.resolve();
}

let state = await driver._getKV(internals.migrationState, MSTATE);
let { value: state } = await driver._getKV(
internals.migrationState,
MSTATE
);
state = JSON.parse(state);
state.s.date = new Date();
state.s.step = step;
Expand All @@ -90,7 +107,10 @@ module.exports = {
return Promise.resolve();
}

let state = await driver._getKV(internals.migrationState, MSTATE);
let { value: state } = await driver._getKV(
internals.migrationState,
MSTATE
);
state = JSON.parse(state);
state.s.date = new Date();
state.s.fin = 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const Walker = function (driver, directory, mode, intern, prefix) {

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

Expand Down

0 comments on commit 6dc4d3b

Please sign in to comment.