From 92f586486f63aee655d624c02d38b3db3d4a04fd Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 5 Feb 2023 01:12:13 +0200 Subject: [PATCH] Fix broken reference consistency check If a branch was identified with a missing parent, the branch would be moved to root. However, the ID of the branch would be changed as a result of that, and this resulted in the creation of a new branch instead of updating the old one. Deleting the old one first solves the issue. --- src/services/consistency_checks.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index 483048d19..41598733e 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -148,13 +148,20 @@ class ConsistencyChecks { AND notes.noteId IS NULL`, ({branchId, parentNoteId}) => { if (this.autoFix) { - const branch = becca.getBranch(branchId); - branch.parentNoteId = 'root'; - branch.save(); + // Delete the old branch and recreate it with root as parent. + const oldBranch = becca.getBranch(branchId); + const noteId = oldBranch.noteId; + oldBranch.markAsDeleted("missing-parent"); + + const newBranch = new Branch({ + parentNoteId: 'root', + noteId: noteId, + prefix: 'recovered' + }).save(); this.reloadNeeded = true; - logFix(`Branch '${branchId}' was set to root parent since it was referencing missing parent note '${parentNoteId}'`); + logFix(`Branch '${branchId}' was was missing parent note '${parentNoteId}', so it was deleted. ${newBranch.branchId} was created in the root instead.`); } else { logError(`Branch '${branchId}' references missing parent note '${parentNoteId}'`); }