Skip to content

Commit

Permalink
all state actions on secondary db line
Browse files Browse the repository at this point in the history
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
  • Loading branch information
wzrdtales committed Sep 7, 2023
1 parent 5bafb17 commit 6acaf40
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 22 deletions.
7 changes: 5 additions & 2 deletions lib/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ const dbmControl = {
};

class Chain {
constructor (context, file, driver, internals) {
constructor (context, file, driver, internals, pdriver) {
this.file = file;
this.context = context;
this.driver = context;
this.pdriver = pdriver;
this.udriver = driver;
if (this.udriver._dbmControl !== true) {
this.udriver._counter = dbmControl;
Expand Down Expand Up @@ -113,7 +114,8 @@ class Chain {
this.context,
this.file,
this.udriver,
this.internals
this.internals,
this.pdriver
);
}

Expand All @@ -136,6 +138,7 @@ class Chain {
this._step.useState(ret);
}

// need to check if we still want this
if (this._step.hasModification) {
const ret = this._step.modify(
this.context,
Expand Down
18 changes: 9 additions & 9 deletions lib/executors/versioned/v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ const execUnit = {

up: async function (context, driver, file) {
const _file = file.get();
const chain = new Chain(context._driver, file, driver, context.internals);
const chain = new Chain(context._driver, file, driver, context.internals, context._pdriver);
if (!_file._meta.noDefaultColumn) {
chain.addChain(AddConventions);
}
chain.addChain(Learn);
chain.addChain(StateTravel);
chain.addChain(Migrate);

await State.startMigration(context._driver, file, context.internals);
await State.startMigration(context._pdriver, file, context.internals);
// startMigration - needs secondary instance since we can not afford to
// loose state and the transaction start will include these for roll back
// we will disable them probably at all from DDL when the driver does not
Expand All @@ -62,20 +62,20 @@ const execUnit = {
throw err;
}
await Promise.promisify(context.writeMigrationRecord.bind(context))(file);
return State.endMigration(context._driver, file, context.internals);
return State.endMigration(context._pdriver, file, context.internals);
// end migration, same as start migration
},

fix: async function (context, driver, file) {
const _file = file.get();
const chain = new Chain(context._driver, file, driver, context.internals);
const chain = new Chain(context._driver, file, driver, context.internals, context._pdriver);
if (!_file._meta.noDefaultColumn) {
chain.addChain(AddConventions);
}
chain.addChain(Learn);
chain.addChain(StateTravel);

await State.startMigration(context._driver, file, context.internals);
await State.startMigration(context._pdriver, file, context.internals);
// startMigration - needs secondary instance since we can not afford to
// loose state and the transaction start will include these for roll back
// we will disable them probably at all from DDL when the driver does not
Expand All @@ -99,16 +99,16 @@ const execUnit = {
await execUnit.down(context, driver, file);
throw err;
}
await State.endMigration(context._driver, file, context.internals);
await State.endMigration(context._pdriver, file, context.internals);
log.verbose(`[fix] current schema`, util.inspect(context.internals.schema, false, null, true));
// end migration, same as start migration
},

down: async function (context, driver, file) {
// start migration, see up comments
await State.startMigration(context._driver, file, context.internals);
await TranslateState(context._driver, file, driver, context.internals);
await State.endMigration(context._driver, file, context.internals);
await State.startMigration(context._pdriver, file, context.internals);
await TranslateState(context._driver, file, driver, context.internals, context._pdriver);
await State.endMigration(context._pdriver, file, context.internals);
return Promise.promisify(context.deleteMigrationRecord.bind(context))(file);
// end migration, see up comments
}
Expand Down
11 changes: 6 additions & 5 deletions lib/methods/v2/statetravel.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ const DEFAULT = {
};

class StateTravel {
constructor (internals, file, driver) {
constructor (internals, file, driver, pdriver) {
this.file = file;
this.internals = internals;
this.driver = driver;
this.pdriver = pdriver;
this._counter = 0;
this._default = async function _default () {
await State.step(this.driver, ++this._counter, this.internals);
await State.step(this.pdriver, ++this._counter, this.internals);

return State.update(
this.driver,
this.pdriver,
this.file,
this.internals.modSchema,
this.internals
Expand All @@ -49,9 +50,9 @@ const noTravelError = prop => {
};

module.exports = {
getInterface: (context, file, driver, internals) => {
getInterface: (context, file, driver, internals, pdriver) => {
if (context.learnable) {
const st = new StateTravel(internals, file, context);
const st = new StateTravel(internals, file, context, pdriver);
return Shadow.overshadow(
driver,
Object.assign(st, context.statechanger),
Expand Down
11 changes: 6 additions & 5 deletions lib/methods/v2/translatestate.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async function processEntry (
file,
driver,
internals,
pdriver,
{ t: type, a: action, c: args }
) {
let skip = false;
Expand Down Expand Up @@ -101,20 +102,20 @@ async function processEntry (
}

internals.modSchema.s.shift();
await State.update(context, file, internals.modSchema, internals);
await State.update(pdriver, file, internals.modSchema, internals);
}

module.exports = async (context, file, driver, internals) => {
module.exports = async (context, file, driver, internals, pdriver) => {
const mod = internals.modSchema;

const chain = new Chain(context, file, driver, internals);
const chain = new Chain(context, file, driver, internals, pdriver);
internals.unlearn = true;

chain.addChain(Learn);
chain.addChain(Migrate);
await Promise.resolve(mod.s.reverse()).each(args =>
processEntry(context, file, chain, internals, args)
processEntry(context, file, chain, internals, pdriver, args)
);

await State.deleteState(context, file, internals);
await State.deleteState(pdriver, file, internals);
};
4 changes: 3 additions & 1 deletion lib/walker.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const INTERFACES = {
const Walker = function (driver, directory, mode, intern, prefix, opts = {}) {
this.driver = dbmUtil.reduceToInterface(driver, INTERFACES[prefix]);
this._driver = driver;
this._pdriver = opts.db2;
Promise.promisifyAll(this._driver);
Promise.promisifyAll(this._pdriver);
this.directory = directory;
this.internals = intern;
/**
Expand Down Expand Up @@ -60,7 +62,7 @@ const Walker = function (driver, directory, mode, intern, prefix, opts = {}) {

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

Expand Down

0 comments on commit 6acaf40

Please sign in to comment.