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.
This commit is contained in:
Elian Doran 2023-02-05 01:12:13 +02:00
parent e7c6d912a4
commit 92f586486f

View file

@ -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}'`);
}