Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(sync): send a note's blob with the row that wins (closes #12173)
Elian Doranb60d838cec56
diff --git a/packages/commons/src/lib/ws_api.ts b/packages/commons/src/lib/ws_api.tsindex 1abf6ac07f..dfdd4e16fa 100644--- a/packages/commons/src/lib/ws_api.ts+++ b/packages/commons/src/lib/ws_api.ts@@ -22,6 +22,7 @@ export interface EntityChange {export interface EntityRow {isDeleted?: boolean;content?: Uint8Array | string;+ blobId?: string;}export interface EntityChangeRecord {diff --git a/packages/trilium-core/src/services/sync_update.spec.ts b/packages/trilium-core/src/services/sync_update.spec.tsindex 007c84d516..9fe9a6dfdf 100644--- a/packages/trilium-core/src/services/sync_update.spec.ts+++ b/packages/trilium-core/src/services/sync_update.spec.ts@@ -124,6 +124,29 @@ describe("sync_update service (real DB)", () => {expect(emitSpy).not.toHaveBeenCalledWith(events.ENTITY_CHANGE_SYNCED, expect.anything());});+ it("requeues the local blob with a newer note whose blob the remote does not reference", () => {+ const noteId = createNote();+ const localBlobId = String(noteRow(noteId).blobId);+ const maxIdBefore = maxEntityChangeId();++ apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { blobId: "su_remote_blob" }) }]);++ expect(noteRow(noteId).blobId).toBe(localBlobId);+ expect(requeuedSince(maxIdBefore, "notes", noteId)).toBe(true);+ expect(requeuedSince(maxIdBefore, "blobs", localBlobId)).toBe(true);+ });++ it("does not requeue the blob when a newer local note shares it with the remote row", () => {+ const noteId = createNote();+ const localBlobId = String(noteRow(noteId).blobId);+ const maxIdBefore = maxEntityChangeId();++ apply([{ entityChange: buildEC({ entityId: noteId, hash: "different", utcDateChanged: PAST }), entity: rowWith(noteId, { title: "remote-title" }) }]);++ expect(requeuedSince(maxIdBefore, "notes", noteId)).toBe(true);+ expect(requeuedSince(maxIdBefore, "blobs", localBlobId)).toBe(false);+ });+it("is a no-op when local and remote are identical but remote is older", () => {const noteId = createNote();const sameHash = localHash("notes", noteId);@@ -200,6 +223,15 @@ function localChangeId(entityName: string, entityId: string): string {return getSql().getValue<string>("SELECT changeId 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;+}++/** Whether the entity's change was written after `id`, which is what puts it in the next push. */+function requeuedSince(id: number, entityName: string, entityId: string): boolean {+ return !!getSql().getValue("SELECT 1 FROM entity_changes WHERE entityName = ? AND entityId = ? AND id > ?", [entityName, entityId, id]);+}+function blobRow(blobId: string, content: string): EntityRow {const now = dateUtils.utcNowDateTime();return { blobId, content, dateModified: now, utcDateModified: now } as unknown as EntityRow;diff --git a/packages/trilium-core/src/services/sync_update.ts b/packages/trilium-core/src/services/sync_update.tsindex 5267034f07..f59c18f7db 100644--- a/packages/trilium-core/src/services/sync_update.ts+++ b/packages/trilium-core/src/services/sync_update.ts@@ -108,6 +108,7 @@ function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow |} else if ((localEC.hash !== remoteEC.hash || localEC.isErased !== remoteEC.isErased) && !localECIsOlderOrSameAsRemote) {// the change on our side is newer than on the other side, so the other side should updateentityChangesService.putEntityChangeForOtherInstances(localEC);+ requeueOwnBlobIfRemoteLacksIt(remoteEC, remoteEntityRow);return false;}@@ -115,6 +116,29 @@ function updateNormalEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow |return false;}+const BLOB_OWNING_ENTITIES = new Set(["notes", "attachments", "revisions"]);++/**+ * Sends the local blob along with a local row that won a conflict. The remote side can have erased+ * that blob in `deleteBlobIfNotUsed()` when its own row moved to another blob, and the blob's own+ * entity change was pushed long ago, so without this the winning row arrives pointing at nothing.+ */+function requeueOwnBlobIfRemoteLacksIt(remoteEC: EntityChange, remoteEntityRow: EntityRow | undefined) {+ if (!BLOB_OWNING_ENTITIES.has(remoteEC.entityName)) {+ return;+ }++ const primaryKeyName = entity_constructor.getEntityFromEntityName(remoteEC.entityName).primaryKeyName;+ const localBlobId = getSql().getValue<string>(+ /*sql*/`SELECT blobId FROM ${remoteEC.entityName} WHERE ${primaryKeyName} = ?`,+ [remoteEC.entityId]+ );++ if (localBlobId && localBlobId !== remoteEntityRow?.blobId) {+ entityChangesService.putBlobEntityChange(localBlobId);+ }+}+function preProcessContent(remoteEC: EntityChange, remoteEntityRow: EntityRow) {if (remoteEC.entityName === "blobs" && remoteEntityRow.content !== null) {// we always use a Buffer object which is different from normal saving - there we use a simple string type for