Skip to content

Commit 3dd7158

Browse files
committed
feat(schemav2): add foreignKey support on tables
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent d891628 commit 3dd7158

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

lib/learn.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ class STD {
4848
if (key.foreignKey) {
4949
if (!this.foreign[t]) this.foreign[t] = {};
5050

51-
this.foreign[t][s.foreignKey.name] = { t, rt: key.foreignKey.table };
51+
this.foreign[t][key.foreignKey.name] = { t, rt: key.foreignKey.table };
5252
if (key.foreignKey.rules) {
53-
this.foreign[t][s.foreignKey.name].r = s.foreignKey.rules;
53+
this.foreign[t][key.foreignKey.name].r = key.foreignKey.rules;
5454
}
5555

5656
let mapping = {};
5757

58-
if (typeof s.foreignKey.mapping === 'string') {
59-
mapping[k] = s.foreignKey.mapping;
58+
if (typeof key.foreignKey.mapping === 'string') {
59+
mapping[k] = key.foreignKey.mapping;
6060
} else {
61-
mapping = Object.assign({}, s.foreignKey.mapping);
61+
mapping = Object.assign({}, key.foreignKey.mapping);
6262
}
6363

64-
this.foreign[t][s.foreignKey.name].m = mapping;
64+
this.foreign[t][key.foreignKey.name].m = mapping;
6565
}
6666
});
6767

@@ -196,6 +196,8 @@ class STD {
196196
this.modI[t][i] = this.indizies[t][i];
197197
delete this.indizies[t][i];
198198

199+
this.modC.push({ t: 1, a: 'addIndex', c: [t, i] });
200+
199201
return Promise.resolve(alter);
200202
}
201203

lib/methods/v2/statetravel.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ module.exports = {
7474
['addForeignKey'].forEach(m => {
7575
_driver.host[m] = _driver[m];
7676
_driver[m] = function (...args) {
77-
return Promise.resolve();
77+
return _driver.host[m].apply(_driver, args);
7878
};
7979
});
8080

lib/methods/v2/translatestate.js

+49-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,50 @@
11
const Promise = require('bluebird');
22
const State = require('../../state');
3+
const Chain = require('../../chain');
4+
const Migrate = require('./migrate');
5+
const Learn = require('../../learn');
36

4-
const f = {
7+
const methods = {
58
createTable: async (driver, [t], internals) => {
9+
const mod = internals.modSchema;
10+
const schema = mod.c[t];
611
// console.log(t, internals.modSchema, internals.modSchema.c[t]);
7-
return driver.createTable(t, internals.modSchema.c[t]);
12+
await driver.createTable(t, schema);
13+
14+
console.log(mod.f[t]);
15+
Object.keys(schema).forEach(key => {
16+
if (schema[key].foreignKey) {
17+
delete mod.f[t][schema[key].foreignKey.name];
18+
}
19+
});
20+
21+
if (mod.i[t]) {
22+
await Promise.resolve(Object.keys(mod.i[t])).each(i => {
23+
const index = mod.i[t][i];
24+
return driver.addIndex(t, i, index.c, index.u);
25+
});
26+
}
27+
28+
if (mod.f[t]) {
29+
await Promise.resolve(Object.keys(mod.f[t])).each(f => {
30+
const foreign = mod.f[t][f];
31+
return driver.addForeignKey(
32+
t,
33+
foreign.rt,
34+
foreign.k,
35+
foreign.m,
36+
foreign.r
37+
);
38+
});
39+
}
40+
},
41+
42+
addColumn: async (driver, [t, c], internals) => {
43+
return driver.addColumn(t, c, internals.modSchema.c[t][c]);
44+
},
45+
46+
changeColumn: async (driver, [t, c], internals) => {
47+
return driver.changeColumn(t, c, internals.modSchema.c[t][c]);
848
}
949
};
1050

@@ -16,6 +56,7 @@ async function processEntry (
1656
{ t: type, a: action, c: args }
1757
) {
1858
// console.log('hello', type, action, args);
59+
const f = Object.assign(methods, context.reverse);
1960
switch (type) {
2061
case 0:
2162
await driver[action].apply(driver, args);
@@ -31,11 +72,15 @@ async function processEntry (
3172
internals.modSchema.s.shift();
3273
await State.update(context, file, internals.modSchema, internals);
3374
}
75+
3476
module.exports = async (context, file, driver, internals) => {
3577
const mod = internals.modSchema;
3678

37-
await Promise.resolve(mod.s).each(args =>
38-
processEntry(context, file, driver, internals, args)
79+
const chain = new Chain(context, file, driver, internals);
80+
chain.addChain(Learn);
81+
chain.addChain(Migrate);
82+
await Promise.resolve(mod.s.reverse()).each(args =>
83+
processEntry(context, file, chain, internals, args)
3984
);
4085

4186
await State.deleteState(context, file, internals);

lib/state.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const MSTATE = '__dbmigrate_state__';
2+
const SSTATE = '__dbmigrate_schema__';
23
const log = require('db-migrate-shared').log;
34

45
module.exports = {
@@ -30,6 +31,13 @@ module.exports = {
3031

3132
init: async function (driver, internals) {
3233
await driver._createKV(internals.migrationState);
34+
let schema = await driver._getKV(internals.migrationState, SSTATE);
35+
if (schema) {
36+
internals.schema = JSON.parse(schema.value);
37+
} else {
38+
await driver._insertKV(internals.migrationState, SSTATE, '{}');
39+
}
40+
3341
if (!internals.dryRun) {
3442
let state = await driver._getKV(internals.migrationState, MSTATE);
3543
return module.exports.lockState(driver, state, internals);
@@ -63,13 +71,15 @@ module.exports = {
6371
}
6472
},
6573

66-
update: function (driver, file, state, internals) {
74+
update: async function (driver, file, state, internals) {
6775
log.verbose(`[state] update state`);
6876
if (internals.dryRun) {
6977
return Promise.resolve();
7078
}
7179
console.log('update', state);
7280

81+
await driver._updateKV(internals.migrationState, SSTATE, internals.schema);
82+
7383
return driver._updateKV(
7484
internals.migrationState,
7585
file.name,

0 commit comments

Comments
 (0)