This repository was archived by the owner on Oct 20, 2025. It is now read-only.
forked from kobaska/loopback
-
Notifications
You must be signed in to change notification settings - Fork 0
batch the deleting of change records #11
Open
Traksewt
wants to merge
2
commits into
agriwebb-merge
Choose a base branch
from
batch-delete-changes
base: agriwebb-merge
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1474,28 +1474,71 @@ module.exports = function(registry) { | |
| buildLookupOfAffectedModelData(Model, updates, function(err, currentMap) { | ||
| if (err) return callback(err); | ||
|
|
||
| updates.forEach(function(update) { | ||
|
|
||
| var deleteIds = []; | ||
| var deleteConflicts = []; | ||
|
|
||
| updates.forEach(function (update) { | ||
| var id = update.change.modelId; | ||
| var current = currentMap[id]; | ||
| switch (update.type) { | ||
| case Change.UPDATE: | ||
| tasks.push(function(cb) { | ||
| applyUpdate(Model, id, current, update.data, update.change, conflicts, options, cb); | ||
| tasks.push(function (cb) { | ||
| applyUpdate( | ||
| Model, | ||
| id, | ||
| current, | ||
| update.data, | ||
| update.change, | ||
| conflicts, | ||
| options, | ||
| cb | ||
| ); | ||
| }); | ||
| break; | ||
|
|
||
| case Change.CREATE: | ||
| tasks.push(function(cb) { | ||
| applyCreate(Model, id, current, update.data, update.change, conflicts, options, cb); | ||
| tasks.push(function (cb) { | ||
| applyCreate( | ||
| Model, | ||
| id, | ||
| current, | ||
| update.data, | ||
| update.change, | ||
| conflicts, | ||
| options, | ||
| cb | ||
| ); | ||
| }); | ||
| break; | ||
| case Change.DELETE: | ||
| tasks.push(function(cb) { | ||
| applyDelete(Model, id, current, update.change, conflicts, options, cb); | ||
| }); | ||
| const conflictId = findDeleteConflicts( | ||
| Model, | ||
| id, | ||
| current, | ||
| update.change, | ||
| conflicts | ||
| ); | ||
| if (!conflictId) { | ||
| deleteConflicts.push(conflictId); | ||
| } else { | ||
| deleteIds.push(id); | ||
| } | ||
|
|
||
| break; | ||
| } | ||
| }); | ||
| if (deleteConflicts.length) { | ||
| tasks.push(function (cb) { | ||
| rectifyDeleteChanges(Model, deleteConflicts, cb); | ||
| }); | ||
| } | ||
|
|
||
| if (deleteIds.length) { | ||
| tasks.push(function (cb) { | ||
| applyDeletes(Model, deleteIds, update.change, conflicts, options, cb); | ||
| }); | ||
| } | ||
|
|
||
| async.parallel(tasks, function(err) { | ||
| if (err) return callback(err); | ||
|
|
@@ -1619,56 +1662,44 @@ module.exports = function(registry) { | |
| } | ||
| } | ||
|
|
||
| function applyDelete(Model, id, current, change, conflicts, options, cb) { | ||
| /** | ||
| * Returns an id if there is a conflict, otherwise null | ||
| * @param {*} Model | ||
| * @param {*} id | ||
| * @param {*} current | ||
| * @param {*} change | ||
| * @param {*} conflicts | ||
| * @param {*} options | ||
| */ | ||
| function findDeleteConflicts(Model, id, current, change, conflicts) { | ||
| if (!current) { | ||
| // The instance was either already deleted or not created at all, | ||
| // we are done. | ||
| return cb(); | ||
| return null; | ||
| } | ||
|
|
||
| var Change = Model.getChangeModel(); | ||
| var rev = Change.revisionForInst(current); | ||
| if (rev !== change.prev) { | ||
| debug('Detected non-rectified change of %s %j', | ||
| Model.modelName, id); | ||
| debug('Detected non-rectified change of %s %j', Model.modelName, id); | ||
| debug('\tExpected revision: %s', change.rev); | ||
| debug('\tActual revision: %s', rev); | ||
| conflicts.push(change); | ||
| return Change.rectifyModelChanges(Model.modelName, [id], cb); | ||
| return id; | ||
| } | ||
| } | ||
| function rectifyDeleteChanges(Model, ids, cb) { | ||
| return Change.rectifyModelChanges(Model.modelName, ids, cb); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is |
||
| } | ||
|
|
||
| Model.deleteById(id, options, function(err, result) { | ||
| function applyDeletes(Model, ids, change, conflicts, options, cb) { | ||
| var idName = this.getIdName(); | ||
| var where = {}; | ||
| where[idName] = { inq: ids }; | ||
| Model.deleteAll(where, options, function (err) { | ||
| if (err) return cb(err); | ||
|
|
||
| var count = result && result.count; | ||
| switch (count) { | ||
| case 1: | ||
| // The happy path, exactly one record was updated | ||
| return cb(); | ||
|
|
||
| case 0: | ||
| debug('DeleteAll detected non-rectified change of %s %j', | ||
| Model.modelName, id); | ||
| conflicts.push(change); | ||
| // NOTE(bajtos) deleteAll triggers change rectification | ||
| // for all model instances, even when no records were updated, | ||
| // thus we don't need to rectify explicitly ourselves | ||
| return cb(); | ||
|
|
||
| case undefined: | ||
| case null: | ||
| return cb(new Error( | ||
| g.f('Cannot apply bulk updates, ' + | ||
| 'the connector does not correctly report ' + | ||
| 'the number of deleted records.'))); | ||
|
|
||
| default: | ||
| debug('%s.deleteAll modified unexpected number of instances: %j', | ||
| Model.modelName, count); | ||
| return cb(new Error( | ||
| g.f('Bulk update failed, the connector has deleted unexpected ' + | ||
| 'number of records: %s', JSON.stringify(count)))); | ||
| } | ||
| // all is good. exit gracefully, we deleted as much as we could! | ||
| return cb(); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.