Skip to content

Commit 7bb858e

Browse files
authored
feat(db-exists): make sure db still exists (#33)
* Added check to make sure database still exists before replication * Removed some changes left over from testing * Removed code left over from testing * 100% code coverage and test * changed package.json back to assert-beautified on test run * Change tests * removed whitespace and renamed function * test(db-exists): make more durable
1 parent 410f797 commit 7bb858e

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

scripts/cluster.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ Cluster.prototype._replicateRawDB = function (sourceDB, targetDB) {
7979
});
8080
};
8181

82-
Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
82+
Cluster.prototype._createAndReplicateDB = function (sourceDB, targetDB) {
8383
var self = this;
8484
self._log('beginning replication of ' + sourceDB + '...');
8585
return self._createDBIfMissing(targetDB).then(function () {
@@ -88,8 +88,20 @@ Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
8888
}).then(function () {
8989
return self._replicateRawDB(sourceDB, targetDB);
9090
}).then(function () {
91-
self._log('finished replicating ' + sourceDB);
91+
return self._log('finished replicating ' + sourceDB);
9292
});
9393
};
9494

95+
Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
96+
var self = this;
97+
return this._sourceSlouch.db.exists(sourceDB)
98+
.then(function (value) {
99+
if (value === true) {
100+
return self._createAndReplicateDB(sourceDB, targetDB);
101+
} else {
102+
self._log('Database does not exist, skipped replication. Database: ', sourceDB);
103+
}
104+
});
105+
};
106+
95107
module.exports = Cluster;

test/node-and-browser.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,4 +290,30 @@ describe('node and browser', function () {
290290
});
291291
});
292292

293+
it('should check that the database still exists before calling replicate', function () {
294+
cluster = new Cluster({
295+
source: utils.couchDBURL(),
296+
target: utils.couchDBURL()
297+
});
298+
299+
var createAndReplicatedDB = false;
300+
cluster._createAndReplicateDB = function () {
301+
createAndReplicatedDB = true;
302+
};
303+
304+
// Perform replication when DBs exist to ensure that spy is working
305+
return cluster._replicateDB('db1', 'db1').then(function () {
306+
createAndReplicatedDB.should.eql(false);
307+
308+
// Reset the spy flag
309+
createAndReplicatedDB = false;
310+
}).then(function () {
311+
// Attempt to replicate from a DB that no longer exists
312+
return cluster._replicateDB('aaa', 'db1');
313+
}).then(function () {
314+
// Make sure we didn't try to actually replicate
315+
createAndReplicatedDB.should.eql(false);
316+
});
317+
});
318+
293319
});

0 commit comments

Comments
 (0)