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(notes): erase a delete batch at its last request, not on every one
Elian Doran2bf67b54359b
diff --git a/packages/trilium-core/src/routes/api/branches.spec.ts b/packages/trilium-core/src/routes/api/branches.spec.tsindex 0bec4c8e04..3f096453d2 100644--- a/packages/trilium-core/src/routes/api/branches.spec.ts+++ b/packages/trilium-core/src/routes/api/branches.spec.ts@@ -24,6 +24,23 @@ function getBranchRow(branchId: string): BranchRow | null {);}+function noteExists(noteId: string): boolean {+ return getSql().getRowOrNull("SELECT noteId FROM notes WHERE noteId = ?", [ noteId ]) !== null;+}++/**+ * Counts the entity changes flagged `isErased` for the given ids. The client reloads itself as soon+ * as one of these reaches it (`froca_updater.processNoteChange`), so a batch that raises the count+ * before its last request kills the requests that were still to come.+ */+function erasedEntityChangeCount(entityIds: string[]): number {+ const placeholders = entityIds.map(() => "?").join(", ");+ return getSql().getValue<number>(+ `SELECT COUNT(1) FROM entity_changes WHERE isErased = 1 AND entityId IN (${placeholders})`,+ entityIds+ ) ?? 0;+}+describe("Branches API (core)", () => {beforeAll(() => {api = CoreApiTester.build();@@ -264,5 +281,32 @@ describe("Branches API (core)", () => {const noteRow = getSql().getRowOrNull("SELECT noteId FROM notes WHERE noteId = ?", [ noteId ]);expect(noteRow).toBeNull();});++ it("holds a multi-branch erase back until the last request of the task group", async () => {+ const first = await createTextNote(api, { title: "Batch erase 1" });+ const second = await createTextNote(api, { title: "Batch erase 2" });+ const taskId = "test-branch-erase-batch";++ const firstRes = await api.delete(`/api/branches/${first.branchId}`, {+ query: { taskId, last: "false", eraseNotes: "true" }+ });+ expect(firstRes.status).toBe(200);++ // Both notes are soft-deleted at this point, but nothing is erased: the client is still+ // sending the rest of the batch, and an erase here would reload it out from under them.+ expect(noteExists(first.noteId)).toBe(true);+ expect(erasedEntityChangeCount([ first.noteId, second.noteId ])).toBe(0);+ expect(getBranchRow(first.branchId)?.isDeleted).toBe(1);++ const secondRes = await api.delete(`/api/branches/${second.branchId}`, {+ query: { taskId, last: "true", eraseNotes: "true" }+ });+ expect(secondRes.status).toBe(200);++ // The last request erases everything the group deleted, in one go.+ expect(noteExists(first.noteId)).toBe(false);+ expect(noteExists(second.noteId)).toBe(false);+ expect(erasedEntityChangeCount([ first.noteId, second.noteId ])).toBe(2);+ });});});diff --git a/packages/trilium-core/src/routes/api/branches.ts b/packages/trilium-core/src/routes/api/branches.tsindex 45e8906ea4..feb2fbfc9b 100644--- a/packages/trilium-core/src/routes/api/branches.ts+++ b/packages/trilium-core/src/routes/api/branches.ts@@ -246,13 +246,14 @@ function deleteBranch(req: Request<{ branchId: string }>) {if (eraseNotes) {// erase automatically means deleting all clones + note itselfbranch.getNote().deleteNote(deleteId, taskContext);- eraseService.eraseNotesWithDeleteId(deleteId);+ taskContext.scheduleErase(deleteId);noteDeleted = true;} else {noteDeleted = branch.deleteBranch(deleteId, taskContext);}if (last) {+ eraseService.eraseNotesWithDeleteIds(taskContext.takeScheduledErases());taskContext.taskSucceeded(null);}diff --git a/packages/trilium-core/src/routes/api/notes.spec.ts b/packages/trilium-core/src/routes/api/notes.spec.tsindex b8780a1fda..3ca0b904a7 100644--- a/packages/trilium-core/src/routes/api/notes.spec.ts+++ b/packages/trilium-core/src/routes/api/notes.spec.ts@@ -266,6 +266,30 @@ describe("Notes API (core)", () => {// Erasing removes the row entirely rather than just flagging it deleted.expect(noteIsDeleted(noteId)).toBeNull();});++ it("holds a multi-note erase back until the last request of the task group", async () => {+ const first = await createTextNote(api, { title: "Batch erase note 1" });+ const second = await createTextNote(api, { title: "Batch erase note 2" });+ const taskId = "test-note-erase-batch";++ const firstRes = await api.delete(`/api/notes/${first.noteId}`, {+ query: { taskId, last: "false", eraseNotes: "true" }+ });+ expect(firstRes.status).toBe(204);++ // Soft-deleted, but not erased: the client is still sending the rest of the batch, and+ // an erase here would reload it out from under them.+ expect(noteIsDeleted(first.noteId)).toBe(1);++ const secondRes = await api.delete(`/api/notes/${second.noteId}`, {+ query: { taskId, last: "true", eraseNotes: "true" }+ });+ expect(secondRes.status).toBe(204);++ // The last request erases everything the group deleted, in one go.+ expect(noteIsDeleted(first.noteId)).toBeNull();+ expect(noteIsDeleted(second.noteId)).toBeNull();+ });});describe("creating with targets", () => {diff --git a/packages/trilium-core/src/routes/api/notes.ts b/packages/trilium-core/src/routes/api/notes.tsindex a8ab279325..2abb4b5138 100644--- a/packages/trilium-core/src/routes/api/notes.ts+++ b/packages/trilium-core/src/routes/api/notes.ts@@ -202,10 +202,11 @@ function deleteNote(req: Request<{ noteId: string }>) {note.deleteNote(deleteId, taskContext);if (eraseNotes) {- eraseService.eraseNotesWithDeleteId(deleteId);+ taskContext.scheduleErase(deleteId);}if (last) {+ eraseService.eraseNotesWithDeleteIds(taskContext.takeScheduledErases());taskContext.taskSucceeded(null);}}diff --git a/packages/trilium-core/src/services/erase.spec.ts b/packages/trilium-core/src/services/erase.spec.tsindex 0ffa19bc7f..c226a5e4a2 100644--- a/packages/trilium-core/src/services/erase.spec.ts+++ b/packages/trilium-core/src/services/erase.spec.ts@@ -40,7 +40,7 @@ function entityChangeFor(entityName: string, entityId: string) {}describe("erase service (real DB)", () => {- describe("eraseNotesWithDeleteId", () => {+ describe("eraseNotesWithDeleteIds", () => {it("erases a soft-deleted note plus its dependent branch and attribute, marking entity_changes as erased", () => {const note = createNote();const noteId = note.noteId;@@ -59,7 +59,7 @@ describe("erase service (real DB)", () => {// Sanity: rows still physically present after a soft delete.expect(rowCount("notes", "noteId", noteId)).toBe(1);- getContext().init(() => eraseService.eraseNotesWithDeleteId(deleteId));+ getContext().init(() => eraseService.eraseNotesWithDeleteIds([ deleteId ]));// The physical rows are gone.expect(rowCount("notes", "noteId", noteId)).toBe(0);@@ -85,7 +85,7 @@ describe("erase service (real DB)", () => {expect(rowCount("attachments", "attachmentId", attachmentId)).toBe(1);- getContext().init(() => eraseService.eraseNotesWithDeleteId(deleteId));+ getContext().init(() => eraseService.eraseNotesWithDeleteIds([ deleteId ]));expect(rowCount("attachments", "attachmentId", attachmentId)).toBe(0);expect(entityChangeFor("attachments", attachmentId)?.isErased).toBe(1);@@ -95,11 +95,37 @@ describe("erase service (real DB)", () => {const note = createNote();const noteId = note.noteId;- getContext().init(() => eraseService.eraseNotesWithDeleteId("nonexistent-delete-id"));+ getContext().init(() =>+ eraseService.eraseNotesWithDeleteIds([ "nonexistent-delete-id" ])+ );// The live note is untouched.expect(rowCount("notes", "noteId", noteId)).toBe(1);});++ it("erases every deletion of a batch, and does nothing for an empty batch", () => {+ const first = createNote();+ const second = createNote();+ const firstDeleteId = `del-batch-a-${counter}`;+ const secondDeleteId = `del-batch-b-${counter}`;++ getContext().init(() => {+ first.markAsDeleted(firstDeleteId);+ second.markAsDeleted(secondDeleteId);+ });++ getContext().init(() => eraseService.eraseNotesWithDeleteIds([]));+ expect(rowCount("notes", "noteId", first.noteId)).toBe(1);++ getContext().init(() =>+ eraseService.eraseNotesWithDeleteIds([ firstDeleteId, secondDeleteId ])+ );++ expect(rowCount("notes", "noteId", first.noteId)).toBe(0);+ expect(rowCount("notes", "noteId", second.noteId)).toBe(0);+ expect(entityChangeFor("notes", first.noteId)?.isErased).toBe(1);+ expect(entityChangeFor("notes", second.noteId)?.isErased).toBe(1);+ });});describe("eraseDeletedNotesNow", () => {diff --git a/packages/trilium-core/src/services/erase.ts b/packages/trilium-core/src/services/erase.tsindex 98ee51d3df..8e193b7efb 100644--- a/packages/trilium-core/src/services/erase.ts+++ b/packages/trilium-core/src/services/erase.ts@@ -145,21 +145,43 @@ function eraseDeletedEntities(eraseEntitiesAfterTimeInSeconds: number | null = n});}-function eraseNotesWithDeleteId(deleteId: string) {+/**+ * Erases the entities marked by any of the given deletions, in a single pass.+ *+ * The client's `deleteNotes` deletes one branch per request, each with its own deleteId, and+ * `setEntityChangesAsErased` marks entity changes that make every connected client reload. Erasing+ * a whole group at once keeps that to a single reload, after its last request.+ */+function eraseNotesWithDeleteIds(deleteIds: string[]) {+ if (deleteIds.length === 0) {+ return;+ }+const sql = getSql();- const noteIdsToErase = sql.getColumn<string>("SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);+ const noteIdsToErase = sql.getManyRows<{ noteId: string }>(+ /*sql*/`SELECT noteId FROM notes WHERE isDeleted = 1 AND deleteId IN (???)`,+ deleteIds+ ).map((row) => row.noteId);eraseNotes(noteIdsToErase);- const branchIdsToErase = sql.getColumn<string>("SELECT branchId FROM branches WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);+ const branchIdsToErase = sql.getManyRows<{ branchId: string }>(+ /*sql*/`SELECT branchId FROM branches WHERE isDeleted = 1 AND deleteId IN (???)`,+ deleteIds+ ).map((row) => row.branchId);eraseBranches(branchIdsToErase);- const attributeIdsToErase = sql.getColumn<string>("SELECT attributeId FROM attributes WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);+ const attributeIdsToErase = sql.getManyRows<{ attributeId: string }>(+ /*sql*/`SELECT attributeId FROM attributes WHERE isDeleted = 1 AND deleteId IN (???)`,+ deleteIds+ ).map((row) => row.attributeId);eraseAttributes(attributeIdsToErase);- const attachmentIdsToErase = sql.getColumn<string>("SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND deleteId = ?", [deleteId]);+ const attachmentIdsToErase = sql.getManyRows<{ attachmentId: string }>(+ /*sql*/`SELECT attachmentId FROM attachments WHERE isDeleted = 1 AND deleteId IN (???)`,+ deleteIds+ ).map((row) => row.attachmentId);eraseAttachments(attachmentIdsToErase);-eraseUnusedBlobs();}@@ -216,7 +238,7 @@ function startScheduledCleanup() {export default {eraseDeletedNotesNow,eraseUnusedAttachmentsNow,- eraseNotesWithDeleteId,+ eraseNotesWithDeleteIds,eraseUnusedBlobs,eraseAttachments,eraseRevisions,diff --git a/packages/trilium-core/src/services/task_context.spec.ts b/packages/trilium-core/src/services/task_context.spec.tsindex b27493fc38..d38844395f 100644--- a/packages/trilium-core/src/services/task_context.spec.ts+++ b/packages/trilium-core/src/services/task_context.spec.ts@@ -210,6 +210,21 @@ describe("TaskContext", () => {});});+ describe("scheduled erases", () => {+ it("accumulates deleteIds across a task group and hands them over exactly once", () => {+ const context = TaskContext.getInstance("erase-batch-1", "deleteNotes", null);++ expect(context.takeScheduledErases()).toEqual([]);++ context.scheduleErase("del-1");+ context.scheduleErase("del-2");++ expect(context.takeScheduledErases()).toEqual([ "del-1", "del-2" ]);+ // Taking them clears the list, so a later request of the same group re-erases nothing.+ expect(context.takeScheduledErases()).toEqual([]);+ });+ });+describe("getInstance", () => {it("creates a single cached context per task id and reuses it on later lookups", () => {const first = TaskContext.getInstance("singleton-1", "importNotes", { safeImport: true });diff --git a/packages/trilium-core/src/services/task_context.ts b/packages/trilium-core/src/services/task_context.tsindex 4aabd7c457..c00215be74 100644--- a/packages/trilium-core/src/services/task_context.ts+++ b/packages/trilium-core/src/services/task_context.ts@@ -13,6 +13,7 @@ class TaskContext<T extends TaskType> {private totalCount: number | null;private phase: ProgressPhase | null;private lastSentCountTs: number;+ private scheduledEraseDeleteIds: string[];data: TaskData<T>;noteDeletionHandlerTriggered: boolean;@@ -21,6 +22,7 @@ class TaskContext<T extends TaskType> {this.taskType = taskType;this.data = data;this.noteDeletionHandlerTriggered = false;+ this.scheduledEraseDeleteIds = [];this.totalCount = null;this.phase = null;@@ -95,6 +97,23 @@ class TaskContext<T extends TaskType> {}}+ /**+ * Records a deleteId whose entities are to be erased once the task group ends. Erasing flags+ * entity changes as erased, which makes every connected client reload; a delete-notes group+ * spans one request per branch, so erasing before the last one cuts off the requests the client+ * has yet to send.+ */+ scheduleErase(deleteId: string) {+ this.scheduledEraseDeleteIds.push(deleteId);+ }++ /** Returns the deleteIds collected by {@link scheduleErase} and clears them. */+ takeScheduledErases(): string[] {+ const deleteIds = this.scheduledEraseDeleteIds;+ this.scheduledEraseDeleteIds = [];+ return deleteIds;+ }+increaseProgressCount() {this.progressCount++;