mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	more autofixers WIP
This commit is contained in:
		
							parent
							
								
									0308b13460
								
							
						
					
					
						commit
						1f853024ee
					
				@ -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() {
 | 
					async function runAllChecks() {
 | 
				
			||||||
    outstandingConsistencyErrors = false;
 | 
					    outstandingConsistencyErrors = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -207,6 +237,17 @@ async function runAllChecks() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    await checkAllDeletedNotesBranchesAreDeleted();
 | 
					    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
 | 
					    // FIXME - does this make sense? Specifically branch - branch comparison seems strange
 | 
				
			||||||
    await runCheck(false, `
 | 
					    await runCheck(false, `
 | 
				
			||||||
          SELECT 
 | 
					          SELECT 
 | 
				
			||||||
@ -222,17 +263,6 @@ async function runAllChecks() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    await checkAllNotesShouldHaveUndeletedBranch();
 | 
					    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, `
 | 
					    await runCheck(false, `
 | 
				
			||||||
          SELECT 
 | 
					          SELECT 
 | 
				
			||||||
            noteRevisionId || ' > ' || note_revisions.noteId AS value
 | 
					            noteRevisionId || ' > ' || note_revisions.noteId AS value
 | 
				
			||||||
@ -242,19 +272,7 @@ async function runAllChecks() {
 | 
				
			|||||||
            notes.noteId IS NULL`,
 | 
					            notes.noteId IS NULL`,
 | 
				
			||||||
        "Missing notes records for following note revision ID > note ID");
 | 
					        "Missing notes records for following note revision ID > note ID");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    await runCheck(false, `
 | 
					    await checkDuplicateParentChildBranches();
 | 
				
			||||||
          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 runCheck(false, `
 | 
					    await runCheck(false, `
 | 
				
			||||||
          SELECT 
 | 
					          SELECT 
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user