From 1f853024ee26a0e7cb660138d7e2139ad656e72e Mon Sep 17 00:00:00 2001 From: azivner Date: Sat, 19 Jan 2019 09:57:51 +0100 Subject: [PATCH] more autofixers WIP --- src/services/consistency_checks.js | 66 +++++++++++++++++++----------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.js index f041d750d..908436ccb 100644 --- a/src/services/consistency_checks.js +++ b/src/services/consistency_checks.js @@ -198,6 +198,36 @@ async function checkAllNotesShouldHaveUndeletedBranch() { } } +async function checkDuplicateParentChildBranches() { + const records = await runCheck(true, ` + SELECT + branches.parentNoteId || ' > ' || branches.noteId AS value, noteId, parentNoteId + FROM + branches + WHERE + branches.isDeleted = 0 + GROUP BY + branches.parentNoteId, + branches.noteId + HAVING + COUNT(*) > 1`, + "Duplicate undeleted parent note <-> note relationship - parent note ID > note ID"); + + for (const {noteId, parentNoteId} of records) { + const branches = await repository.getEntities(`SELECT * FROM branches WHERE noteId = ? and parentNoteId = ? and isDeleted = 1`, [noteId, parentNoteId]); + + if (branches.length <= 1) { + log.error("Inconsistent detection of duplicate parent note <-> relationships."); + } + + // delete all but the first branch + for (const branch of branches.slice(1)) { + branch.isDeleted = true; + await branch.save(); + } + } +} + async function runAllChecks() { outstandingConsistencyErrors = false; @@ -207,6 +237,17 @@ async function runAllChecks() { await checkAllDeletedNotesBranchesAreDeleted(); + await runCheck(false, ` + SELECT + child.parentNoteId || ' > ' || child.noteId AS value + FROM branches + AS child + LEFT JOIN branches AS parent ON parent.noteId = child.parentNoteId + WHERE + parent.noteId IS NULL + AND child.parentNoteId != 'none'`, + "Not existing parent in the following parent > child relations"); + // FIXME - does this make sense? Specifically branch - branch comparison seems strange await runCheck(false, ` SELECT @@ -222,17 +263,6 @@ async function runAllChecks() { await checkAllNotesShouldHaveUndeletedBranch(); - await runCheck(false, ` - SELECT - child.parentNoteId || ' > ' || child.noteId AS value - FROM branches - AS child - LEFT JOIN branches AS parent ON parent.noteId = child.parentNoteId - WHERE - parent.noteId IS NULL - AND child.parentNoteId != 'none'`, - "Not existing parent in the following parent > child relations"); - await runCheck(false, ` SELECT noteRevisionId || ' > ' || note_revisions.noteId AS value @@ -242,19 +272,7 @@ async function runAllChecks() { notes.noteId IS NULL`, "Missing notes records for following note revision ID > note ID"); - await runCheck(false, ` - SELECT - branches.parentNoteId || ' > ' || branches.noteId AS value - FROM - branches - WHERE - branches.isDeleted = 0 - GROUP BY - branches.parentNoteId, - branches.noteId - HAVING - COUNT(*) > 1`, - "Duplicate undeleted parent note <-> note relationship - parent note ID > note ID"); + await checkDuplicateParentChildBranches(); await runCheck(false, ` SELECT