From ddce014495ad52e1d7068fd0082dc019a013638b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 15 Jul 2024 19:25:31 +0300 Subject: [PATCH] server: Fix sync failing if local EC is missing --- src/services/sync_update.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/services/sync_update.ts b/src/services/sync_update.ts index 888947b8b0..0b4c5fb1db 100644 --- a/src/services/sync_update.ts +++ b/src/services/sync_update.ts @@ -75,13 +75,12 @@ function updateEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow, instan } function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow, instanceId: string, updateContext: UpdateContext) { - const localEC = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]); + const localEC = sql.getRow(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]); + const localECIsOlderThanRemote = ( + localEC && localEC.utcDateChanged && remoteEC.utcDateChanged && + localEC.utcDateChanged <= remoteEC.utcDateChanged); - if (!localEC.utcDateChanged || !remoteEC.utcDateChanged) { - throw new Error("Missing date changed."); - } - - if (!localEC || localEC.utcDateChanged <= remoteEC.utcDateChanged) { + if (!localEC || localECIsOlderThanRemote) { if (remoteEC.isErased) { if (localEC?.isErased) { eraseEntity(remoteEC); // make sure it's erased anyway @@ -104,7 +103,7 @@ function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow, } if (!localEC - || localEC.utcDateChanged < remoteEC.utcDateChanged + || localECIsOlderThanRemote || localEC.hash !== remoteEC.hash || localEC.isErased !== remoteEC.isErased ) { @@ -113,7 +112,7 @@ function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow, return true; } else if ((localEC.hash !== remoteEC.hash || localEC.isErased !== remoteEC.isErased) - && localEC.utcDateChanged > remoteEC.utcDateChanged) { + && !localECIsOlderThanRemote) { // the change on our side is newer than on the other side, so the other side should update entityChangesService.putEntityChangeForOtherInstances(localEC);