Skip to content

Build your personal knowledge base with Trilium Notes

fix(sync): let changes dated in the future lose conflicts (#12173)

Elian Doran7a09d87bf17a
diff --git a/packages/trilium-core/src/services/sync_update.spec.ts b/packages/trilium-core/src/services/sync_update.spec.ts
index 9fe9a6dfdf..69ef7fe238 100644
--- a/packages/trilium-core/src/services/sync_update.spec.ts
+++ b/packages/trilium-core/src/services/sync_update.spec.ts
@@ -147,6 +147,25 @@ describe("sync_update service (real DB)", () => {
expect(requeuedSince(maxIdBefore, "blobs", localBlobId)).toBe(false);
});
+ it("lets a remote change win against a local change dated in the future", () => {
+ const noteId = createNote();
+ const clockAhead = dateUtils.utcDateTimeStr(new Date(Date.now() + 3600 * 1000));
+ cls.init(() => getSql().execute("UPDATE entity_changes SET utcDateChanged = ? WHERE entityName = 'notes' AND entityId = ?", [clockAhead, noteId]));
+
+ apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: dateUtils.utcNowDateTime() }), entity: rowWith(noteId, { title: "remote-edit" }) }]);
+
+ expect(getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId])).toBe("remote-edit");
+ });
+
+ it("stores a remote change dated in the future at the current time", () => {
+ const noteId = createNote();
+
+ apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: FUTURE }), entity: rowWith(noteId, { title: "remote-edit" }) }]);
+
+ expect(getSql().getValue("SELECT title FROM notes WHERE noteId = ?", [noteId])).toBe("remote-edit");
+ expect(localDateChanged("notes", noteId) <= dateUtils.utcNowDateTime()).toBe(true);
+ });
+
it("is a no-op when local and remote are identical but remote is older", () => {
const noteId = createNote();
const sameHash = localHash("notes", noteId);
@@ -223,6 +242,10 @@ function localChangeId(entityName: string, entityId: string): string {
return getSql().getValue<string>("SELECT changeId FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]) ?? "";
}
+function localDateChanged(entityName: string, entityId: string): string {
+ return getSql().getValue<string>("SELECT utcDateChanged FROM entity_changes WHERE entityName = ? AND entityId = ?", [entityName, entityId]) ?? "";
+}
+
function maxEntityChangeId(): number {
return getSql().getValue<number>("SELECT COALESCE(MAX(id), 0) FROM entity_changes") ?? 0;
}
diff --git a/packages/trilium-core/src/services/sync_update.ts b/packages/trilium-core/src/services/sync_update.ts
index f59c18f7db..61dbcf20d7 100644
--- a/packages/trilium-core/src/services/sync_update.ts
+++ b/packages/trilium-core/src/services/sync_update.ts
@@ -7,6 +7,7 @@ import ws from "./ws.js";
import { default as eventService } from "./events.js";
import entity_constructor from "../becca/entity_constructor.js";
import { decodeBase64, decodeBase64Into } from "./utils/binary.js";
+import dateUtils from "./utils/date.js";
interface UpdateContext {
alreadyErased: number;
@@ -74,9 +75,17 @@ function updateEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow | undef
}
}
-function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow | undefined, instanceId: string, updateContext: UpdateContext) {
+function updateNormalEntity(incomingEC: EntityChange, remoteEntityRow: EntityRow | undefined, instanceId: string, updateContext: UpdateContext) {
+ // A change dated later than now was written while a clock ran ahead, so its real age is
+ // unknown: an incoming one is stored as now, and a local one loses the conflict.
+ const now = dateUtils.utcNowDateTime();
+ const remoteEC = incomingEC.utcDateChanged && incomingEC.utcDateChanged > now
+ ? { ...incomingEC, utcDateChanged: now }
+ : incomingEC;
const localEC = getSql().getRow<EntityChange | undefined>(/*sql*/`SELECT * FROM entity_changes WHERE entityName = ? AND entityId = ?`, [remoteEC.entityName, remoteEC.entityId]);
- const localECIsOlderOrSameAsRemote = localEC && localEC.utcDateChanged && remoteEC.utcDateChanged && localEC.utcDateChanged <= remoteEC.utcDateChanged;
+ const localECIsFromTheFuture = !!localEC?.utcDateChanged && localEC.utcDateChanged > now;
+ const localECIsOlderOrSameAsRemote = localEC && (localECIsFromTheFuture
+ || (localEC.utcDateChanged && remoteEC.utcDateChanged && localEC.utcDateChanged <= remoteEC.utcDateChanged));
if (!localEC || localECIsOlderOrSameAsRemote) {
if (remoteEC.isErased) {