Skip to content

Commit

Permalink
own the state
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Gurtzick <[email protected]>
  • Loading branch information
wzrdtales committed Sep 7, 2023
1 parent c523073 commit e0bdae5
Showing 1 changed file with 42 additions and 4 deletions.
46 changes: 42 additions & 4 deletions lib/state.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,68 @@
const MSTATE = '__dbmigrate_state__';
const SSTATE = '__dbmigrate_schema__';
const RUNON =
'run_on';

const log = require('db-migrate-shared').log;
const fs = require('fs').promises;
const path = require('path');
const crypto = require('crypto');

const ID = crypto.randomBytes(32).toString('base64');
let owner = false;

Check warning on line 12 in lib/state.js

View workflow job for this annotation

GitHub Actions / build (14.x)

'owner' is assigned a value but never used

Check warning on line 12 in lib/state.js

View workflow job for this annotation

GitHub Actions / build (16.x)

'owner' is assigned a value but never used

Check warning on line 12 in lib/state.js

View workflow job for this annotation

GitHub Actions / build (18.x)

'owner' is assigned a value but never used

Check warning on line 12 in lib/state.js

View workflow job for this annotation

GitHub Actions / build (latest)

'owner' is assigned a value but never used

module.exports = {
/**
* Lock state will add the basic state controller an ID
* and a process controlled date.
* It is important that all systems are in sync. We will use the
* database clock instead if possible.
*/
lockState: async function (driver, state, internals) {
if (!state) {
owner = true;
return driver._insertKV(
internals.migrationState,
MSTATE,
JSON.stringify({
s: {
step: 0,
fin: 0,
ID,
date: new Date()
}
})
);
} else {
let runOn = state.run_on;
state = state.value;
state = JSON.parse(state);
state.s.date = new Date();
return driver._updateKV(

if (!state.s.ID || state.s.ID !== ID) {
owner = true;
state.s.ID = ID;
}

const d = new Date();

state.s.date = d;
await driver._updateKVC(
internals.migrationState,
MSTATE,

JSON.stringify(state)
JSON.stringify(state),
RUNON,
runOn
);

state = await driver._getKV(internals.migrationState, MSTATE);

runOn = state.run_on;
state = state.value;
state = JSON.parse(state);

if (state.s.ID !== ID) {
owner = false;
}
}
},

Expand Down Expand Up @@ -165,6 +200,9 @@ module.exports = {
state.s.date = new Date();
state.s.fin = 1;

// unlock from current process
state.s.ID = 0;

return driver._updateKV(
internals.migrationState,
MSTATE,
Expand Down

0 comments on commit e0bdae5

Please sign in to comment.