Skip to content

Commit

Permalink
server: Fix sync failing if local EC is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Jul 15, 2024
1 parent f323193 commit ddce014
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/services/sync_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EntityChange>(`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);
const localEC = sql.getRow<EntityChange | undefined>(`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
Expand All @@ -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
) {
Expand All @@ -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);

Expand Down

0 comments on commit ddce014

Please sign in to comment.