2016-05-04 00:10:46 +00:00
|
|
|
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
|
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2014-10-08 22:07:03 +00:00
|
|
|
/*!
|
2014-01-26 21:20:19 +00:00
|
|
|
* Module Dependencies.
|
|
|
|
*/
|
|
|
|
|
2014-11-04 12:52:49 +00:00
|
|
|
var PersistedModel = require('../../lib/loopback').PersistedModel;
|
|
|
|
var loopback = require('../../lib/loopback');
|
2016-01-27 19:42:47 +00:00
|
|
|
var utils = require('../../lib/utils');
|
2014-11-04 12:52:49 +00:00
|
|
|
var crypto = require('crypto');
|
|
|
|
var CJSON = {stringify: require('canonical-json')};
|
|
|
|
var async = require('async');
|
|
|
|
var assert = require('assert');
|
|
|
|
var debug = require('debug')('loopback:change');
|
2015-03-26 16:14:41 +00:00
|
|
|
var deprecate = require('depd')('loopback');
|
2014-01-26 21:20:19 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Change list entry.
|
|
|
|
*
|
2015-02-23 21:13:52 +00:00
|
|
|
* @property {String} id Hash of the modelName and ID.
|
|
|
|
* @property {String} rev The current model revision.
|
|
|
|
* @property {String} prev The previous model revision.
|
|
|
|
* @property {Number} checkpoint The current checkpoint at time of the change.
|
|
|
|
* @property {String} modelName Model name.
|
|
|
|
* @property {String} modelId Model ID.
|
|
|
|
* @property {Object} settings Extends the `Model.settings` object.
|
|
|
|
* @property {String} settings.hashAlgorithm Algorithm used to create cryptographic hash, used as argument
|
|
|
|
* to [crypto.createHash](http://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm). Default is sha1.
|
|
|
|
* @property {Boolean} settings.ignoreErrors By default, when changes are rectified, an error will throw an exception.
|
|
|
|
* However, if this setting is true, then errors will not throw exceptions.
|
2014-10-13 09:58:14 +00:00
|
|
|
* @class Change
|
|
|
|
* @inherits {PersistedModel}
|
2014-01-26 21:20:19 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
module.exports = function(Change) {
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/*!
|
|
|
|
* Constants
|
|
|
|
*/
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.UPDATE = 'update';
|
|
|
|
Change.CREATE = 'create';
|
|
|
|
Change.DELETE = 'delete';
|
|
|
|
Change.UNKNOWN = 'unknown';
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/*!
|
|
|
|
* Conflict Class
|
|
|
|
*/
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.Conflict = Conflict;
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/*!
|
|
|
|
* Setup the extended model.
|
|
|
|
*/
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.setup = function() {
|
|
|
|
PersistedModel.setup.call(this);
|
|
|
|
var Change = this;
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.getter.id = function() {
|
|
|
|
var hasModel = this.modelName && this.modelId;
|
|
|
|
if (!hasModel) return null;
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
return Change.idForModel(this.modelName, this.modelId);
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
|
|
|
};
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.setup();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Track the recent change of the given modelIds.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {Array} modelIds
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Array} changes Changes that were tracked
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.rectifyModelChanges = function(modelName, modelIds, callback) {
|
|
|
|
var Change = this;
|
2015-03-26 16:14:41 +00:00
|
|
|
var errors = [];
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
callback = callback || utils.createPromiseCallback();
|
|
|
|
|
2015-03-26 16:14:41 +00:00
|
|
|
var tasks = modelIds.map(function(id) {
|
|
|
|
return function(cb) {
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.findOrCreateChange(modelName, id, function(err, change) {
|
2015-03-26 16:14:41 +00:00
|
|
|
if (err) return next(err);
|
|
|
|
change.rectify(next);
|
2014-10-13 09:58:14 +00:00
|
|
|
});
|
2015-03-26 16:14:41 +00:00
|
|
|
|
|
|
|
function next(err) {
|
|
|
|
if (err) {
|
|
|
|
err.modelName = modelName;
|
|
|
|
err.modelId = id;
|
|
|
|
errors.push(err);
|
|
|
|
}
|
|
|
|
cb();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
async.parallel(tasks, function(err) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
if (errors.length) {
|
|
|
|
var desc = errors
|
|
|
|
.map(function(e) {
|
|
|
|
return '#' + e.modelId + ' - ' + e.toString();
|
|
|
|
})
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
var msg = 'Cannot rectify ' + modelName + ' changes:\n' + desc;
|
|
|
|
err = new Error(msg);
|
|
|
|
err.details = { errors: errors };
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
callback();
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
2016-01-27 19:42:47 +00:00
|
|
|
return callback.promise;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get an identifier for a given model.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String} modelId
|
|
|
|
* @return {String}
|
|
|
|
*/
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.idForModel = function(modelName, modelId) {
|
|
|
|
return this.hash([modelName, modelId].join('-'));
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Find or create a change for the given model.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {String} modelId
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Change} change
|
|
|
|
* @end
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.findOrCreateChange = function(modelName, modelId, callback) {
|
|
|
|
assert(loopback.findModel(modelName), modelName + ' does not exist');
|
2016-01-27 19:42:47 +00:00
|
|
|
callback = callback || utils.createPromiseCallback();
|
2014-10-13 09:58:14 +00:00
|
|
|
var id = this.idForModel(modelName, modelId);
|
|
|
|
var Change = this;
|
|
|
|
|
|
|
|
this.findById(id, function(err, change) {
|
|
|
|
if (err) return callback(err);
|
|
|
|
if (change) {
|
|
|
|
callback(null, change);
|
|
|
|
} else {
|
|
|
|
var ch = new Change({
|
|
|
|
id: id,
|
|
|
|
modelName: modelName,
|
|
|
|
modelId: modelId
|
|
|
|
});
|
|
|
|
ch.debug('creating change');
|
2015-03-16 12:38:37 +00:00
|
|
|
Change.updateOrCreate(ch, callback);
|
2014-10-13 09:58:14 +00:00
|
|
|
}
|
|
|
|
});
|
2016-01-27 19:42:47 +00:00
|
|
|
return callback.promise;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Update (or create) the change with the current revision.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Change} change
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.rectify = function(cb) {
|
|
|
|
var change = this;
|
|
|
|
var currentRev = this.rev;
|
|
|
|
|
|
|
|
change.debug('rectify change');
|
|
|
|
|
2016-01-27 19:42:47 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2015-12-07 13:05:06 +00:00
|
|
|
change.currentRevision(function(err, rev) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
// avoid setting rev and prev to the same value
|
|
|
|
if (currentRev === rev) {
|
|
|
|
change.debug('rev and prev are equal (not updating anything)');
|
|
|
|
return cb(null, change);
|
2014-10-13 09:58:14 +00:00
|
|
|
}
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2015-12-07 13:05:06 +00:00
|
|
|
// FIXME(@bajtos) Allo callers to pass in the checkpoint value
|
|
|
|
// (or even better - a memoized async function to get the cp value)
|
|
|
|
// That will enable `rectifyAll` to cache the checkpoint value
|
|
|
|
change.constructor.getCheckpointModel().current(
|
|
|
|
function(err, checkpoint) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
doRectify(checkpoint, rev);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2016-01-27 19:42:47 +00:00
|
|
|
return cb.promise;
|
2015-03-05 12:13:21 +00:00
|
|
|
|
2015-12-07 13:05:06 +00:00
|
|
|
function doRectify(checkpoint, rev) {
|
2015-03-05 12:13:21 +00:00
|
|
|
if (rev) {
|
|
|
|
if (currentRev === rev) {
|
2015-12-07 13:05:06 +00:00
|
|
|
change.debug('ASSERTION FAILED: Change currentRev==rev ' +
|
|
|
|
'should have been already handled');
|
2015-12-07 12:55:14 +00:00
|
|
|
return cb(null, change);
|
2015-03-05 12:13:21 +00:00
|
|
|
} else {
|
|
|
|
change.rev = rev;
|
|
|
|
change.debug('updated revision (was ' + currentRev + ')');
|
|
|
|
if (change.checkpoint !== checkpoint) {
|
|
|
|
// previous revision is updated only across checkpoints
|
2014-10-13 09:58:14 +00:00
|
|
|
change.prev = currentRev;
|
2015-03-05 12:13:21 +00:00
|
|
|
change.debug('updated prev');
|
2014-10-13 09:58:14 +00:00
|
|
|
}
|
2015-03-05 12:13:21 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
change.rev = null;
|
|
|
|
change.debug('updated revision (was ' + currentRev + ')');
|
|
|
|
if (change.checkpoint !== checkpoint) {
|
|
|
|
// previous revision is updated only across checkpoints
|
2014-10-13 09:58:14 +00:00
|
|
|
if (currentRev) {
|
|
|
|
change.prev = currentRev;
|
|
|
|
} else if (!change.prev) {
|
|
|
|
change.debug('ERROR - could not determing prev');
|
|
|
|
change.prev = Change.UNKNOWN;
|
|
|
|
}
|
2015-03-05 12:13:21 +00:00
|
|
|
change.debug('updated prev');
|
2014-10-13 09:58:14 +00:00
|
|
|
}
|
2015-03-05 12:13:21 +00:00
|
|
|
}
|
2014-05-20 02:54:55 +00:00
|
|
|
|
2015-03-05 12:13:21 +00:00
|
|
|
if (change.checkpoint != checkpoint) {
|
|
|
|
debug('update checkpoint to', checkpoint);
|
2014-10-13 09:58:14 +00:00
|
|
|
change.checkpoint = checkpoint;
|
2015-03-05 12:13:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (change.prev === Change.UNKNOWN) {
|
|
|
|
// this occurs when a record of a change doesn't exist
|
|
|
|
// and its current revision is null (not found)
|
|
|
|
change.remove(cb);
|
|
|
|
} else {
|
|
|
|
change.save(cb);
|
|
|
|
}
|
2014-10-13 09:58:14 +00:00
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get a change's current revision based on current data.
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} rev The current revision
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.currentRevision = function(cb) {
|
2016-01-27 19:42:47 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2014-10-13 09:58:14 +00:00
|
|
|
var model = this.getModelCtor();
|
|
|
|
var id = this.getModelId();
|
|
|
|
model.findById(id, function(err, inst) {
|
2015-03-26 16:14:41 +00:00
|
|
|
if (err) return cb(err);
|
2014-10-13 09:58:14 +00:00
|
|
|
if (inst) {
|
|
|
|
cb(null, Change.revisionForInst(inst));
|
2014-05-20 02:54:55 +00:00
|
|
|
} else {
|
2014-10-13 09:58:14 +00:00
|
|
|
cb(null, null);
|
2014-05-20 02:54:55 +00:00
|
|
|
}
|
2014-01-28 20:54:41 +00:00
|
|
|
});
|
2016-01-27 19:42:47 +00:00
|
|
|
return cb.promise;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Create a hash of the given `string` with the `options.hashAlgorithm`.
|
|
|
|
* **Default: `sha1`**
|
|
|
|
*
|
|
|
|
* @param {String} str The string to be hashed
|
|
|
|
* @return {String} The hashed string
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.hash = function(str) {
|
|
|
|
return crypto
|
|
|
|
.createHash(Change.settings.hashAlgorithm || 'sha1')
|
|
|
|
.update(str)
|
|
|
|
.digest('hex');
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get the revision string for the given object
|
|
|
|
* @param {Object} inst The data to get the revision string for
|
|
|
|
* @return {String} The revision string
|
|
|
|
*/
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.revisionForInst = function(inst) {
|
2015-03-16 12:38:37 +00:00
|
|
|
assert(inst, 'Change.revisionForInst() requires an instance object.');
|
2014-10-13 09:58:14 +00:00
|
|
|
return this.hash(CJSON.stringify(inst));
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get a change's type. Returns one of:
|
|
|
|
*
|
|
|
|
* - `Change.UPDATE`
|
|
|
|
* - `Change.CREATE`
|
|
|
|
* - `Change.DELETE`
|
|
|
|
* - `Change.UNKNOWN`
|
|
|
|
*
|
|
|
|
* @return {String} the type of change
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.type = function() {
|
|
|
|
if (this.rev && this.prev) {
|
|
|
|
return Change.UPDATE;
|
|
|
|
}
|
|
|
|
if (this.rev && !this.prev) {
|
|
|
|
return Change.CREATE;
|
|
|
|
}
|
|
|
|
if (!this.rev && this.prev) {
|
|
|
|
return Change.DELETE;
|
|
|
|
}
|
|
|
|
return Change.UNKNOWN;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Compare two changes.
|
|
|
|
* @param {Change} change
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.equals = function(change) {
|
|
|
|
if (!change) return false;
|
|
|
|
var thisRev = this.rev || null;
|
|
|
|
var thatRev = change.rev || null;
|
|
|
|
return thisRev === thatRev;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-20 02:54:55 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Does this change conflict with the given change.
|
|
|
|
* @param {Change} change
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.prototype.conflictsWith = function(change) {
|
|
|
|
if (!change) return false;
|
|
|
|
if (this.equals(change)) return false;
|
|
|
|
if (Change.bothDeleted(this, change)) return false;
|
|
|
|
if (this.isBasedOn(change)) return false;
|
|
|
|
return true;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-20 02:54:55 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Are both changes deletes?
|
|
|
|
* @param {Change} a
|
|
|
|
* @param {Change} b
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-05-20 02:54:55 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.bothDeleted = function(a, b) {
|
2014-11-04 12:52:49 +00:00
|
|
|
return a.type() === Change.DELETE &&
|
|
|
|
b.type() === Change.DELETE;
|
|
|
|
};
|
2014-05-20 02:54:55 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Determine if the change is based on the given change.
|
|
|
|
* @param {Change} change
|
|
|
|
* @return {Boolean}
|
|
|
|
*/
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.prototype.isBasedOn = function(change) {
|
|
|
|
return this.prev === change.rev;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-26 21:20:19 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Determine the differences for a given model since a given checkpoint.
|
|
|
|
*
|
|
|
|
* The callback will contain an error or `result`.
|
|
|
|
*
|
|
|
|
* **result**
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* {
|
2014-01-26 21:20:19 +00:00
|
|
|
* deltas: Array,
|
|
|
|
* conflicts: Array
|
|
|
|
* }
|
2014-10-13 09:58:14 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* **deltas**
|
|
|
|
*
|
|
|
|
* An array of changes that differ from `remoteChanges`.
|
|
|
|
*
|
|
|
|
* **conflicts**
|
|
|
|
*
|
|
|
|
* An array of changes that conflict with `remoteChanges`.
|
|
|
|
*
|
|
|
|
* @param {String} modelName
|
|
|
|
* @param {Number} since Compare changes after this checkpoint
|
|
|
|
* @param {Change[]} remoteChanges A set of changes to compare
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Object} result See above.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.diff = function(modelName, since, remoteChanges, callback) {
|
2016-01-27 19:42:47 +00:00
|
|
|
callback = callback || utils.createPromiseCallback();
|
|
|
|
|
2015-01-10 18:28:55 +00:00
|
|
|
if (!Array.isArray(remoteChanges) || remoteChanges.length === 0) {
|
2016-01-27 19:42:47 +00:00
|
|
|
callback(null, {deltas: [], conflicts: []});
|
|
|
|
return callback.promise;
|
2015-01-10 18:28:55 +00:00
|
|
|
}
|
2014-10-13 09:58:14 +00:00
|
|
|
var remoteChangeIndex = {};
|
|
|
|
var modelIds = [];
|
|
|
|
remoteChanges.forEach(function(ch) {
|
|
|
|
modelIds.push(ch.modelId);
|
|
|
|
remoteChangeIndex[ch.modelId] = new Change(ch);
|
2014-01-26 21:20:19 +00:00
|
|
|
});
|
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
// normalize `since`
|
|
|
|
since = Number(since) || 0;
|
|
|
|
this.find({
|
|
|
|
where: {
|
|
|
|
modelName: modelName,
|
2015-03-16 12:38:37 +00:00
|
|
|
modelId: {inq: modelIds}
|
2014-01-28 20:54:41 +00:00
|
|
|
}
|
2015-03-16 12:38:37 +00:00
|
|
|
}, function(err, allLocalChanges) {
|
2014-10-13 09:58:14 +00:00
|
|
|
if (err) return callback(err);
|
|
|
|
var deltas = [];
|
|
|
|
var conflicts = [];
|
|
|
|
var localModelIds = [];
|
|
|
|
|
2015-03-16 12:38:37 +00:00
|
|
|
var localChanges = allLocalChanges.filter(function(c) {
|
|
|
|
return c.checkpoint >= since;
|
|
|
|
});
|
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
localChanges.forEach(function(localChange) {
|
|
|
|
localChange = new Change(localChange);
|
|
|
|
localModelIds.push(localChange.modelId);
|
|
|
|
var remoteChange = remoteChangeIndex[localChange.modelId];
|
|
|
|
if (remoteChange && !localChange.equals(remoteChange)) {
|
|
|
|
if (remoteChange.conflictsWith(localChange)) {
|
|
|
|
remoteChange.debug('remote conflict');
|
|
|
|
localChange.debug('local conflict');
|
|
|
|
conflicts.push(localChange);
|
|
|
|
} else {
|
|
|
|
remoteChange.debug('remote delta');
|
|
|
|
deltas.push(remoteChange);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
modelIds.forEach(function(id) {
|
2015-03-16 12:38:37 +00:00
|
|
|
if (localModelIds.indexOf(id) !== -1) return;
|
|
|
|
|
|
|
|
var d = remoteChangeIndex[id];
|
|
|
|
var oldChange = allLocalChanges.filter(function(c) {
|
|
|
|
return c.modelId === id;
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
if (oldChange) {
|
|
|
|
d.prev = oldChange.rev;
|
|
|
|
} else {
|
|
|
|
d.prev = null;
|
2014-10-13 09:58:14 +00:00
|
|
|
}
|
2015-03-16 12:38:37 +00:00
|
|
|
|
|
|
|
deltas.push(d);
|
2014-10-13 09:58:14 +00:00
|
|
|
});
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
callback(null, {
|
|
|
|
deltas: deltas,
|
|
|
|
conflicts: conflicts
|
|
|
|
});
|
2014-01-28 20:54:41 +00:00
|
|
|
});
|
2016-01-27 19:42:47 +00:00
|
|
|
return callback.promise;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-10-13 09:58:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Correct all change list entries.
|
2014-11-04 12:52:49 +00:00
|
|
|
* @param {Function} cb
|
2014-10-13 09:58:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
Change.rectifyAll = function(cb) {
|
|
|
|
debug('rectify all');
|
|
|
|
var Change = this;
|
|
|
|
// this should be optimized
|
|
|
|
this.find(function(err, changes) {
|
|
|
|
if (err) return cb(err);
|
2015-02-20 18:28:33 +00:00
|
|
|
async.each(
|
|
|
|
changes,
|
|
|
|
function(c, next) { c.rectify(next); },
|
|
|
|
cb);
|
2014-10-13 09:58:14 +00:00
|
|
|
});
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-20 02:54:55 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get the checkpoint model.
|
|
|
|
* @return {Checkpoint}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Change.getCheckpointModel = function() {
|
|
|
|
var checkpointModel = this.Checkpoint;
|
|
|
|
if (checkpointModel) return checkpointModel;
|
2015-03-03 13:23:46 +00:00
|
|
|
// FIXME(bajtos) This code creates multiple different models with the same
|
|
|
|
// model name, which is not a valid supported usage of juggler's API.
|
2014-12-24 15:40:48 +00:00
|
|
|
this.Checkpoint = checkpointModel = loopback.Checkpoint.extend('checkpoint');
|
2015-02-20 14:31:15 +00:00
|
|
|
assert(this.dataSource, 'Cannot getCheckpointModel(): ' + this.modelName +
|
|
|
|
' is not attached to a dataSource');
|
2014-10-13 09:58:14 +00:00
|
|
|
checkpointModel.attachTo(this.dataSource);
|
|
|
|
return checkpointModel;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-16 00:27:02 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.handleError = function(err) {
|
2015-03-26 16:14:41 +00:00
|
|
|
deprecate('Change.handleError is deprecated, ' +
|
|
|
|
'you should pass errors to your callback instead.');
|
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
if (!this.settings.ignoreErrors) {
|
|
|
|
throw err;
|
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.prototype.debug = function() {
|
|
|
|
if (debug.enabled) {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2015-03-04 14:00:53 +00:00
|
|
|
args[0] = args[0] + ' %s';
|
|
|
|
args.push(this.modelName);
|
2014-10-13 09:58:14 +00:00
|
|
|
debug.apply(this, args);
|
|
|
|
debug('\tid', this.id);
|
|
|
|
debug('\trev', this.rev);
|
|
|
|
debug('\tprev', this.prev);
|
2015-03-04 14:00:53 +00:00
|
|
|
debug('\tcheckpoint', this.checkpoint);
|
2014-10-13 09:58:14 +00:00
|
|
|
debug('\tmodelName', this.modelName);
|
|
|
|
debug('\tmodelId', this.modelId);
|
|
|
|
debug('\ttype', this.type());
|
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get the `Model` class for `change.modelName`.
|
|
|
|
* @return {Model}
|
|
|
|
*/
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.prototype.getModelCtor = function() {
|
|
|
|
return this.constructor.settings.trackModel;
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.prototype.getModelId = function() {
|
|
|
|
// TODO(ritch) get rid of the need to create an instance
|
|
|
|
var Model = this.getModelCtor();
|
|
|
|
var id = this.modelId;
|
|
|
|
var m = new Model();
|
|
|
|
m.setId(id);
|
|
|
|
return m.getId();
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
Change.prototype.getModel = function(callback) {
|
|
|
|
var Model = this.constructor.settings.trackModel;
|
|
|
|
var id = this.getModelId();
|
|
|
|
Model.findById(id, callback);
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* When two changes conflict a conflict is created.
|
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* **Note**: call `conflict.fetch()` to get the `target` and `source` models.
|
2014-10-13 09:58:14 +00:00
|
|
|
*
|
|
|
|
* @param {*} modelId
|
|
|
|
* @param {PersistedModel} SourceModel
|
|
|
|
* @param {PersistedModel} TargetModel
|
|
|
|
* @property {ModelClass} source The source model instance
|
|
|
|
* @property {ModelClass} target The target model instance
|
|
|
|
* @class Change.Conflict
|
|
|
|
*/
|
|
|
|
|
|
|
|
function Conflict(modelId, SourceModel, TargetModel) {
|
|
|
|
this.SourceModel = SourceModel;
|
|
|
|
this.TargetModel = TargetModel;
|
|
|
|
this.SourceChange = SourceModel.getChangeModel();
|
|
|
|
this.TargetChange = TargetModel.getChangeModel();
|
|
|
|
this.modelId = modelId;
|
2014-05-14 16:20:04 +00:00
|
|
|
}
|
2014-01-28 20:54:41 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Fetch the conflicting models.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {PersistedModel} source
|
|
|
|
* @param {PersistedModel} target
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.models = function(cb) {
|
|
|
|
var conflict = this;
|
|
|
|
var SourceModel = this.SourceModel;
|
|
|
|
var TargetModel = this.TargetModel;
|
|
|
|
var source;
|
|
|
|
var target;
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
getSourceModel,
|
|
|
|
getTargetModel
|
|
|
|
], done);
|
|
|
|
|
|
|
|
function getSourceModel(cb) {
|
|
|
|
SourceModel.findById(conflict.modelId, function(err, model) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
source = model;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
function getTargetModel(cb) {
|
|
|
|
TargetModel.findById(conflict.modelId, function(err, model) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
target = model;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
function done(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, source, target);
|
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Get the conflicting changes.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {Change} sourceChange
|
|
|
|
* @param {Change} targetChange
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.changes = function(cb) {
|
|
|
|
var conflict = this;
|
|
|
|
var sourceChange;
|
|
|
|
var targetChange;
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
getSourceChange,
|
|
|
|
getTargetChange
|
|
|
|
], done);
|
|
|
|
|
|
|
|
function getSourceChange(cb) {
|
2015-04-09 10:44:29 +00:00
|
|
|
var SourceModel = conflict.SourceModel;
|
|
|
|
SourceModel.findLastChange(conflict.modelId, function(err, change) {
|
2014-10-13 09:58:14 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
sourceChange = change;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
function getTargetChange(cb) {
|
2015-04-09 10:44:29 +00:00
|
|
|
var TargetModel = conflict.TargetModel;
|
|
|
|
TargetModel.findLastChange(conflict.modelId, function(err, change) {
|
2014-10-13 09:58:14 +00:00
|
|
|
if (err) return cb(err);
|
|
|
|
targetChange = change;
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
function done(err) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
cb(null, sourceChange, targetChange);
|
|
|
|
}
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Resolve the conflict.
|
|
|
|
*
|
2015-03-20 16:47:07 +00:00
|
|
|
* Set the source change's previous revision to the current revision of the
|
|
|
|
* (conflicting) target change. Since the changes are no longer conflicting
|
|
|
|
* and appear as if the source change was based on the target, they will be
|
|
|
|
* replicated normally as part of the next replicate() call.
|
|
|
|
*
|
|
|
|
* This is effectively resolving the conflict using the source version.
|
|
|
|
*
|
2014-10-13 09:58:14 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.resolve = function(cb) {
|
|
|
|
var conflict = this;
|
2015-04-09 10:44:29 +00:00
|
|
|
conflict.TargetModel.findLastChange(
|
|
|
|
this.modelId,
|
|
|
|
function(err, targetChange) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
conflict.SourceModel.updateLastChange(
|
|
|
|
conflict.modelId,
|
|
|
|
{ prev: targetChange.rev },
|
|
|
|
cb);
|
|
|
|
});
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-05-14 16:20:04 +00:00
|
|
|
|
2015-03-20 16:47:07 +00:00
|
|
|
/**
|
|
|
|
* Resolve the conflict using the instance data in the source model.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
Conflict.prototype.resolveUsingSource = function(cb) {
|
|
|
|
this.resolve(function(err) {
|
|
|
|
// don't forward any cb arguments from resolve()
|
|
|
|
cb(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resolve the conflict using the instance data in the target model.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
Conflict.prototype.resolveUsingTarget = function(cb) {
|
|
|
|
var conflict = this;
|
|
|
|
|
|
|
|
conflict.models(function(err, source, target) {
|
|
|
|
if (err) return done(err);
|
|
|
|
if (target === null) {
|
|
|
|
return conflict.SourceModel.deleteById(conflict.modelId, done);
|
|
|
|
}
|
2015-04-09 10:44:29 +00:00
|
|
|
var inst = new conflict.SourceModel(
|
|
|
|
target.toObject(),
|
|
|
|
{ persisted: true });
|
2015-03-20 16:47:07 +00:00
|
|
|
inst.save(done);
|
|
|
|
});
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
// don't forward any cb arguments from internal calls
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-09 10:44:29 +00:00
|
|
|
/**
|
|
|
|
* Return a new Conflict instance with swapped Source and Target models.
|
|
|
|
*
|
|
|
|
* This is useful when resolving a conflict in one-way
|
|
|
|
* replication, where the source data must not be changed:
|
|
|
|
*
|
|
|
|
* ```js
|
|
|
|
* conflict.swapParties().resolveUsingTarget(cb);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns {Conflict} A new Conflict instance.
|
|
|
|
*/
|
|
|
|
Conflict.prototype.swapParties = function() {
|
|
|
|
var Ctor = this.constructor;
|
|
|
|
return new Ctor(this.modelId, this.TargetModel, this.SourceModel);
|
|
|
|
};
|
|
|
|
|
2015-03-20 16:47:07 +00:00
|
|
|
/**
|
|
|
|
* Resolve the conflict using the supplied instance data.
|
|
|
|
*
|
|
|
|
* @param {Object} data The set of changes to apply on the model
|
|
|
|
* instance. Use `null` value to delete the source instance instead.
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.resolveManually = function(data, cb) {
|
|
|
|
var conflict = this;
|
|
|
|
if (!data) {
|
|
|
|
return conflict.SourceModel.deleteById(conflict.modelId, done);
|
|
|
|
}
|
|
|
|
|
|
|
|
conflict.models(function(err, source, target) {
|
|
|
|
if (err) return done(err);
|
|
|
|
var inst = source || new conflict.SourceModel(target);
|
|
|
|
inst.setAttributes(data);
|
|
|
|
inst.save(function(err) {
|
|
|
|
if (err) return done(err);
|
|
|
|
conflict.resolve(done);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function done(err) {
|
|
|
|
// don't forward any cb arguments from internal calls
|
|
|
|
cb(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-13 09:58:14 +00:00
|
|
|
/**
|
|
|
|
* Determine the conflict type.
|
|
|
|
*
|
|
|
|
* Possible results are
|
|
|
|
*
|
|
|
|
* - `Change.UPDATE`: Source and target models were updated.
|
|
|
|
* - `Change.DELETE`: Source and or target model was deleted.
|
|
|
|
* - `Change.UNKNOWN`: the conflict type is uknown or due to an error.
|
|
|
|
*
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} type The conflict type.
|
|
|
|
*/
|
|
|
|
|
|
|
|
Conflict.prototype.type = function(cb) {
|
|
|
|
var conflict = this;
|
|
|
|
this.changes(function(err, sourceChange, targetChange) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
var sourceChangeType = sourceChange.type();
|
|
|
|
var targetChangeType = targetChange.type();
|
|
|
|
if (sourceChangeType === Change.UPDATE && targetChangeType === Change.UPDATE) {
|
|
|
|
return cb(null, Change.UPDATE);
|
|
|
|
}
|
|
|
|
if (sourceChangeType === Change.DELETE || targetChangeType === Change.DELETE) {
|
|
|
|
return cb(null, Change.DELETE);
|
|
|
|
}
|
|
|
|
return cb(null, Change.UNKNOWN);
|
|
|
|
});
|
2014-11-04 12:52:49 +00:00
|
|
|
};
|
2014-10-13 09:58:14 +00:00
|
|
|
};
|