Skip to content

Commit e6007f9

Browse files
Merge pull request #28 from sdesalas/master
Fixes for restore `--overwrite` flag
2 parents f360127 + f9521e4 commit e6007f9

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

lib/dynamo-restore.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function DynamoRestore(options) {
3232
});
3333

3434
this.options = options;
35+
this.dynamodb = new AWS.DynamoDB();
3536
}
3637

3738
// Stick to prototypal inheritance. While this can be done differently
@@ -53,7 +54,7 @@ DynamoRestore.prototype.run = function(finishCallback) {
5354
}).bind(this));
5455
// Finish off by updating write capacity to end-state (if needed)
5556
this.on('finish', (function() {
56-
var dynamodb = new AWS.DynamoDB(),
57+
var dynamodb = this.dynamodb,
5758
options = this.options;
5859
// Do we need to update write capacity?
5960
if (options.writecapacity) {
@@ -89,22 +90,23 @@ DynamoRestore.prototype._validateS3Backup = function(options) {
8990
};
9091

9192
DynamoRestore.prototype._validateTable = function(options) {
92-
var dynamodb = new AWS.DynamoDB();
93+
var dynamodb = this.dynamodb;
9394
if (!options.table) {
9495
return this.emit('error', 'Please provide a Dynamo DB table name to restore to.');
9596
}
9697
dynamodb.listTables({}, this._checkTableExists.bind(this));
9798
};
9899

99100
DynamoRestore.prototype._checkTableExists = function(error, data) {
101+
var dynamodb = this.dynamodb;
100102
if (error || !data || !data.TableNames) {
101103
return this.emit('error', 'Fatal Error. Could not connect to AWS DynamoDB engine. Please check your credentials.');
102104
}
103105
if (data.TableNames.indexOf(this.options.table) > -1) {
104106
// Table exists, should we overwrite it??
105107
if (this.options.overwrite) {
106-
this.emit('warning', util.format('WARN: table [%s] will be overwritten.', options.table));
107-
dynamodb.describeTable({ TableName: this.options.table }, this._checkTableReady.bind(this));
108+
this.emit('warning', util.format('WARN: table [%s] will be overwritten.', this.options.table));
109+
setTimeout(dynamodb.describeTable.bind(dynamodb, { TableName: this.options.table }, this._checkTableReady.bind(this)), 1000);
108110
} else {
109111
this.emit('error', 'Fatal Error. The destination table already exists! Exiting process..');
110112
}
@@ -228,7 +230,7 @@ DynamoRestore.prototype._extractSchema = function(template) {
228230
};
229231

230232
DynamoRestore.prototype._createTable = function(callback) {
231-
var dynamodb = new AWS.DynamoDB(),
233+
var dynamodb = this.dynamodb,
232234
options = this.options;
233235
if (!options.table || !options.partitionkey) {
234236
return this.emit('error', 'Fatal Error. Could not create dynamo table. Not enough information provided.');
@@ -269,7 +271,7 @@ DynamoRestore.prototype._createTable = function(callback) {
269271
};
270272

271273
DynamoRestore.prototype._checkTableReady = function(error, data) {
272-
var dynamodb = new AWS.DynamoDB();
274+
var dynamodb = this.dynamodb;
273275
if (error || !data || !data.Table) {
274276
return this.emit('error', 'Error creating table ' + this.options.table);
275277
}
@@ -288,7 +290,7 @@ DynamoRestore.prototype._checkTableReady = function(error, data) {
288290
DynamoRestore.prototype._sendBatch = function() {
289291
// Prepare
290292
var params = { RequestItems: {} },
291-
dynamo = new AWS.DynamoDB(),
293+
dynamo = this.dynamodb,
292294
options = this.options;
293295
batch = this.batches.shift();
294296
params.RequestItems[options.table] = batch.items;
@@ -313,7 +315,7 @@ DynamoRestore.prototype._sendBatch = function() {
313315
var unprocessedItems = data && data.UnprocessedItems && data.UnprocessedItems[options.table] || [];
314316
if (unprocessedItems.length) {
315317
// Retry unprocessed items
316-
this.emit('warning', 'Retrying ' + unprocessedItems.length + ' unprocessed items');
318+
this.emit('warning', unprocessedItems.length + ' unprocessed items. Add to queue and back off a bit.');
317319
this.batches.push({
318320
items: unprocessedItems,
319321
attempts: batch.attempts + 1

0 commit comments

Comments
 (0)