mongopatch
Advanced tools
Comparing version 0.7.2 to 0.7.3
{ | ||
"name": "mongopatch", | ||
"version": "0.7.2", | ||
"version": "0.7.3", | ||
"repository": "git://github.com/e-conomic/mongopatch.git", | ||
@@ -27,3 +27,4 @@ "description": "MongoDB patching tool", | ||
"speedometer": "~0.1.2", | ||
"moment": "~2.5.0" | ||
"moment": "~2.5.0", | ||
"json-stable-stringify": "1.0.0" | ||
}, | ||
@@ -30,0 +31,0 @@ "devDependencies": { |
@@ -168,2 +168,6 @@ mongopatch [![Build Status](https://travis-ci.org/e-conomic/mongopatch.png?branch=master)](https://travis-ci.org/e-conomic/mongopatch) | ||
#### Version 0.7.3 | ||
- Added optimistic locking fallback using the `$where` operator. Some documents didn't match them self when used in a query. | ||
#### Version 0.7.2 | ||
@@ -170,0 +174,0 @@ |
@@ -0,1 +1,5 @@ | ||
var fs = require('fs'); | ||
var util = require('util'); | ||
var path = require('path'); | ||
var async = require('async'); | ||
@@ -5,3 +9,6 @@ var streams = require('stream-wrapper'); | ||
var speedometer = require('speedometer'); | ||
var stringify = require('json-stable-stringify'); | ||
var bson = new (require('bson').pure().BSON)(); | ||
var mongojs = require('mongojs'); | ||
var traverse = require('traverse'); | ||
@@ -11,3 +18,7 @@ var diff = require('./diff'); | ||
var DEFAULT_CONCURRENCY = 1; | ||
var ATTEMPT_LIMIT = 5; | ||
var JSON_STRINGIFY_SOURCE = fs.readFileSync(path.join(__dirname, '..', 'node_modules', 'json-stable-stringify', 'index.js'), 'utf-8'); | ||
var COMPARE_TEMPLATE = fs.readFileSync(path.join(__dirname, 'compare.js.txt'), 'utf-8'); | ||
var bsonCopy = function(obj) { | ||
@@ -59,3 +70,40 @@ return bson.deserialize(bson.serialize(obj)); | ||
var applyUpdateUsingWhere = function(patch, callback) { | ||
var document = traverse(patch.before).map(function(value) { | ||
if(value instanceof mongojs.ObjectId) { | ||
this.update(value.toJSON(), true); | ||
} else if(value instanceof Date) { | ||
this.update(value.toJSON(), true); | ||
} else if(value instanceof mongojs.NumberLong) { | ||
this.update(value.toNumber(), true); | ||
} else if(value instanceof mongojs.Timestamp) { | ||
this.update(util.format('%s:%s', value.getLowBits(), value.getHighBits()), true); | ||
} | ||
}); | ||
document = JSON.stringify(stringify(document)); | ||
var where = util.format(COMPARE_TEMPLATE, JSON_STRINGIFY_SOURCE, document); | ||
var query = { _id: patch.before._id }; | ||
query.$where = where; | ||
patch.collection.findAndModify({ | ||
query: query, | ||
'new': true, | ||
update: patch.modifier | ||
}, function(err, after) { | ||
callback(err, after); | ||
}); | ||
}; | ||
var applyUpdateUsingDocument = function(worker, patch, callback) { | ||
if(patch.attempts === ATTEMPT_LIMIT) { | ||
// In some cases a document doesn't match itself when used | ||
// as a query (perhaps a bug). Try one last time using the slow | ||
// where operator. | ||
patch.attempts++; | ||
return applyUpdateUsingWhere(patch, callback); | ||
} | ||
async.waterfall([ | ||
@@ -106,2 +154,4 @@ function(next) { | ||
patch.modifier = modifier; | ||
patch.attempts++; | ||
applyUpdateUsingDocument(worker, patch, callback); | ||
@@ -166,2 +216,4 @@ } | ||
logDocument = result[0]; | ||
patch.attempts = patch.attempts || 1; | ||
fn(patch, next); | ||
@@ -174,5 +226,7 @@ }, | ||
if(patch.skipped) { | ||
return logCollection.update({ _id: logDocument._id }, { $set: { skipped: true } }, function(err) { | ||
callback(err, patch); | ||
}); | ||
return logCollection.update({ _id: logDocument._id }, { $set: { modified: false, skipped: true, attempts: patch.attempts } }, | ||
function(err) { | ||
callback(err, patch); | ||
} | ||
); | ||
} | ||
@@ -186,3 +240,3 @@ | ||
{ _id: logDocument._id }, | ||
{ $set: { after: after, modified: patch.modified, diff: patch.diff } }, | ||
{ $set: { after: after, modified: patch.modified, skipped: false, diff: patch.diff, attempts: patch.attempts } }, | ||
function(err) { | ||
@@ -239,2 +293,3 @@ next(err); | ||
function(next) { | ||
patch.attempts = patch.attempts || 1; | ||
fn(patch, next); | ||
@@ -241,0 +296,0 @@ }, |
38580
13
972
190
16
4
+ Addedjson-stable-stringify@1.0.0
+ Addedes-abstract@1.23.8(transitive)
+ Addedjson-stable-stringify@1.0.0(transitive)
+ Addedjsonify@0.0.1(transitive)
+ Addedown-keys@1.0.1(transitive)
+ Addedsafe-push-apply@1.0.0(transitive)
- Removedes-abstract@1.23.7(transitive)