From c654172d33eed3773b5f76074a2c97479b522920 Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 21 Jan 2019 22:51:49 +0100 Subject: [PATCH] auto fixer for "undeleted branch of deleted note" consistency check --- src/services/consistency_checks.js | 31 ++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index b79c69d4b..076875ec0 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -101,6 +101,26 @@ async function fixEmptyRelationTargets(errorList) { } } +async function fixUndeletedBranches() { + const undeletedBranches = await sql.getRows(` + SELECT + branchId, noteId + FROM + branches + JOIN notes USING(noteId) + WHERE + notes.isDeleted = 1 + AND branches.isDeleted = 0`); + + for (const {branchId, noteId} of undeletedBranches) { + const branch = await repository.getBranch(branchId); + branch.isDeleted = true; + await branch.save(); + + log.info(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`); + } +} + async function runAllChecks() { const errorList = []; @@ -125,16 +145,7 @@ async function runAllChecks() { notes.noteId IS NULL`, "Missing notes records for following branch ID > note ID", errorList); - await runCheck(` - SELECT - branchId - FROM - branches - JOIN notes USING(noteId) - WHERE - notes.isDeleted = 1 - AND branches.isDeleted = 0`, - "Branch is not deleted even though main note is deleted for following branch IDs", errorList); + await fixUndeletedBranches(); await runCheck(` SELECT