Skip to content

Commit

Permalink
feat(error handling): added advanced error handling and rollback
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 16, 2019
1 parent 7d7f823 commit aa13a35
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
19 changes: 14 additions & 5 deletions lib/executors/versioned/v2.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const Promise = require('bluebird');
const State = require('../../state');
const log = require('db-migrate-shared').log;
const Learn = require('../../learn');
const Chain = require('../../chain');
const StateTravel = require('../../methods/v2/statetravel');
Expand Down Expand Up @@ -49,11 +50,19 @@ const execUnit = {
chain.addChain(Migrate);

await State.startMigration(context._driver, file, context.internals);
await _file.migrate(chain, {
options: context.internals.safeOptions,
seedLink: context.seedLink,
dbm: context.internals.safeOptions.dbmigrate
});
try {
await _file.migrate(chain, {
options: context.internals.safeOptions,
seedLink: context.seedLink,
dbm: context.internals.safeOptions.dbmigrate
});
} catch (err) {
log.error(
'An error occured. No alternative failure strategy defined. Rolling back!'
);
await execUnit.down(context, driver, file);
throw err;
}
return Promise.promisify(context.writeMigrationRecord.bind(context))(file);
return execUnit.learn(context, driver, file);

Expand Down
55 changes: 45 additions & 10 deletions lib/learn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Shadow = require('./driver/shadow');

class STD {
constructor ({ schema, modSchema: mod }, driver) {
this.validations = {};
this.driver = driver;
this.indizies = schema.i;
this.schema = schema.c;
Expand Down Expand Up @@ -98,16 +99,50 @@ class STD {
let alter = {};
if (this.schema[t]) {
if (this.schema[t][c].notNull === true) {
if (this.driver._meta.supports.optionParam === true) {
throw new Error(
'Can not drop a notNull column without providing a' +
' recreation strategy.'
);
} else {
throw new Error(
'This driver does not support optionParameters which are' +
' required to provide a recreation strategy.'
);
if (this.validations.columnStrategies !== true) {
if (
this.driver._meta &&
this.driver._meta.supports &&
this.driver._meta.supports.optionParam === true
) {
/**
* This is a validation only, no action will be taken unless throwing
* errors.
*
* The driver needs to respect the options properly.
*/
switch (o.columnStrategy) {
case 'defaultValue':
break;
case 'delay':
break;
default:
if (!o.columnStrategy) {
throw new Error(
'Can not drop a notNull column without providing a' +
' recreation strategy.'
);
}
throw new Error(
`There is no such column recreation strategy "${
o.columnStrategy
}!"`
);
}
} else {
throw new Error(
'This driver does not support optionParameters which are' +
' required to provide a recreation strategy.'
);
}

if (!this.driver._meta.supports.columnStrategies) {
throw new Error(
'This driver does not support column recreation strategies.'
);
}

this.validations.columnStrategies = true;
}
}
this.modS[t] = {};
Expand Down

0 comments on commit aa13a35

Please sign in to comment.