mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-31 04:51:31 +08:00 
			
		
		
		
	refactoring of consistency checks + some auto fixers
This commit is contained in:
		
							parent
							
								
									91ca07929d
								
							
						
					
					
						commit
						06b8a82f70
					
				| @ -5,23 +5,29 @@ const sqlInit = require('./sql_init'); | |||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const messagingService = require('./messaging'); | const messagingService = require('./messaging'); | ||||||
| const syncMutexService = require('./sync_mutex'); | const syncMutexService = require('./sync_mutex'); | ||||||
| const repository = require('./repository.js'); | const repository = require('./repository'); | ||||||
| const cls = require('./cls'); | const cls = require('./cls'); | ||||||
|  | const Branch = require('../entities/branch'); | ||||||
| 
 | 
 | ||||||
| async function runCheck(query, errorText, errorList) { | let outstandingConsistencyErrors = false; | ||||||
|     const result = await sql.getColumn(query); |  | ||||||
| 
 | 
 | ||||||
|     if (result.length > 0) { | async function runCheck(recoverable, query, errorText) { | ||||||
|         const resultText = result.map(val => "'" + val + "'").join(', '); |     const results = await sql.getRows(query); | ||||||
| 
 | 
 | ||||||
|         const err = errorText + ": " + resultText; |     if (results.length > 0) { | ||||||
|         errorList.push(err); |         const resultText = results.map(row => "'" + row.value + "'").join(', '); | ||||||
| 
 | 
 | ||||||
|         log.error(err); |         log.error(errorText + ": " + resultText); | ||||||
|  | 
 | ||||||
|  |         if (!recoverable) { | ||||||
|  |             outstandingConsistencyErrors = true; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| async function checkTreeCycles(errorList) { |     return results; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function checkTreeCycles() { | ||||||
|     const childToParents = {}; |     const childToParents = {}; | ||||||
|     const rows = await sql.getRows("SELECT noteId, parentNoteId FROM branches WHERE isDeleted = 0"); |     const rows = await sql.getRows("SELECT noteId, parentNoteId FROM branches WHERE isDeleted = 0"); | ||||||
| 
 | 
 | ||||||
| @ -33,7 +39,7 @@ async function checkTreeCycles(errorList) { | |||||||
|         childToParents[childNoteId].push(parentNoteId); |         childToParents[childNoteId].push(parentNoteId); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     function checkTreeCycle(noteId, path, errorList) { |     function checkTreeCycle(noteId, path) { | ||||||
|         if (noteId === 'root') { |         if (noteId === 'root') { | ||||||
|             return; |             return; | ||||||
|         } |         } | ||||||
| @ -45,13 +51,15 @@ async function checkTreeCycles(errorList) { | |||||||
| 
 | 
 | ||||||
|         for (const parentNoteId of childToParents[noteId]) { |         for (const parentNoteId of childToParents[noteId]) { | ||||||
|             if (path.includes(parentNoteId)) { |             if (path.includes(parentNoteId)) { | ||||||
|                 errorList.push(`Tree cycle detected at parent-child relationship: ${parentNoteId} - ${noteId}, whole path: ${path}`); |                 log.error(`Tree cycle detected at parent-child relationship: ${parentNoteId} - ${noteId}, whole path: ${path}`); | ||||||
|  |                  | ||||||
|  |                 outstandingConsistencyErrors = true; | ||||||
|             } |             } | ||||||
|             else { |             else { | ||||||
|                 const newPath = path.slice(); |                 const newPath = path.slice(); | ||||||
|                 newPath.push(noteId); |                 newPath.push(noteId); | ||||||
| 
 | 
 | ||||||
|                 checkTreeCycle(parentNoteId, newPath, errorList); |                 checkTreeCycle(parentNoteId, newPath); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @ -59,86 +67,150 @@ async function checkTreeCycles(errorList) { | |||||||
|     const noteIds = Object.keys(childToParents); |     const noteIds = Object.keys(childToParents); | ||||||
| 
 | 
 | ||||||
|     for (const noteId of noteIds) { |     for (const noteId of noteIds) { | ||||||
|         checkTreeCycle(noteId, [], errorList); |         checkTreeCycle(noteId, []); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     if (childToParents['root'].length !== 1 || childToParents['root'][0] !== 'none') { |     if (childToParents['root'].length !== 1 || childToParents['root'][0] !== 'none') { | ||||||
|         errorList.push('Incorrect root parent: ' + JSON.stringify(childToParents['root'])); |         log.error('Incorrect root parent: ' + JSON.stringify(childToParents['root'])); | ||||||
|  |         outstandingConsistencyErrors = true; | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function runSyncRowChecks(table, key, errorList) { | async function runSyncRowChecks(table, key) { | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|         SELECT  |         SELECT  | ||||||
|           ${key}  |           ${key} AS value | ||||||
|         FROM  |         FROM  | ||||||
|           ${table}  |           ${table}  | ||||||
|           LEFT JOIN sync ON sync.entityName = '${table}' AND entityId = ${key}  |           LEFT JOIN sync ON sync.entityName = '${table}' AND entityId = ${key}  | ||||||
|         WHERE  |         WHERE  | ||||||
|           sync.id IS NULL AND ` + (table === 'options' ? 'isSynced = 1' : '1'),
 |           sync.id IS NULL AND ` + (table === 'options' ? 'isSynced = 1' : '1'),
 | ||||||
|         `Missing sync records for ${key} in table ${table}`, errorList); |         `Missing sync records for ${key} in table ${table}`); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|         SELECT  |         SELECT  | ||||||
|           entityId  |           entityId AS value | ||||||
|         FROM  |         FROM  | ||||||
|           sync  |           sync  | ||||||
|           LEFT JOIN ${table} ON entityId = ${key}  |           LEFT JOIN ${table} ON entityId = ${key}  | ||||||
|         WHERE  |         WHERE  | ||||||
|           sync.entityName = '${table}'  |           sync.entityName = '${table}'  | ||||||
|           AND ${key} IS NULL`,
 |           AND ${key} IS NULL`,
 | ||||||
|         `Missing ${table} records for existing sync rows`, errorList); |         `Missing ${table} records for existing sync rows`); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function fixEmptyRelationTargets(errorList) { | async function fixEmptyRelationTargets() { | ||||||
|     const emptyRelations = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'relation' AND value = ''"); |     const emptyRelations = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'relation' AND value = ''"); | ||||||
| 
 | 
 | ||||||
|     for (const relation of emptyRelations) { |     for (const relation of emptyRelations) { | ||||||
|         relation.isDeleted = true; |         relation.isDeleted = true; | ||||||
|         await relation.save(); |         await relation.save(); | ||||||
| 
 | 
 | ||||||
|         errorList.push(`Relation ${relation.attributeId} of name "${relation.name} has empty target. Autofixed.`); |         log.error(`Relation ${relation.attributeId} of name "${relation.name} has empty target. Autofixed.`); | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function runAllChecks() { | async function checkMissingBranches() { | ||||||
|     const errorList = []; |     const notes = await runCheck(true, ` | ||||||
| 
 |  | ||||||
|     await runCheck(` |  | ||||||
|           SELECT  |           SELECT  | ||||||
|             noteId  |             noteId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             notes  |             notes  | ||||||
|             LEFT JOIN branches USING(noteId)  |             LEFT JOIN branches USING(noteId)  | ||||||
|           WHERE  |           WHERE  | ||||||
|             noteId != 'root'  |             noteId != 'root'  | ||||||
|             AND branches.branchId IS NULL`,
 |             AND branches.branchId IS NULL`,
 | ||||||
|         "Missing branches records for following note IDs", errorList); |         "Missing branches for following note IDs"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     for (const {value: noteId} of notes) { | ||||||
|  |         const branch = await new Branch({ | ||||||
|  |             parentNoteId: 'root', | ||||||
|  |             noteId: noteId, | ||||||
|  |             prefix: 'recovered' | ||||||
|  |         }).save(); | ||||||
|  | 
 | ||||||
|  |         log.info(`Created missing branch ${branch.branchId} for note ${noteId}`); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function checkMissingNotes() { | ||||||
|  |     const records = await runCheck(true, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             branchId || ' > ' || branches.noteId  |             branchId || ' > ' || branches.noteId AS value, branchId, branches.noteId | ||||||
|           FROM  |           FROM  | ||||||
|             branches  |             branches  | ||||||
|             LEFT JOIN notes USING(noteId)  |             LEFT JOIN notes USING(noteId)  | ||||||
|           WHERE  |           WHERE  | ||||||
|             notes.noteId IS NULL`,
 |             notes.noteId IS NULL`,
 | ||||||
|         "Missing notes records for following branch ID > note ID", errorList); |         "Missing notes records for following branch ID > note ID"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     for (const {branchId, noteId} of records) { | ||||||
|  |         const branch = await repository.getBranch(branchId); | ||||||
|  |         branch.isDeleted = true; | ||||||
|  |         await branch.save(); | ||||||
|  | 
 | ||||||
|  |         log.info(`Removed ${branchId} because it pointed to the missing ${noteId}`); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function checkAllDeletedNotesBranchesAreDeleted() { | ||||||
|  |     const branches = await runCheck(true, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             branchId  |             branchId AS value, branchId, noteId | ||||||
|           FROM  |           FROM  | ||||||
|             branches  |             branches  | ||||||
|             JOIN notes USING(noteId)  |             JOIN notes USING(noteId)  | ||||||
|           WHERE  |           WHERE  | ||||||
|             notes.isDeleted = 1  |             notes.isDeleted = 1  | ||||||
|             AND branches.isDeleted = 0`,
 |             AND branches.isDeleted = 0`,
 | ||||||
|         "Branch is not deleted even though main note is deleted for following branch IDs", errorList); |         "Branch is not deleted even though main note is deleted for following branch IDs"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     for (const {branchId, noteId} of branches) { | ||||||
|  |         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 checkAllNotesShouldHaveUndeletedBranch() { | ||||||
|  |     // we do extra JOIN to eliminate orphan notes without branches (which are reported separately)
 | ||||||
|  |     const notes = await runCheck(true, ` | ||||||
|           SELECT |           SELECT | ||||||
|             child.branchId |             DISTINCT noteId AS value | ||||||
|  |           FROM | ||||||
|  |             notes | ||||||
|  |             JOIN branches USING(noteId) | ||||||
|  |           WHERE | ||||||
|  |             (SELECT COUNT(*) FROM branches WHERE notes.noteId = branches.noteId AND branches.isDeleted = 0) = 0 | ||||||
|  |             AND notes.isDeleted = 0 | ||||||
|  |     `, 'No undeleted branches for note IDs');
 | ||||||
|  | 
 | ||||||
|  |     for (const {value: noteId} of notes) { | ||||||
|  |         const branch = await new Branch({ | ||||||
|  |             parentNoteId: 'root', | ||||||
|  |             noteId: noteId, | ||||||
|  |             prefix: 'recovered' | ||||||
|  |         }).save(); | ||||||
|  | 
 | ||||||
|  |         log.info(`Created missing branch ${branch.branchId} for note ${noteId}`); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async function runAllChecks() { | ||||||
|  |     outstandingConsistencyErrors = false; | ||||||
|  | 
 | ||||||
|  |     await checkMissingBranches(); | ||||||
|  | 
 | ||||||
|  |     await checkMissingNotes(); | ||||||
|  | 
 | ||||||
|  |     await checkAllDeletedNotesBranchesAreDeleted(); | ||||||
|  | 
 | ||||||
|  |     // FIXME - does this make sense? Specifically branch - branch comparison seems strange
 | ||||||
|  |     await runCheck(false, ` | ||||||
|  |           SELECT  | ||||||
|  |             child.branchId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             branches AS child |             branches AS child | ||||||
|           WHERE  |           WHERE  | ||||||
| @ -146,43 +218,33 @@ async function runAllChecks() { | |||||||
|             AND child.parentNoteId != 'none' |             AND child.parentNoteId != 'none' | ||||||
|             AND (SELECT COUNT(*) FROM branches AS parent WHERE parent.noteId = child.parentNoteId  |             AND (SELECT COUNT(*) FROM branches AS parent WHERE parent.noteId = child.parentNoteId  | ||||||
|                                                                  AND parent.isDeleted = 0) = 0`,
 |                                                                  AND parent.isDeleted = 0) = 0`,
 | ||||||
|         "All parent branches are deleted but child branch is not for these child branch IDs", errorList); |         "All parent branches are deleted but child branch is not for these child branch IDs"); | ||||||
| 
 | 
 | ||||||
|     // we do extra JOIN to eliminate orphan notes without branches (which are reported separately)
 |     await checkAllNotesShouldHaveUndeletedBranch(); | ||||||
|     await runCheck(` |  | ||||||
|           SELECT |  | ||||||
|             DISTINCT noteId |  | ||||||
|           FROM |  | ||||||
|             notes |  | ||||||
|             JOIN branches USING(noteId) |  | ||||||
|           WHERE |  | ||||||
|             (SELECT COUNT(*) FROM branches WHERE notes.noteId = branches.noteId AND branches.isDeleted = 0) = 0 |  | ||||||
|             AND notes.isDeleted = 0 |  | ||||||
|     `, 'No undeleted branches for note IDs', errorList);
 |  | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             child.parentNoteId || ' > ' || child.noteId  |             child.parentNoteId || ' > ' || child.noteId AS value | ||||||
|           FROM branches  |           FROM branches  | ||||||
|             AS child  |             AS child  | ||||||
|             LEFT JOIN branches AS parent ON parent.noteId = child.parentNoteId  |             LEFT JOIN branches AS parent ON parent.noteId = child.parentNoteId  | ||||||
|           WHERE  |           WHERE  | ||||||
|             parent.noteId IS NULL  |             parent.noteId IS NULL  | ||||||
|             AND child.parentNoteId != 'none'`,
 |             AND child.parentNoteId != 'none'`,
 | ||||||
|         "Not existing parent in the following parent > child relations", errorList); |         "Not existing parent in the following parent > child relations"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             noteRevisionId || ' > ' || note_revisions.noteId  |             noteRevisionId || ' > ' || note_revisions.noteId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             note_revisions LEFT JOIN notes USING(noteId)  |             note_revisions LEFT JOIN notes USING(noteId)  | ||||||
|           WHERE  |           WHERE  | ||||||
|             notes.noteId IS NULL`,
 |             notes.noteId IS NULL`,
 | ||||||
|         "Missing notes records for following note revision ID > note ID", errorList); |         "Missing notes records for following note revision ID > note ID"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             branches.parentNoteId || ' > ' || branches.noteId  |             branches.parentNoteId || ' > ' || branches.noteId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             branches  |             branches  | ||||||
|           WHERE  |           WHERE  | ||||||
| @ -192,11 +254,11 @@ async function runAllChecks() { | |||||||
|             branches.noteId |             branches.noteId | ||||||
|           HAVING  |           HAVING  | ||||||
|             COUNT(*) > 1`,
 |             COUNT(*) > 1`,
 | ||||||
|         "Duplicate undeleted parent note <-> note relationship - parent note ID > note ID", errorList); |         "Duplicate undeleted parent note <-> note relationship - parent note ID > note ID"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             noteId |             noteId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             notes |             notes | ||||||
|           WHERE  |           WHERE  | ||||||
| @ -207,33 +269,33 @@ async function runAllChecks() { | |||||||
|             AND type != 'image'  |             AND type != 'image'  | ||||||
|             AND type != 'search'  |             AND type != 'search'  | ||||||
|             AND type != 'relation-map'`,
 |             AND type != 'relation-map'`,
 | ||||||
|         "Note has invalid type", errorList); |         "Note has invalid type"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT |           SELECT | ||||||
|             noteId |             noteId AS value | ||||||
|           FROM |           FROM | ||||||
|             notes |             notes | ||||||
|           WHERE |           WHERE | ||||||
|             isDeleted = 0 |             isDeleted = 0 | ||||||
|             AND content IS NULL`,
 |             AND content IS NULL`,
 | ||||||
|         "Note content is null even though it is not deleted", errorList); |         "Note content is null even though it is not deleted"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             parentNoteId |             parentNoteId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             branches |             branches | ||||||
|             JOIN notes ON notes.noteId = branches.parentNoteId |             JOIN notes ON notes.noteId = branches.parentNoteId | ||||||
|           WHERE  |           WHERE  | ||||||
|             type == 'search'`,
 |             type == 'search'`,
 | ||||||
|         "Search note has children", errorList); |         "Search note has children"); | ||||||
| 
 | 
 | ||||||
|     await fixEmptyRelationTargets(errorList); |     await fixEmptyRelationTargets(); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             attributeId |             attributeId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             attributes |             attributes | ||||||
|           WHERE  |           WHERE  | ||||||
| @ -241,22 +303,22 @@ async function runAllChecks() { | |||||||
|             AND type != 'label-definition'  |             AND type != 'label-definition'  | ||||||
|             AND type != 'relation' |             AND type != 'relation' | ||||||
|             AND type != 'relation-definition'`,
 |             AND type != 'relation-definition'`,
 | ||||||
|         "Attribute has invalid type", errorList); |         "Attribute has invalid type"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false,` | ||||||
|           SELECT  |           SELECT  | ||||||
|             attributeId |             attributeId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             attributes |             attributes | ||||||
|             LEFT JOIN notes ON attributes.noteId = notes.noteId AND notes.isDeleted = 0 |             LEFT JOIN notes ON attributes.noteId = notes.noteId AND notes.isDeleted = 0 | ||||||
|           WHERE |           WHERE | ||||||
|             attributes.isDeleted = 0 |             attributes.isDeleted = 0 | ||||||
|             AND notes.noteId IS NULL`,
 |             AND notes.noteId IS NULL`,
 | ||||||
|         "Attribute reference to the owning note is broken", errorList); |         "Attribute reference to the owning note is broken"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT |           SELECT | ||||||
|             attributeId |             attributeId AS value | ||||||
|           FROM |           FROM | ||||||
|             attributes |             attributes | ||||||
|             LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId AND targetNote.isDeleted = 0 |             LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId AND targetNote.isDeleted = 0 | ||||||
| @ -264,22 +326,22 @@ async function runAllChecks() { | |||||||
|             attributes.type = 'relation' |             attributes.type = 'relation' | ||||||
|             AND attributes.isDeleted = 0 |             AND attributes.isDeleted = 0 | ||||||
|             AND targetNote.noteId IS NULL`,
 |             AND targetNote.noteId IS NULL`,
 | ||||||
|         "Relation reference to the target note is broken", errorList); |         "Relation reference to the target note is broken"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             linkId |             linkId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             links |             links | ||||||
|           WHERE  |           WHERE  | ||||||
|             type != 'image' |             type != 'image' | ||||||
|             AND type != 'hyper' |             AND type != 'hyper' | ||||||
|             AND type != 'relation-map'`,
 |             AND type != 'relation-map'`,
 | ||||||
|         "Link type is invalid", errorList); |         "Link type is invalid"); | ||||||
| 
 | 
 | ||||||
|     await runCheck(` |     await runCheck(false, ` | ||||||
|           SELECT  |           SELECT  | ||||||
|             linkId |             linkId AS value | ||||||
|           FROM  |           FROM  | ||||||
|             links |             links | ||||||
|             LEFT JOIN notes AS sourceNote ON sourceNote.noteId = links.noteId AND sourceNote.isDeleted = 0 |             LEFT JOIN notes AS sourceNote ON sourceNote.noteId = links.noteId AND sourceNote.isDeleted = 0 | ||||||
| @ -288,39 +350,39 @@ async function runAllChecks() { | |||||||
|             links.isDeleted = 0 |             links.isDeleted = 0 | ||||||
|             AND (sourceNote.noteId IS NULL |             AND (sourceNote.noteId IS NULL | ||||||
|                  OR targetNote.noteId IS NULL)`,
 |                  OR targetNote.noteId IS NULL)`,
 | ||||||
|         "Link to source/target note link is broken", errorList); |         "Link to source/target note link is broken"); | ||||||
| 
 | 
 | ||||||
|     await runSyncRowChecks("notes", "noteId", errorList); |     await runSyncRowChecks("notes", "noteId"); | ||||||
|     await runSyncRowChecks("note_revisions", "noteRevisionId", errorList); |     await runSyncRowChecks("note_revisions", "noteRevisionId"); | ||||||
|     await runSyncRowChecks("branches", "branchId", errorList); |     await runSyncRowChecks("branches", "branchId"); | ||||||
|     await runSyncRowChecks("recent_notes", "branchId", errorList); |     await runSyncRowChecks("recent_notes", "branchId"); | ||||||
|     await runSyncRowChecks("attributes", "attributeId", errorList); |     await runSyncRowChecks("attributes", "attributeId"); | ||||||
|     await runSyncRowChecks("api_tokens", "apiTokenId", errorList); |     await runSyncRowChecks("api_tokens", "apiTokenId"); | ||||||
|     await runSyncRowChecks("options", "name", errorList); |     await runSyncRowChecks("options", "name"); | ||||||
| 
 | 
 | ||||||
|     if (errorList.length === 0) { |     if (outstandingConsistencyErrors) { | ||||||
|         // we run this only if basic checks passed since this assumes basic data consistency
 |         // we run this only if basic checks passed since this assumes basic data consistency
 | ||||||
| 
 | 
 | ||||||
|         await checkTreeCycles(errorList); |         await checkTreeCycles(); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     return errorList; |     return !outstandingConsistencyErrors; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| async function runChecks() { | async function runChecks() { | ||||||
|     let errorList; |  | ||||||
|     let elapsedTimeMs; |     let elapsedTimeMs; | ||||||
|  |     let dbConsistent; | ||||||
| 
 | 
 | ||||||
|     await syncMutexService.doExclusively(async () => { |     await syncMutexService.doExclusively(async () => { | ||||||
|         const startTime = new Date(); |         const startTime = new Date(); | ||||||
| 
 | 
 | ||||||
|         errorList = await runAllChecks(); |         dbConsistent = await runAllChecks(); | ||||||
| 
 | 
 | ||||||
|         elapsedTimeMs = new Date().getTime() - startTime.getTime(); |         elapsedTimeMs = new Date().getTime() - startTime.getTime(); | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     if (errorList.length > 0) { |     if (!dbConsistent) { | ||||||
|         log.info(`Consistency checks failed (took ${elapsedTimeMs}ms) with these errors: ` + JSON.stringify(errorList)); |         log.info(`Consistency checks failed (took ${elapsedTimeMs}ms)`); | ||||||
| 
 | 
 | ||||||
|         messagingService.sendMessageToAllClients({type: 'consistency-checks-failed'}); |         messagingService.sendMessageToAllClients({type: 'consistency-checks-failed'}); | ||||||
|     } |     } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 azivner
						azivner