mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-10-29 03:23:25 +08:00 
			
		
		
		
	added non-autoFix variants to fixer methods
This commit is contained in:
		
							parent
							
								
									466a4802b6
								
							
						
					
					
						commit
						1fddd6f318
					
				| @ -30,9 +30,11 @@ async function findAndFixIssues(query, fixerCb) { | |||||||
|     const results = await sql.getRows(query); |     const results = await sql.getRows(query); | ||||||
| 
 | 
 | ||||||
|     for (const res of results) { |     for (const res of results) { | ||||||
|         if (await optionsService.getOptionInt('autoFixConsistencyIssues')) { |         const autoFix = await optionsService.getOptionInt('autoFixConsistencyIssues'); | ||||||
|             await fixerCb(res); |  | ||||||
| 
 | 
 | ||||||
|  |         await fixerCb(res, autoFix); | ||||||
|  | 
 | ||||||
|  |         if (autoFix) { | ||||||
|             fixedIssues = true; |             fixedIssues = true; | ||||||
|         } |         } | ||||||
|         else { |         else { | ||||||
| @ -143,12 +145,17 @@ async function findExistencyIssues() { | |||||||
|           WHERE |           WHERE | ||||||
|             notes.isDeleted = 1 |             notes.isDeleted = 1 | ||||||
|             AND branches.isDeleted = 0`,
 |             AND branches.isDeleted = 0`,
 | ||||||
|         async ({branchId, noteId}) => { |         async ({branchId, noteId}, autoFix) => { | ||||||
|  |             if (autoFix) { | ||||||
|                 const branch = await repository.getBranch(branchId); |                 const branch = await repository.getBranch(branchId); | ||||||
|                 branch.isDeleted = true; |                 branch.isDeleted = true; | ||||||
|                 await branch.save(); |                 await branch.save(); | ||||||
| 
 | 
 | ||||||
|                 logFix(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`); |                 logFix(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 logError(`Branch ${branchId} is not deleted even though associated note ${noteId} is deleted.`) | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     await findAndFixIssues(` |     await findAndFixIssues(` | ||||||
| @ -160,12 +167,17 @@ async function findExistencyIssues() { | |||||||
|       WHERE |       WHERE | ||||||
|         parentNote.isDeleted = 1 |         parentNote.isDeleted = 1 | ||||||
|         AND branches.isDeleted = 0 |         AND branches.isDeleted = 0 | ||||||
|     `, async ({branchId, parentNoteId}) => {
 |     `, async ({branchId, parentNoteId}, autoFix) => {
 | ||||||
|  |         if (autoFix) { | ||||||
|             const branch = await repository.getBranch(branchId); |             const branch = await repository.getBranch(branchId); | ||||||
|             branch.isDeleted = true; |             branch.isDeleted = true; | ||||||
|             await branch.save(); |             await branch.save(); | ||||||
| 
 | 
 | ||||||
|             logFix(`Branch ${branchId} has been deleted since associated parent note ${parentNoteId} is deleted.`); |             logFix(`Branch ${branchId} has been deleted since associated parent note ${parentNoteId} is deleted.`); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             logError(`Branch ${branchId} is not deleted even though associated parent note ${parentNoteId} is deleted.`) | ||||||
|  |         } | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     await findAndFixIssues(` |     await findAndFixIssues(` | ||||||
| @ -177,7 +189,8 @@ async function findExistencyIssues() { | |||||||
|       WHERE |       WHERE | ||||||
|         notes.isDeleted = 0 |         notes.isDeleted = 0 | ||||||
|         AND branches.branchId IS NULL |         AND branches.branchId IS NULL | ||||||
|     `, async ({noteId}) => {
 |     `, async ({noteId}, autoFix) => {
 | ||||||
|  |         if (autoFix) { | ||||||
|             const branch = await new Branch({ |             const branch = await new Branch({ | ||||||
|                 parentNoteId: 'root', |                 parentNoteId: 'root', | ||||||
|                 noteId: noteId, |                 noteId: noteId, | ||||||
| @ -185,6 +198,10 @@ async function findExistencyIssues() { | |||||||
|             }).save(); |             }).save(); | ||||||
| 
 | 
 | ||||||
|             logFix(`Created missing branch ${branch.branchId} for note ${noteId}`); |             logFix(`Created missing branch ${branch.branchId} for note ${noteId}`); | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             logError(`No undeleted branch found for note ${noteId}`); | ||||||
|  |         } | ||||||
|     }); |     }); | ||||||
| 
 | 
 | ||||||
|     // there should be a unique relationship between note and its parent
 |     // there should be a unique relationship between note and its parent
 | ||||||
| @ -200,8 +217,14 @@ async function findExistencyIssues() { | |||||||
|         branches.noteId |         branches.noteId | ||||||
|       HAVING |       HAVING | ||||||
|         COUNT(*) > 1`,
 |         COUNT(*) > 1`,
 | ||||||
|     async ({noteId, parentNoteId}) => { |     async ({noteId, parentNoteId}, autoFix) => { | ||||||
|         const branches = await repository.getEntities(`SELECT * FROM branches WHERE noteId = ? and parentNoteId = ? and isDeleted = 1`, [noteId, parentNoteId]); |         if (autoFix) { | ||||||
|  |             const branches = await repository.getEntities( | ||||||
|  |                 `SELECT *
 | ||||||
|  |                        FROM branches | ||||||
|  |                        WHERE noteId = ? | ||||||
|  |                          and parentNoteId = ? | ||||||
|  |                          and isDeleted = 1`, [noteId, parentNoteId]);
 | ||||||
| 
 | 
 | ||||||
|             // it's not necessarily "original" branch, it's just the only one which will survive
 |             // it's not necessarily "original" branch, it's just the only one which will survive
 | ||||||
|             const origBranch = branches[0]; |             const origBranch = branches[0]; | ||||||
| @ -213,6 +236,10 @@ async function findExistencyIssues() { | |||||||
| 
 | 
 | ||||||
|                 logFix(`Removing branch ${branch.branchId} since it's parent-child duplicate of branch ${origBranch.branchId}`); |                 logFix(`Removing branch ${branch.branchId} since it's parent-child duplicate of branch ${origBranch.branchId}`); | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             logError(`Duplicate branches for note ${noteId} and parent ${parentNoteId}`); | ||||||
|  |         } | ||||||
|     }); |     }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -269,12 +296,17 @@ async function findLogicIssues() { | |||||||
|             isDeleted = 0  |             isDeleted = 0  | ||||||
|             AND type = 'relation'  |             AND type = 'relation'  | ||||||
|             AND value = ''`,
 |             AND value = ''`,
 | ||||||
|         async ({attributeId}) => { |         async ({attributeId}, autoFix) => { | ||||||
|  |             if (autoFix) { | ||||||
|                 const relation = await repository.getAttribute(attributeId); |                 const relation = await repository.getAttribute(attributeId); | ||||||
|                 relation.isDeleted = true; |                 relation.isDeleted = true; | ||||||
|                 await relation.save(); |                 await relation.save(); | ||||||
| 
 | 
 | ||||||
|                 logFix(`Removed relation ${relation.attributeId} of name "${relation.name} with empty target.`); |                 logFix(`Removed relation ${relation.attributeId} of name "${relation.name} with empty target.`); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 logError(`Relation ${attributeId} has empty target.`); | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     await findIssues(` |     await findIssues(` | ||||||
| @ -300,12 +332,17 @@ async function findLogicIssues() { | |||||||
|           WHERE |           WHERE | ||||||
|             attributes.isDeleted = 0 |             attributes.isDeleted = 0 | ||||||
|             AND notes.isDeleted = 1`,
 |             AND notes.isDeleted = 1`,
 | ||||||
|         async ({attributeId, noteId}) => { |         async ({attributeId, noteId}, autoFix) => { | ||||||
|  |             if (autoFix) { | ||||||
|                 const attribute = await repository.getAttribute(attributeId); |                 const attribute = await repository.getAttribute(attributeId); | ||||||
|                 attribute.isDeleted = true; |                 attribute.isDeleted = true; | ||||||
|                 await attribute.save(); |                 await attribute.save(); | ||||||
| 
 | 
 | ||||||
|                 logFix(`Removed attribute ${attributeId} because owning note ${noteId} is also deleted.`); |                 logFix(`Removed attribute ${attributeId} because owning note ${noteId} is also deleted.`); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 logError(`Attribute ${attributeId} is not deleted even though owning note ${noteId} is deleted.`); | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     await findAndFixIssues(` |     await findAndFixIssues(` | ||||||
| @ -319,12 +356,17 @@ async function findLogicIssues() { | |||||||
|             attributes.type = 'relation' |             attributes.type = 'relation' | ||||||
|             AND attributes.isDeleted = 0 |             AND attributes.isDeleted = 0 | ||||||
|             AND notes.isDeleted = 1`,
 |             AND notes.isDeleted = 1`,
 | ||||||
|         async ({attributeId, targetNoteId}) => { |         async ({attributeId, targetNoteId}, autoFix) => { | ||||||
|  |             if (autoFix) { | ||||||
|                 const attribute = await repository.getAttribute(attributeId); |                 const attribute = await repository.getAttribute(attributeId); | ||||||
|                 attribute.isDeleted = true; |                 attribute.isDeleted = true; | ||||||
|                 await attribute.save(); |                 await attribute.save(); | ||||||
| 
 | 
 | ||||||
|                 logFix(`Removed attribute ${attributeId} because target note ${targetNoteId} is also deleted.`); |                 logFix(`Removed attribute ${attributeId} because target note ${targetNoteId} is also deleted.`); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 logError(`Attribute ${attributeId} is not deleted even though target note ${targetNoteId} is deleted.`); | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -337,10 +379,15 @@ async function runSyncRowChecks(entityName, key) { | |||||||
|           LEFT JOIN sync ON sync.entityName = '${entityName}' AND entityId = ${key}  |           LEFT JOIN sync ON sync.entityName = '${entityName}' AND entityId = ${key}  | ||||||
|         WHERE  |         WHERE  | ||||||
|           sync.id IS NULL AND ` + (entityName === 'options' ? 'isSynced = 1' : '1'),
 |           sync.id IS NULL AND ` + (entityName === 'options' ? 'isSynced = 1' : '1'),
 | ||||||
|         async ({entityId}) => { |         async ({entityId}, autoFix) => { | ||||||
|  |             if (autoFix) { | ||||||
|                 await syncTableService.addEntitySync(entityName, entityId); |                 await syncTableService.addEntitySync(entityName, entityId); | ||||||
| 
 | 
 | ||||||
|             logFix(`Created missing sync record entityName=${entityName}, entityId=${entityId}`); |                 logFix(`Created missing sync record for entityName=${entityName}, entityId=${entityId}`); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 logError(`Missing sync record for entityName=${entityName}, entityId=${entityId}`); | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
| 
 | 
 | ||||||
|     await findAndFixIssues(` |     await findAndFixIssues(` | ||||||
| @ -352,11 +399,15 @@ async function runSyncRowChecks(entityName, key) { | |||||||
|         WHERE  |         WHERE  | ||||||
|           sync.entityName = '${entityName}'  |           sync.entityName = '${entityName}'  | ||||||
|           AND ${key} IS NULL`,
 |           AND ${key} IS NULL`,
 | ||||||
|         async ({id, entityId}) => { |         async ({id, entityId}, autoFix) => { | ||||||
| 
 |             if (autoFix) { | ||||||
|                 await sql.execute("DELETE FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]); |                 await sql.execute("DELETE FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]); | ||||||
| 
 | 
 | ||||||
|                 logFix(`Deleted extra sync record id=${id}, entityName=${entityName}, entityId=${entityId}`); |                 logFix(`Deleted extra sync record id=${id}, entityName=${entityName}, entityId=${entityId}`); | ||||||
|  |             } | ||||||
|  |             else { | ||||||
|  |                 logError(`Unrecognized sync record id=${id}, entityName=${entityName}, entityId=${entityId}`); | ||||||
|  |             } | ||||||
|         }); |         }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 zadam
						zadam