Closed MarkMYoung closed 11 years ago
I have the same problem, did you find a solution?
I start with a new database, insert a couple of items, and then delete the first item:
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "5CD42CA1AE7C4868BABDBE48750CB6AA", "3F269763244B4B889AFFA05AB0CF54D8"]
DELETE FROM `Device` WHERE id = '5CD42CA1AE7C4868BABDBE48750CB6AA' null
Then delete the second item:
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "5CD42CA1AE7C4868BABDBE48750CB6AA", "EA0D44D922384C5486C8B6D1340C5B4D"]
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "59DC76C346FE46E7806963F2A14B7CEF", "7C2443ED24494E8A81AC56ECB34C7E87"]
DELETE FROM `Device` WHERE id = '59DC76C346FE46E7806963F2A14B7CEF' null`
And so on, every time I delete an item, the previously deleted items are added with a different id than the ones before.
Okay, I took some time today and think I've tracked down the problem. When I got really close to the issue, I added a unique constraint to persistence.sync.RemovedObject.
persistence.sync.RemovedObject.index(['entity', 'objectId'], {unique:true});
I don't think that's the real solution though. I think in persistence.sync.js, almost at the very bottom, where it says
if(meta.enableSync) {
session.add(new persistence.sync.RemovedObject({entity: rec.entity, objectId: rec.id}));
}
It should say something like
if(meta.enableSync
&& !(rec.id in persistence.trackedObjects
&& persistence.trackedObjects[rec.id]._type === rec.entity
)
) {
session.add(new persistence.sync.RemovedObject({entity: rec.entity, objectId: rec.id}));
}
The issue is that persistence.add doesn't know to inspect RemovedObjects' meta data to determine whether it's already been added (and I don't think persistence.add should be specialized to inspect that). I don't have a good place to test this. Please test this (the modified if-statement, not the unique index) and let me/us know whether it worked.
Nope, doesn't work. With this change, the object is not added to the _SyncRemovedObject list when flush() is called, as the object is still in the trackedObjects list. When I call flush() a second time, the object is gone and the _SyncRemovedObject item is added. However, subsequent deletes of other items still insert more and more RemovedObjects:
remove Device1
flush 1
DELETE FROM `Device` WHERE id = 'FDE4C0BB2D62425E97A521871F4A8F20' null
flush 2
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "FDE4C0BB2D62425E97A521871F4A8F20", "7E41D8832D694C258F9C0D5A0148CDBE"]
remove Device2
flush 1
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "FDE4C0BB2D62425E97A521871F4A8F20", "93B1E54354CB4CF2813989C73AE2F0A8"]
DELETE FROM `Device` WHERE id = 'CEDB48BE98874AC7869B5309315E1453' null
flush 2
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "FDE4C0BB2D62425E97A521871F4A8F20", "2D71F65A8F6844D995E50D9F945C981B"]
INSERT INTO `_SyncRemovedObject` (`entity`, `objectId`, id) VALUES (?, ?, ?) ["Device", "CEDB48BE98874AC7869B5309315E1453", "C5AE9521E0024CF693B04A9DDC043CA2"]
I noticed that persistence.trackedObjects[?]._data.objectId equals rec.id for the _SyncRemovedObject types. Could we check on that to avoid duplicating the items?
persistence.trackedObjects
Object
(...)
C5AE9521E0024CF693B04A9DDC043CA2: Entity
_data: Object
entity: "Device"
objectId: "CEDB48BE98874AC7869B5309315E1453"
_data_obj: Object
_dirtyProperties: Object
_new: false
_session: Object
_type: "_SyncRemovedObject"
id: "C5AE9521E0024CF693B04A9DDC043CA2"
I think I fixed this by clearing out objectsRemoved after using it in this function.
Successive deletes produce redundant entries in the _SyncRemovedObject table.
One record removed first time, one _SyncRemovedObject record created:
One record removed second time, two _SyncRemovedObject records created:
One record removed third time, three _SyncRemovedObject records created:
One record removed fourth time, four _SyncRemovedObject records created: