Skip to content

Commit aa13a35

Browse files
committed
feat(error handling): added advanced error handling and rollback
Signed-off-by: Tobias Gurtzick <magic@wizardtales.com>
1 parent 7d7f823 commit aa13a35

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

lib/executors/versioned/v2.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const Promise = require('bluebird');
44
const State = require('../../state');
5+
const log = require('db-migrate-shared').log;
56
const Learn = require('../../learn');
67
const Chain = require('../../chain');
78
const StateTravel = require('../../methods/v2/statetravel');
@@ -49,11 +50,19 @@ const execUnit = {
4950
chain.addChain(Migrate);
5051

5152
await State.startMigration(context._driver, file, context.internals);
52-
await _file.migrate(chain, {
53-
options: context.internals.safeOptions,
54-
seedLink: context.seedLink,
55-
dbm: context.internals.safeOptions.dbmigrate
56-
});
53+
try {
54+
await _file.migrate(chain, {
55+
options: context.internals.safeOptions,
56+
seedLink: context.seedLink,
57+
dbm: context.internals.safeOptions.dbmigrate
58+
});
59+
} catch (err) {
60+
log.error(
61+
'An error occured. No alternative failure strategy defined. Rolling back!'
62+
);
63+
await execUnit.down(context, driver, file);
64+
throw err;
65+
}
5766
return Promise.promisify(context.writeMigrationRecord.bind(context))(file);
5867
return execUnit.learn(context, driver, file);
5968

lib/learn.js

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

44
class STD {
55
constructor ({ schema, modSchema: mod }, driver) {
6+
this.validations = {};
67
this.driver = driver;
78
this.indizies = schema.i;
89
this.schema = schema.c;
@@ -98,16 +99,50 @@ class STD {
9899
let alter = {};
99100
if (this.schema[t]) {
100101
if (this.schema[t][c].notNull === true) {
101-
if (this.driver._meta.supports.optionParam === true) {
102-
throw new Error(
103-
'Can not drop a notNull column without providing a' +
104-
' recreation strategy.'
105-
);
106-
} else {
107-
throw new Error(
108-
'This driver does not support optionParameters which are' +
109-
' required to provide a recreation strategy.'
110-
);
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+
}
126+
throw new Error(
127+
`There is no such column recreation strategy "${
128+
o.columnStrategy
129+
}!"`
130+
);
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+
);
143+
}
144+
145+
this.validations.columnStrategies = true;
111146
}
112147
}
113148
this.modS[t] = {};

0 commit comments

Comments
 (0)