Skip to content

Build your personal knowledge base with Trilium Notes

fix(sync): drop erased entities from becca (closes #4762)

Elian Doran22c4f8f67700
diff --git a/packages/trilium-core/src/becca/becca_loader.ts b/packages/trilium-core/src/becca/becca_loader.ts
index 8d1c93c6e4..f9b997d351 100644
--- a/packages/trilium-core/src/becca/becca_loader.ts
+++ b/packages/trilium-core/src/becca/becca_loader.ts
@@ -198,6 +198,20 @@ function branchDeleted(branchId: string) {
if (branch.branchId) {
delete becca.branches[branch.branchId];
}
+
+ dropOrphanedSkeleton(childNote);
+ dropOrphanedSkeleton(parentNote);
+}
+
+/**
+ * Removes a skeleton note (see `BBranch.childNote`) once no branch or attribute refers to it. Its
+ * row can no longer arrive when the note was erased before becca was loaded.
+ */
+function dropOrphanedSkeleton(note: BNote | undefined) {
+ if (note && note.title === undefined && !note.parentBranches.length && !note.children.length
+ && !note.ownedAttributes.length) {
+ noteDeleted(note.noteId);
+ }
}
function noteUpdated(entityRow: NoteRow) {
@@ -265,6 +279,8 @@ function attributeDeleted(attributeId: string) {
if (key in becca.attributeIndex) {
becca.attributeIndex[key] = becca.attributeIndex[key].filter((attr) => attr.attributeId !== attribute.attributeId);
}
+
+ dropOrphanedSkeleton(note);
}
function attributeUpdated(attributeRow: BAttribute) {
diff --git a/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts b/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts
index da02f779e7..aeb8e14118 100644
--- a/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts
+++ b/packages/trilium-core/src/services/sync_skeleton_notes.spec.ts
@@ -3,7 +3,7 @@ import type { Request } from "../http_interface";
import { beforeAll, describe, expect, it } from "vitest";
import becca from "../becca/becca.js";
-import "../becca/becca_loader.js";
+import beccaLoader from "../becca/becca_loader.js";
import treeRoute from "../routes/api/tree.js";
import * as cls from "./context.js";
import { initMessaging } from "./messaging/index.js";
@@ -100,6 +100,39 @@ describe("sync: skeleton notes created by out-of-order entities", () => {
});
});
+describe("sync: entities erased by the sync partner", () => {
+ beforeAll(() => {
+ initMessaging(fakeMessagingProvider);
+ ws.init();
+ });
+
+ it("drops an erased note, its branch and its label from becca", () => {
+ const { noteId, branchId, attributeId } = syncLabeledNote();
+
+ eraseInSync("notes", noteId);
+ eraseInSync("branches", branchId);
+ eraseInSync("attributes", attributeId);
+
+ expect(beccaTraces(noteId, branchId, attributeId)).toEqual(NO_TRACES);
+ });
+
+ it("leaves no skeleton when becca reloads between a note's erase and its branch's", () => {
+ const { noteId, branchId, attributeId } = syncLabeledNote();
+
+ eraseInSync("notes", noteId);
+ // An app restart mid-sync: the note row is gone, its branch and label rows are not yet.
+ cls.init(() => beccaLoader.load());
+ const skeleton = becca.notes[noteId];
+ expect(skeleton).toBeDefined();
+ expect(skeleton?.title).toBeUndefined();
+
+ eraseInSync("branches", branchId);
+ eraseInSync("attributes", attributeId);
+
+ expect(beccaTraces(noteId, branchId, attributeId)).toEqual(NO_TRACES);
+ });
+});
+
const REMOTE_INSTANCE_ID = "skeletonSpecRemote";
const sentMessages: any[] = [];
@@ -124,6 +157,42 @@ function syncBranchAheadOfItsNote() {
return { noteId, branchId };
}
+/** Applies a sync batch creating a note under root with a `status` label, in the partner's order. */
+function syncLabeledNote() {
+ counter++;
+ const noteId = `eraseSpecNote${counter}`;
+ const branchId = `root_${noteId}`;
+ const attributeId = `eraseSpecAttr${counter}`;
+
+ applySync([
+ { entityChange: buildEC({ entityName: "notes", entityId: noteId }), entity: remoteNoteRow(noteId) },
+ { entityChange: buildEC({ entityName: "branches", entityId: branchId }), entity: remoteBranchRow(branchId, noteId) },
+ { entityChange: buildEC({ entityName: "attributes", entityId: attributeId }), entity: remoteLabelRow(attributeId, noteId) }
+ ]);
+ expect(becca.getNoteOrThrow(noteId).title).toBe("arrived late");
+ expect(becca.branches[branchId]).toBeDefined();
+ expect(becca.attributes[attributeId]).toBeDefined();
+
+ return { noteId, branchId, attributeId };
+}
+
+const NO_TRACES = { note: false, branch: false, attribute: false, underRoot: false };
+
+/** What becca still holds of a synced note, so one assertion reports every leftover at once. */
+function beccaTraces(noteId: string, branchId: string, attributeId: string) {
+ return {
+ note: noteId in becca.notes,
+ branch: branchId in becca.branches,
+ attribute: attributeId in becca.attributes,
+ underRoot: becca.getNoteOrThrow("root").children.some((child) => child.noteId === noteId)
+ };
+}
+
+/** Applies a sync batch in which the partner reports the entity as erased, which carries no row. */
+function eraseInSync(entityName: string, entityId: string) {
+ applySync([{ entityChange: buildEC({ entityName, entityId, isErased: true }), entity: undefined }]);
+}
+
function applySync(records: EntityChangeRecord[]) {
return cls.init(() => syncUpdateService.updateEntities(records, REMOTE_INSTANCE_ID));
}
@@ -174,6 +243,20 @@ function remoteNoteRow(noteId: string): EntityRow {
} as unknown as EntityRow;
}
+function remoteLabelRow(attributeId: string, noteId: string): EntityRow {
+ return {
+ attributeId,
+ noteId,
+ type: "label",
+ name: "status",
+ value: "To Do",
+ position: 10,
+ isInheritable: 0,
+ isDeleted: 0,
+ utcDateModified: dateUtils.utcNowDateTime()
+ } as unknown as EntityRow;
+}
+
function insertNoteEntityChange(noteId: string): number {
return cls.init(() => {
getSql().execute(
diff --git a/packages/trilium-core/src/services/sync_update.ts b/packages/trilium-core/src/services/sync_update.ts
index 3baae2d7b2..5267034f07 100644
--- a/packages/trilium-core/src/services/sync_update.ts
+++ b/packages/trilium-core/src/services/sync_update.ts
@@ -59,12 +59,13 @@ function updateEntity(remoteEC: EntityChange, remoteEntityRow: EntityRow | undef
: updateNormalEntity(remoteEC, remoteEntityRow, instanceId, updateContext);
if (updated) {
- if (remoteEntityRow?.isDeleted) {
+ // An erase carries no row, so becca learns of it as a deletion.
+ if (remoteEC.isErased || remoteEntityRow?.isDeleted) {
eventService.emit(eventService.ENTITY_DELETE_SYNCED, {
entityName: remoteEC.entityName,
entityId: remoteEC.entityId
});
- } else if (!remoteEC.isErased) {
+ } else {
eventService.emit(eventService.ENTITY_CHANGE_SYNCED, {
entityName: remoteEC.entityName,
entityRow: remoteEntityRow