Skip to content

Commit

Permalink
Fixed validateCollection for mongodb 1.9.1
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Aug 10, 2011
1 parent 20af814 commit e88d4a4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
2 changes: 2 additions & 0 deletions HISTORY
@@ -1,6 +1,8 @@

* Specific type Double for capped collections (https://github.com/mbostock), Issue #312
* Decorating Errors with all all object info from Mongo (https://github.com/laurie71), Issue #308
* Implementing fixes for mongodb 1.9.1 and higher to make tests pass
* Admin validateCollection now takes an options argument for you to pass in full option

0.9.6-9
* Bug fix for bson parsing the key '':'' correctly without crashing
Expand Down
20 changes: 17 additions & 3 deletions lib/mongodb/admin.js
Expand Up @@ -125,20 +125,34 @@ Admin.prototype.command = function(command, callback) {
});
}

Admin.prototype.validateCollection = function(collectionName, callback) {
Admin.prototype.validateCollection = function(collectionName, options, callback) {
var args = Array.prototype.slice.call(arguments, 1);
callback = args.pop();
options = args.length ? args.shift() : {};

var self = this;
var command = {validate: collectionName};
var keys = Object.keys(options);

// Decorate command with extra options
for(var i = 0; i < keys.length; i++) {
if(options.hasOwnProperty(keys[i])) {
command[keys[i]] = options[keys[i]];
}
}

this.db.executeDbCommand(command, function(err, doc) {
if(err != null) return callback(err, null);
doc = doc.documents[0];

if(doc.ok == 0) {
return callback(new Error("Error with validate command"), null);
} else if(doc.result.constructor != String) {
} else if(doc.result != null && doc.result.constructor != String) {
return callback(new Error("Error with validation data"), null);
} else if(doc.result.match(/exception|corrupt/) != null) {
} else if(doc.result != null && doc.result.match(/exception|corrupt/) != null) {
return callback(new Error("Error: invalid collection " + collectionName), null);
} else if(doc.valid != null && !doc.valid) {
return callback(new Error("Error: invalid collection " + collectionName), null);
} else {
return callback(null, doc);
}
Expand Down
10 changes: 7 additions & 3 deletions test/admin_test.js
Expand Up @@ -55,8 +55,13 @@ var tests = testCase({
fs_client.admin(function(err, adminDb) {
adminDb.authenticate('admin', 'admin', function(err, replies) {
adminDb.validateCollection('test', function(err, doc) {
test.ok(doc.result != null);
test.ok(doc.result.match(/firstExtent/) != null);
// Pre 1.9.1 servers
if(doc.result != null) {
test.ok(doc.result != null);
test.ok(doc.result.match(/firstExtent/) != null);
} else {
test.ok(doc.firstExtent != null);
}

fs_client.close();
test.done();
Expand Down Expand Up @@ -160,7 +165,6 @@ var tests = testCase({
test.ok(infos.constructor == Array);
test.ok(infos.length >= 1);
test.ok(infos[0].ts.constructor == Date);
test.ok(infos[0].info.constructor == String);
test.ok(infos[0].millis.constructor == Number);

fs_client.close();
Expand Down
12 changes: 9 additions & 3 deletions test/find_test.js
Expand Up @@ -740,13 +740,19 @@ var tests = testCase({
});
});
},

'Should correctly return null when attempting to modify a non-existing document' : function(test) {
client.createCollection('AttemptToFindAndModifyNonExistingDocument', function(err, collection) {
// Let's modify the document in place
collection.findAndModify({name: 'test1'}, [], {$set: {name: 'test2'}}, {}, function(err, updated_doc) {
test.equal(null, updated_doc);
test.ok(err != null);
if(parseInt(client.version.replace(/./, '')) < 191) {
test.equal(null, updated_doc);
test.ok(err != null);
} else {
test.equal(null, updated_doc);
test.equal(null, err);
}

test.done();
});
});
Expand Down

0 comments on commit e88d4a4

Please sign in to comment.