diff --git a/lib/migrate/migrator.js b/lib/migrate/migrator.js index 89c8efd..e92d2e4 100644 --- a/lib/migrate/migrator.js +++ b/lib/migrate/migrator.js @@ -107,20 +107,28 @@ function MigrateRunner(config){ this.migrator = create(npd); } -MigrateRunner.prototype._createMigrateTableIfNotExits = function(){ +MigrateRunner.prototype._createMigrateTableIfNotExists = function() { var self = this; var tableName = this.config.migrations.tableName; - return this.npd().showTables() - .then(function(tables){ - var isFound = _.find(tables.TableNames, function(t){ return t === tableName }); - if(isFound) { return; } - - return self.migrator().createTable(tableName, function(t){ - t.number('version').hashKey(); - t.provisionedThroughput.apply(t, self.config.migrations.ProvisionedThroughput); - }) - .then(function(){ - return self.npd().rawClient().waitFor('tableExists', {TableName: tableName}); + return new Promise(function(resolve, reject) { + self.npd().table(tableName).describe().then(function(_) { + resolve(); + }).catch(function (err) { + if (!err) { + reject(); + } else if (err.code === 'ResourceInUseException') { + resolve(); + } else if (err.code === 'ResourceNotFoundException') { + self.migrator().createTable(tableName, function(t) { + t.number('version').hashKey(); + t.provisionedThroughput.apply(t, self.config.migrations.ProvisionedThroughput); + }) + .then(function() { + self.npd().rawClient().waitFor('tableExists', {TableName: tableName}).then(resolve).catch(reject); + }).catch(reject); + } else { + reject(); + } }); }); }; @@ -129,7 +137,7 @@ MigrateRunner.prototype.run = function(){ var self = this; var tableName = this.config.migrations.tableName; - return this._createMigrateTableIfNotExits().then(function(){ + return this._createMigrateTableIfNotExists().then(function(){ return self.npd().table(tableName).all().then(function(data){ var dirs = fs.readdirSync(self.config.cwd); @@ -165,6 +173,8 @@ MigrateRunner.prototype.run = function(){ return utils.PromiseWaterfall(tasks); }); + }).catch(function (err) { + console.error(err); }); };