Skip to content

Commit aa3ff89

Browse files
committed
create own methods dynamically on chain steps
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent aa13a35 commit aa3ff89

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

lib/driver/shadow.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ exports.infect = function (db, intern, ShadowProto) {
5757

5858
exports.overshadow = function (db, ShadowProto, ShadowError) {
5959
const newDb = Object.assign({}, ShadowProto);
60+
newDb.prototype = Object.assign({}, ShadowProto.prototype);
6061

6162
for (var prop in db) {
6263
if (

lib/learn.js

Lines changed: 77 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ const Shadow = require('./driver/shadow');
33

44
class STD {
55
constructor ({ schema, modSchema: mod }, driver) {
6+
this.checkColumn = function (t, c) {
7+
if (!this.schema[t]) {
8+
throw new Error(`There is no ${t} table in schema!`);
9+
}
10+
11+
if (!this.schema[t][c]) {
12+
throw new Error(`There is no ${c} column in schema!`);
13+
}
14+
};
615
this.validations = {};
716
this.driver = driver;
817
this.indizies = schema.i;
@@ -97,60 +106,82 @@ class STD {
97106

98107
removeColumn (t, c, o) {
99108
let alter = {};
100-
if (this.schema[t]) {
101-
if (this.schema[t][c].notNull === true) {
102-
if (this.validations.columnStrategies !== true) {
103-
if (
104-
this.driver._meta &&
105-
this.driver._meta.supports &&
106-
this.driver._meta.supports.optionParam === true
107-
) {
108-
/**
109-
* This is a validation only, no action will be taken unless throwing
110-
* errors.
111-
*
112-
* The driver needs to respect the options properly.
113-
*/
114-
switch (o.columnStrategy) {
115-
case 'defaultValue':
116-
break;
117-
case 'delay':
118-
break;
119-
default:
120-
if (!o.columnStrategy) {
121-
throw new Error(
122-
'Can not drop a notNull column without providing a' +
123-
' recreation strategy.'
124-
);
125-
}
109+
this.checkColumn(t, c);
110+
111+
if (this.schema[t][c].notNull === true) {
112+
if (this.validations.columnStrategies !== true) {
113+
if (
114+
this.driver._meta &&
115+
this.driver._meta.supports &&
116+
this.driver._meta.supports.optionParam === true
117+
) {
118+
/**
119+
* This is a validation only, no action will be taken unless throwing
120+
* errors.
121+
*
122+
* The driver needs to respect the options properly.
123+
*/
124+
switch (o.columnStrategy) {
125+
case 'defaultValue':
126+
break;
127+
case 'delay':
128+
break;
129+
default:
130+
if (!o.columnStrategy) {
126131
throw new Error(
127-
`There is no such column recreation strategy "${
128-
o.columnStrategy
129-
}!"`
132+
'Can not drop a notNull column without providing a' +
133+
' recreation strategy.'
130134
);
131-
}
132-
} else {
133-
throw new Error(
134-
'This driver does not support optionParameters which are' +
135-
' required to provide a recreation strategy.'
136-
);
137-
}
138-
139-
if (!this.driver._meta.supports.columnStrategies) {
140-
throw new Error(
141-
'This driver does not support column recreation strategies.'
142-
);
135+
}
136+
throw new Error(
137+
`There is no such column recreation strategy "${
138+
o.columnStrategy
139+
}!"`
140+
);
143141
}
142+
} else {
143+
throw new Error(
144+
'This driver does not support optionParameters which are' +
145+
' required to provide a recreation strategy.'
146+
);
147+
}
144148

145-
this.validations.columnStrategies = true;
149+
if (!this.driver._meta.supports.columnStrategies) {
150+
throw new Error(
151+
'This driver does not support column recreation strategies.'
152+
);
146153
}
154+
155+
this.validations.columnStrategies = true;
147156
}
148-
this.modS[t] = {};
149-
this.modS[t][c] = this.schema[t][c];
150-
delete this.schema[t][c];
151157
}
152158

153-
this.modC.push({ t: 1, a: 'addColumn', c: [t, c] });
159+
this.modS[t] = {};
160+
161+
switch (o.columnStrategies) {
162+
case 'delay':
163+
this.modS[t][c] = this.schema[t][c];
164+
165+
o.passthrough = o.passthrough || {};
166+
o.passthrough.column =
167+
o.passthrough.column || `__dbmrn_${c}_${new Date().toString()}__`;
168+
169+
this.modC.push({
170+
t: 1,
171+
a: 'renameColumn',
172+
c: [t, o.passthrough.column, c]
173+
});
174+
175+
break;
176+
case 'defaultValue':
177+
this.modS[t][c] = this.schema[t][c];
178+
this.modS[t][c].defaultValue = o.passthrough.defaultValue;
179+
180+
this.modC.push({ t: 1, a: 'addColumn', c: [t, c, o] });
181+
break;
182+
}
183+
184+
delete this.schema[t][c];
154185

155186
return Promise.resolve(alter);
156187
}
@@ -178,16 +209,6 @@ class STD {
178209
return Promise.resolve();
179210
}
180211

181-
checkColumn (t, c) {
182-
if (!this.schema[t]) {
183-
throw new Error(`There is no ${t} table in schema!`);
184-
}
185-
186-
if (!this.schema[t][c]) {
187-
throw new Error(`There is no ${c} column in schema!`);
188-
}
189-
}
190-
191212
changeColumn (t, c, s) {
192213
this.checkColumn(t, c);
193214

0 commit comments

Comments
 (0)