Skip to content

Commit d891628

Browse files
committed
feat(migration): add support for first basic options on new schema
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent 6dc4d3b commit d891628

File tree

7 files changed

+125
-24
lines changed

7 files changed

+125
-24
lines changed

lib/chain.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class Chain {
7474
let ret;
7575
let stat = [];
7676
while (this.step()) {
77-
ret = this._interface[m].apply(this._interface, args);
77+
ret = await this._interface[m].apply(this._interface, args);
7878
stat.push(ret);
7979

8080
if (this._step.hasStateUsage) {

lib/executors/versioned/v2.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

33
const Promise = require('bluebird');
4+
const State = require('../../state');
45
const Learn = require('../../learn');
56
const Chain = require('../../chain');
6-
const TravelState = require('../../methods/v2/statetravel');
7+
const StateTravel = require('../../methods/v2/statetravel');
78
const Migrate = require('../../methods/v2/migrate');
9+
const TranslateState = require('../../methods/v2/translatestate');
810

911
const execUnit = {
1012
_extend: (context, type) => {
@@ -39,18 +41,20 @@ const execUnit = {
3941
});
4042
},
4143

42-
up: function (context, driver, file) {
44+
up: async function (context, driver, file) {
4345
const _file = file.get();
4446
const chain = new Chain(context._driver, file, driver, context.internals);
4547
chain.addChain(Learn);
46-
chain.addChain(TravelState);
48+
chain.addChain(StateTravel);
4749
chain.addChain(Migrate);
4850

49-
return _file.migrate(chain, {
51+
await State.startMigration(context._driver, file, context.internals);
52+
await _file.migrate(chain, {
5053
options: context.internals.safeOptions,
5154
seedLink: context.seedLink,
5255
dbm: context.internals.safeOptions.dbmigrate
5356
});
57+
return Promise.promisify(context.writeMigrationRecord.bind(context))(file);
5458
return execUnit.learn(context, driver, file);
5559

5660
return context.driver
@@ -72,7 +76,19 @@ const execUnit = {
7276
.then(context.driver.endMigration.bind(context.driver));
7377
},
7478

75-
down: function (context, driver, file) {
79+
down: async function (context, driver, file) {
80+
await State.startMigration(context._driver, file, context.internals);
81+
await TranslateState(context._driver, file, driver, context.internals);
82+
await State.endMigration(context._driver, file, context.internals);
83+
return Promise.promisify(context.deleteMigrationRecord.bind(context))(file);
84+
85+
await _file.migrate(chain, {
86+
options: context.internals.safeOptions,
87+
seedLink: context.seedLink,
88+
dbm: context.internals.safeOptions.dbmigrate
89+
});
90+
return Promise.promisify(context.deleteMigrationRecord.bind(context))(file);
91+
7692
return driver
7793
.startMigration()
7894
.then(() => {

lib/learn.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class STD {
99
this.modS = mod.c;
1010
this.modI = mod.i;
1111
this.modF = mod.f;
12+
this.modC = mod.s;
1213
}
1314

1415
dropTable (t) {
@@ -33,6 +34,8 @@ class STD {
3334
delete this.indizies[t];
3435
}
3536

37+
this.modC.push({ t: 1, a: 'createTable', c: [t] });
38+
3639
return Promise.resolve(alter);
3740
}
3841

@@ -62,6 +65,8 @@ class STD {
6265
}
6366
});
6467

68+
this.modC.push({ t: 0, a: 'dropTable', c: [t] });
69+
6570
return Promise.resolve();
6671
}
6772

@@ -71,6 +76,8 @@ class STD {
7176
delete this.schema[t];
7277
}
7378

79+
this.modC.push({ t: 0, a: 'renameTable', c: [n, t] });
80+
7481
return Promise.resolve();
7582
}
7683

@@ -86,17 +93,16 @@ class STD {
8693
return this.createTable.apply(this, args);
8794
}
8895

89-
removeColumn (tableName, columnName, columnSpec) {
96+
removeColumn (t, c) {
9097
let alter = {};
91-
alter = { c: {}, i: {}, f: {} };
92-
if (this.schema[tableName]) {
93-
alter.c[tableName] = {};
94-
alter.c[tableName][columnName] = this.schema[tableName][columnName];
95-
this.modS[tableName] = {};
96-
this.modS[tableName][columnName] = this.schema[tableName][columnName];
97-
delete this.schema[tableName][columnName];
98+
if (this.schema[t]) {
99+
this.modS[t] = {};
100+
this.modS[t][c] = this.schema[t][c];
101+
delete this.schema[t][c];
98102
}
99103

104+
this.modC.push({ t: 1, a: 'addColumn', c: [t, c] });
105+
100106
return Promise.resolve(alter);
101107
}
102108

@@ -106,6 +112,8 @@ class STD {
106112
delete this.schema[t][o];
107113
}
108114

115+
this.modC.push({ t: 0, a: 'renameColumn', c: [t, n, o] });
116+
109117
return Promise.resolve();
110118
}
111119

@@ -116,6 +124,8 @@ class STD {
116124
this.schema[t] = this.schema[t] || {};
117125
this.schema[t][c] = s;
118126

127+
this.modC.push({ t: 0, a: 'removeColumn', c: [t, c] });
128+
119129
return Promise.resolve();
120130
}
121131

@@ -132,8 +142,11 @@ class STD {
132142
changeColumn (t, c, s) {
133143
this.checkColumn(t, c);
134144

145+
this.modS[t][c] = this.schema[t][c];
135146
this.schema[t][c] = Object.assign(this.schema[t][c], s);
136147

148+
this.modC.push({ t: 1, a: 'changeColumn', c: [t, c] });
149+
137150
return Promise.resolve();
138151
}
139152

@@ -153,6 +166,8 @@ class STD {
153166
if (!this.indizies[t]) this.indizies[t] = {};
154167
this.indizies[t][i] = index;
155168

169+
this.modC.push({ t: 0, a: 'removeIndex', c: [t, i] });
170+
156171
return Promise.resolve();
157172
}
158173

@@ -201,6 +216,8 @@ class STD {
201216
this.foreign[t][k].r = r;
202217
}
203218

219+
this.modC.push({ t: 0, a: 'removeForeignKey', c: [t, k] });
220+
204221
return Promise.resolve();
205222
}
206223

lib/methods/v2/statetravel.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ class StateTravel {
3030
Object.keys(DEFAULT).forEach(m => {
3131
StateTravel.prototype[m] = async function (...args) {
3232
await State.step(this.driver, ++this._counter, this.internals);
33-
return State.update(
33+
const res = await State.update(
3434
this.driver,
3535
this.file,
3636
this.internals.modSchema,
3737
this.internals
3838
);
39+
40+
console.log('result', res);
41+
return res;
3942
};
4043
});
4144

lib/methods/v2/translatestate.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const Promise = require('bluebird');
2+
const State = require('../../state');
3+
4+
const f = {
5+
createTable: async (driver, [t], internals) => {
6+
// console.log(t, internals.modSchema, internals.modSchema.c[t]);
7+
return driver.createTable(t, internals.modSchema.c[t]);
8+
}
9+
};
10+
11+
async function processEntry (
12+
context,
13+
file,
14+
driver,
15+
internals,
16+
{ t: type, a: action, c: args }
17+
) {
18+
// console.log('hello', type, action, args);
19+
switch (type) {
20+
case 0:
21+
await driver[action].apply(driver, args);
22+
break;
23+
case 1:
24+
await f[action](driver, args, internals);
25+
break;
26+
27+
default:
28+
throw new Error(`Invalid state record, of type ${type}`);
29+
}
30+
31+
internals.modSchema.s.shift();
32+
await State.update(context, file, internals.modSchema, internals);
33+
}
34+
module.exports = async (context, file, driver, internals) => {
35+
const mod = internals.modSchema;
36+
37+
await Promise.resolve(mod.s).each(args =>
38+
processEntry(context, file, driver, internals, args)
39+
);
40+
41+
await State.deleteState(context, file, internals);
42+
};

lib/state.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,45 @@ module.exports = {
4040

4141
startMigration: async function (driver, file, internals) {
4242
let state = await driver._getKV(internals.migrationState, MSTATE);
43+
let mig = await driver._getKV(internals.migrationState, file.name);
44+
45+
if (mig) {
46+
internals.modSchema = JSON.parse(mig.value);
47+
}
4348

4449
if (internals.dryRun) {
4550
return Promise.resolve();
4651
}
4752

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

50-
return driver._insertKV(
51-
internals.migrationState,
52-
file.name,
53-
JSON.stringify({})
54-
);
55+
if (!mig) {
56+
return driver._insertKV(
57+
internals.migrationState,
58+
file.name,
59+
JSON.stringify({})
60+
);
61+
} else {
62+
return Promise.resolve();
63+
}
5564
},
5665

5766
update: function (driver, file, state, internals) {
58-
console.log('called');
67+
log.verbose(`[state] update state`);
5968
if (internals.dryRun) {
60-
log.info(`[state] update state`);
6169
return Promise.resolve();
6270
}
71+
console.log('update', state);
72+
73+
return driver._updateKV(
74+
internals.migrationState,
75+
file.name,
76+
JSON.stringify(state)
77+
);
78+
},
6379

64-
return driver._updateKV(internals.migrationState, file.name, state);
80+
get: function (driver, file, internals) {
81+
return driver._getKV(internals.migrationState, file.name);
6582
},
6683

6784
tick: async function (driver, internals) {
@@ -102,6 +119,10 @@ module.exports = {
102119
);
103120
},
104121

122+
deleteState: function (driver, file, internals) {
123+
return driver._deleteKV(internals.migrationState, file.name);
124+
},
125+
105126
endMigration: async function (driver, file, internals) {
106127
if (internals.dryRun) {
107128
return Promise.resolve();

lib/walker.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const Walker = function (driver, directory, mode, intern, prefix) {
3131
this.directory = directory;
3232
this.internals = intern;
3333
this.internals.schema = { i: {}, c: {}, f: {} };
34-
this.internals.modSchema = { i: {}, c: {}, f: {} };
34+
this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
3535
this.mode = mode;
3636

3737
if (!this.mode) this.prefix = `static-${prefix}`;
@@ -173,6 +173,7 @@ Walker.prototype = {
173173
log.verbose(this.title + 'preparing to run up:', file.name);
174174
const _meta = file.get()._meta || {};
175175
const version = _meta.version || 1;
176+
this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
176177
return require(`./executors/versioned/v${version}`).up(
177178
this,
178179
this.driver,
@@ -216,6 +217,7 @@ Walker.prototype = {
216217
log.verbose(this.title + 'preparing to run down:', file.name);
217218
const _meta = file.get()._meta || {};
218219
const version = _meta.version || 1;
220+
this.internals.modSchema = { i: {}, c: {}, f: {}, s: [] };
219221
return require(`./executors/versioned/v${version}`).down(
220222
this,
221223
this.driver,

0 commit comments

Comments
 (0)