Skip to content

Commit

Permalink
feat(schemav2): add foreignKey support on tables
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 15, 2019
1 parent d891628 commit 3dd7158
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
14 changes: 8 additions & 6 deletions lib/learn.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ class STD {
if (key.foreignKey) {
if (!this.foreign[t]) this.foreign[t] = {};

this.foreign[t][s.foreignKey.name] = { t, rt: key.foreignKey.table };
this.foreign[t][key.foreignKey.name] = { t, rt: key.foreignKey.table };
if (key.foreignKey.rules) {
this.foreign[t][s.foreignKey.name].r = s.foreignKey.rules;
this.foreign[t][key.foreignKey.name].r = key.foreignKey.rules;
}

let mapping = {};

if (typeof s.foreignKey.mapping === 'string') {
mapping[k] = s.foreignKey.mapping;
if (typeof key.foreignKey.mapping === 'string') {
mapping[k] = key.foreignKey.mapping;
} else {
mapping = Object.assign({}, s.foreignKey.mapping);
mapping = Object.assign({}, key.foreignKey.mapping);
}

this.foreign[t][s.foreignKey.name].m = mapping;
this.foreign[t][key.foreignKey.name].m = mapping;
}
});

Expand Down Expand Up @@ -196,6 +196,8 @@ class STD {
this.modI[t][i] = this.indizies[t][i];
delete this.indizies[t][i];

this.modC.push({ t: 1, a: 'addIndex', c: [t, i] });

return Promise.resolve(alter);
}

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 @@ -74,7 +74,7 @@ module.exports = {
['addForeignKey'].forEach(m => {
_driver.host[m] = _driver[m];
_driver[m] = function (...args) {
return Promise.resolve();
return _driver.host[m].apply(_driver, args);
};
});

Expand Down
53 changes: 49 additions & 4 deletions lib/methods/v2/translatestate.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
const Promise = require('bluebird');
const State = require('../../state');
const Chain = require('../../chain');
const Migrate = require('./migrate');
const Learn = require('../../learn');

const f = {
const methods = {
createTable: async (driver, [t], internals) => {
const mod = internals.modSchema;
const schema = mod.c[t];
// console.log(t, internals.modSchema, internals.modSchema.c[t]);
return driver.createTable(t, internals.modSchema.c[t]);
await driver.createTable(t, schema);

console.log(mod.f[t]);
Object.keys(schema).forEach(key => {
if (schema[key].foreignKey) {
delete mod.f[t][schema[key].foreignKey.name];
}
});

if (mod.i[t]) {
await Promise.resolve(Object.keys(mod.i[t])).each(i => {
const index = mod.i[t][i];
return driver.addIndex(t, i, index.c, index.u);
});
}

if (mod.f[t]) {
await Promise.resolve(Object.keys(mod.f[t])).each(f => {
const foreign = mod.f[t][f];
return driver.addForeignKey(
t,
foreign.rt,
foreign.k,
foreign.m,
foreign.r
);
});
}
},

addColumn: async (driver, [t, c], internals) => {
return driver.addColumn(t, c, internals.modSchema.c[t][c]);
},

changeColumn: async (driver, [t, c], internals) => {
return driver.changeColumn(t, c, internals.modSchema.c[t][c]);
}
};

Expand All @@ -16,6 +56,7 @@ async function processEntry (
{ t: type, a: action, c: args }
) {
// console.log('hello', type, action, args);
const f = Object.assign(methods, context.reverse);
switch (type) {
case 0:
await driver[action].apply(driver, args);
Expand All @@ -31,11 +72,15 @@ async function processEntry (
internals.modSchema.s.shift();
await State.update(context, file, internals.modSchema, internals);
}

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

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

await State.deleteState(context, file, internals);
Expand Down
12 changes: 11 additions & 1 deletion lib/state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const MSTATE = '__dbmigrate_state__';
const SSTATE = '__dbmigrate_schema__';
const log = require('db-migrate-shared').log;

module.exports = {
Expand Down Expand Up @@ -30,6 +31,13 @@ module.exports = {

init: async function (driver, internals) {
await driver._createKV(internals.migrationState);
let schema = await driver._getKV(internals.migrationState, SSTATE);
if (schema) {
internals.schema = JSON.parse(schema.value);
} else {
await driver._insertKV(internals.migrationState, SSTATE, '{}');
}

if (!internals.dryRun) {
let state = await driver._getKV(internals.migrationState, MSTATE);
return module.exports.lockState(driver, state, internals);
Expand Down Expand Up @@ -63,13 +71,15 @@ module.exports = {
}
},

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

await driver._updateKV(internals.migrationState, SSTATE, internals.schema);

return driver._updateKV(
internals.migrationState,
file.name,
Expand Down

0 comments on commit 3dd7158

Please sign in to comment.