Skip to content

Commit

Permalink
feat(db-exists): make sure db still exists (#33)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
redgeoff authored Dec 16, 2017
1 parent 410f797 commit 7bb858e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
16 changes: 14 additions & 2 deletions scripts/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Cluster.prototype._replicateRawDB = function (sourceDB, targetDB) {
});
};

Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
Cluster.prototype._createAndReplicateDB = function (sourceDB, targetDB) {
var self = this;
self._log('beginning replication of ' + sourceDB + '...');
return self._createDBIfMissing(targetDB).then(function () {
Expand All @@ -88,8 +88,20 @@ Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
}).then(function () {
return self._replicateRawDB(sourceDB, targetDB);
}).then(function () {
self._log('finished replicating ' + sourceDB);
return self._log('finished replicating ' + sourceDB);
});
};

Cluster.prototype._replicateDB = function (sourceDB, targetDB) {
var self = this;
return this._sourceSlouch.db.exists(sourceDB)
.then(function (value) {
if (value === true) {
return self._createAndReplicateDB(sourceDB, targetDB);
} else {
self._log('Database does not exist, skipped replication. Database: ', sourceDB);
}
});
};

module.exports = Cluster;
26 changes: 26 additions & 0 deletions test/node-and-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,30 @@ describe('node and browser', function () {
});
});

it('should check that the database still exists before calling replicate', function () {
cluster = new Cluster({
source: utils.couchDBURL(),
target: utils.couchDBURL()
});

var createAndReplicatedDB = false;
cluster._createAndReplicateDB = function () {
createAndReplicatedDB = true;
};

// Perform replication when DBs exist to ensure that spy is working
return cluster._replicateDB('db1', 'db1').then(function () {
createAndReplicatedDB.should.eql(false);

// Reset the spy flag
createAndReplicatedDB = false;
}).then(function () {
// Attempt to replicate from a DB that no longer exists
return cluster._replicateDB('aaa', 'db1');
}).then(function () {
// Make sure we didn't try to actually replicate
createAndReplicatedDB.should.eql(false);
});
});

});

0 comments on commit 7bb858e

Please sign in to comment.