Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handling migration errors and saving the correct pos at each step. #16

Closed
wants to merge 2 commits into from

2 participants

Deniz Piri João Sousa Andrade
Deniz Piri

Handling possible migration errors and saving the correct migration position so that after the migration file is fixed, migration can be resumed.

Deniz Piri DenizPiri Handling possible migration errors and saving the correct migration p…
…osition so that after the migration file is fixed, migration can be resumed.
5bbe44c
Deniz Piri DenizPiri referenced this pull request
Open

handle errors #1

João Sousa Andrade
Collaborator

Closed in favor of #32

João Sousa Andrade joaosa closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 14, 2012
  1. Deniz Piri

    Handling possible migration errors and saving the correct migration p…

    DenizPiri authored
    …osition so that after the migration file is fixed, migration can be resumed.
Commits on Oct 15, 2012
  1. Deniz Piri
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 19 deletions.
  1. +7 −6 bin/migrate
  2. +24 −13 lib/set.js
13 bin/migrate
View
@@ -219,16 +219,17 @@ function performMigration(direction, migrationName) {
log(direction, migration.title);
});
- set.on('save', function(){
- log('migration', 'complete');
- process.exit();
- });
-
var migrationPath = migrationName
? join('migrations', migrationName)
: migrationName;
- set[direction](null, migrationPath);
+ set[direction](function(err){
+ if (err)
+ log('migration', 'error - ' + err);
+ else
+ log('migration', 'complete');
+ process.exit();
+ }, migrationPath);
}
// invoke command
37 lib/set.js
View
@@ -49,7 +49,6 @@ Set.prototype.save = function(fn){
var self = this
, json = JSON.stringify(this);
fs.writeFile(this.path, json, function(err){
- self.emit('save');
fn && fn(err);
});
};
@@ -151,30 +150,42 @@ Set.prototype._migrate = function(direction, fn, migrationName){
switch (direction) {
case 'up':
migrations = this.migrations.slice(this.pos, migrationPos+1);
- this.pos += migrations.length;
break;
case 'down':
migrations = this.migrations.slice(migrationPos, this.pos).reverse();
- this.pos -= migrations.length;
break;
}
- function next(err, migration) {
- // error from previous migration
- if (err) return fn(err);
-
+ function next(migration) {
// done
if (!migration) {
- self.emit('complete');
- self.save(fn);
+ fn && fn();
return;
}
self.emit('migration', migration, direction);
- migration[direction](function(err){
- next(err, migrations.shift());
- });
+ function error_handler(err) {
+ if (!err)
+ self.pos += (direction == 'up') ? 1 : -1;
+
+ if (err)
+ fn && fn(err); // handle the error
+ else {
+ self.save(function(err){
+ if (err)
+ fn && fn(err);
+ else
+ next(migrations.shift());
+ });
+ }
+ }
+ try {
+ migration[direction](error_handler);
+ }
+ catch (exc) {
+ error_handler(exc);
+ }
}
- next(null, migrations.shift());
+ next(migrations.shift());
};
Something went wrong with that request. Please try again.