Skip to content
This repository was archived by the owner on Jul 7, 2022. It is now read-only.

Commit 2c4cce3

Browse files
committed
make total a not-global
1 parent 491d33d commit 2c4cce3

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

copyCollection.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
var options = {
2828
to: 'target',
29-
collection: 'source',
30-
batchInserts: true
29+
collection: 'items',
30+
batchInserts: false
3131
};
3232

3333
CopyCollection = new Batch(options, false);
@@ -69,14 +69,16 @@ CopyCollection.start = function() {
6969
return false;
7070
}
7171

72-
this.out('Dropping ' + this.options.to + ' collection', 1);
73-
try {
74-
db[this.options.to].drop();
75-
} catch (err) {
76-
out(err.message, 1);
77-
}
72+
if (this.total) {
73+
this.out('Dropping ' + this.options.to + ' collection', 1);
74+
try {
75+
db[this.options.to].drop();
76+
} catch (err) {
77+
out(err.message, 1);
78+
}
7879

79-
this.stack = [];
80+
this.stack = [];
81+
}
8082

8183
return true;
8284
};

template.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,21 @@ Batch = function(options, run) {
7373
this.out('Starting ' + this.name + ' batch..', 1);
7474

7575
try {
76-
total = db[this.options.collection].count(this.options.conditions);
76+
this.total = db[this.options.collection].count(this.options.conditions);
7777
} catch (err) {
7878
this.out(err.message, 1);
7979
return false;
8080
}
8181

82-
this.out('Found ' + total + ' rows in ' + this.options.collection + ' to process', 1);
82+
this.out('Found ' + this.total + ' rows in ' + this.options.collection + ' to process', 1);
8383

84-
if (!total) {
84+
if (!this.total) {
8585
return true;
8686
}
8787

8888
if (!this.options.step) {
89-
if (total > 10000) {
90-
this.options.step = Math.pow(10, total.toString().length - 3);
89+
if (this.total > 10000) {
90+
this.options.step = Math.pow(10, this.total.toString().length - 3);
9191
} else {
9292
this.options.step = 100;
9393
}
@@ -105,7 +105,7 @@ Batch = function(options, run) {
105105
* @return void.
106106
*/
107107
this.finish = function() {
108-
this.out('Found ' + total + ' rows in ' + this.options.collection + ' to process', 3);
108+
this.out('Found ' + this.total + ' rows in ' + this.options.collection + ' to process', 3);
109109
this.out('All finished', 1);
110110
}
111111

@@ -185,7 +185,7 @@ Batch = function(options, run) {
185185
* @return void.
186186
*/
187187
this.processCursors = function() {
188-
while (this.processed < total || total === true) {
188+
while (this.processed < this.total || this.total === true) {
189189
if (!this.processCursor()) {
190190
this.out('Last slice failed - aborting further processing in processCursors', 4);
191191
return;
@@ -205,7 +205,7 @@ Batch = function(options, run) {
205205
this.run = function() {
206206
this.startTime = new Date().getTime();
207207
this.processed = 0;
208-
this.total = 0;
208+
this.total = this.options.total || 0;
209209
this.currentRow = null;
210210

211211
if (!this.start()) {

0 commit comments

Comments
 (0)