trilium/src/services/consistency_checks.js

434 lines
13 KiB
JavaScript
Raw Normal View History

2017-12-15 11:16:26 +08:00
"use strict";
const sql = require('./sql');
const sqlInit = require('./sql_init');
2017-12-15 11:16:26 +08:00
const log = require('./log');
const messagingService = require('./messaging');
const syncMutexService = require('./sync_mutex');
const repository = require('./repository');
const cls = require('./cls');
const syncTableService = require('./sync_table');
const Branch = require('../entities/branch');
2017-12-15 11:16:26 +08:00
let outstandingConsistencyErrors = false;
2019-02-02 16:26:57 +08:00
let fixedIssues = false;
2017-12-15 11:16:26 +08:00
async function findIssues(query, errorCb) {
const results = await sql.getRows(query);
2017-12-24 02:16:18 +08:00
for (const res of results) {
2019-02-02 16:26:57 +08:00
logError("Consistency error: " + errorCb(res));
2017-12-15 11:16:26 +08:00
outstandingConsistencyErrors = true;
2017-12-15 11:16:26 +08:00
}
return results;
2017-12-15 11:16:26 +08:00
}
async function findAndFixIssues(query, fixerCb) {
const results = await sql.getRows(query);
for (const res of results) {
await fixerCb(res);
2019-02-02 16:26:57 +08:00
fixedIssues = true;
}
return results;
}
async function checkTreeCycles() {
2018-01-02 08:41:22 +08:00
const childToParents = {};
2018-03-25 09:39:15 +08:00
const rows = await sql.getRows("SELECT noteId, parentNoteId FROM branches WHERE isDeleted = 0");
2018-01-02 08:41:22 +08:00
for (const row of rows) {
2018-01-29 08:30:14 +08:00
const childNoteId = row.noteId;
const parentNoteId = row.parentNoteId;
2018-01-02 08:41:22 +08:00
2018-10-22 03:37:34 +08:00
childToParents[childNoteId] = childToParents[childNoteId] || [];
2018-01-02 08:41:22 +08:00
childToParents[childNoteId].push(parentNoteId);
}
function checkTreeCycle(noteId, path) {
2018-01-02 08:41:22 +08:00
if (noteId === 'root') {
return;
}
if (!childToParents[noteId] || childToParents[noteId].length === 0) {
2019-02-02 16:26:57 +08:00
logError(`No parents found for note ${noteId}`);
outstandingConsistencyErrors = true;
return;
}
2018-01-02 08:41:22 +08:00
for (const parentNoteId of childToParents[noteId]) {
if (path.includes(parentNoteId)) {
2019-02-02 16:26:57 +08:00
logError(`Tree cycle detected at parent-child relationship: ${parentNoteId} - ${noteId}, whole path: ${path}`);
outstandingConsistencyErrors = true;
2018-01-02 08:41:22 +08:00
}
else {
const newPath = path.slice();
newPath.push(noteId);
checkTreeCycle(parentNoteId, newPath);
2018-01-02 08:41:22 +08:00
}
}
}
const noteIds = Object.keys(childToParents);
for (const noteId of noteIds) {
checkTreeCycle(noteId, []);
2018-01-02 08:41:22 +08:00
}
2018-10-22 03:37:34 +08:00
if (childToParents['root'].length !== 1 || childToParents['root'][0] !== 'none') {
2019-02-02 16:26:57 +08:00
logError('Incorrect root parent: ' + JSON.stringify(childToParents['root']));
outstandingConsistencyErrors = true;
2018-10-22 03:37:34 +08:00
}
2018-01-02 08:41:22 +08:00
}
async function runSyncRowChecks(entityName, key) {
await findAndFixIssues(`
SELECT
${key} as entityId
FROM
${entityName}
LEFT JOIN sync ON sync.entityName = '${entityName}' AND entityId = ${key}
WHERE
sync.id IS NULL AND ` + (entityName === 'options' ? 'isSynced = 1' : '1'),
async ({entityId}) => {
await syncTableService.addEntitySync(entityName, entityId);
2019-02-02 16:26:57 +08:00
logFix(`Created missing sync record entityName=${entityName}, entityId=${entityId}`);
});
await findAndFixIssues(`
SELECT
id, entityId
FROM
sync
LEFT JOIN ${entityName} ON entityId = ${key}
WHERE
sync.entityName = '${entityName}'
AND ${key} IS NULL`,
async ({id, entityId}) => {
await sql.execute("DELETE FROM sync WHERE entityName = ? AND entityId = ?", [entityName, entityId]);
2019-02-02 16:26:57 +08:00
logFix(`Deleted extra sync record id=${id}, entityName=${entityName}, entityId=${entityId}`);
});
2017-12-15 11:16:26 +08:00
}
async function fixEmptyRelationTargets() {
2018-11-13 06:34:22 +08:00
const emptyRelations = await repository.getEntities("SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'relation' AND value = ''");
for (const relation of emptyRelations) {
relation.isDeleted = true;
await relation.save();
2019-02-02 16:26:57 +08:00
logFix(`Removed relation ${relation.attributeId} of name "${relation.name} with empty target.`);
fixedIssues = true;
}
}
async function runAllChecks() {
outstandingConsistencyErrors = false;
await findAndFixIssues(`
SELECT
noteId
FROM
notes
2019-02-02 16:26:57 +08:00
LEFT JOIN branches USING(noteId)
WHERE
noteId != 'root'
2018-03-25 09:39:15 +08:00
AND branches.branchId IS NULL`,
async ({noteId}) => {
const branch = await new Branch({
parentNoteId: 'root',
noteId: noteId,
prefix: 'recovered'
}).save();
2019-02-02 16:26:57 +08:00
logFix(`Created missing branch ${branch.branchId} for note ${noteId}`);
});
await findAndFixIssues(`
SELECT
branchId,
branches.noteId
FROM
branches
LEFT JOIN notes USING(noteId)
WHERE
notes.noteId IS NULL`,
async ({branchId, noteId}) => {
const branch = await repository.getBranch(branchId);
branch.isDeleted = true;
await branch.save();
2019-02-02 16:26:57 +08:00
logFix(`Removed branch ${branchId} because it pointed to the missing note ${noteId}`);
});
await findAndFixIssues(`
SELECT
branchId, noteId
FROM
branches
2019-02-02 16:26:57 +08:00
JOIN notes USING(noteId)
WHERE
notes.isDeleted = 1
AND branches.isDeleted = 0`,
async ({branchId, noteId}) => {
const branch = await repository.getBranch(branchId);
branch.isDeleted = true;
await branch.save();
2019-02-02 16:26:57 +08:00
logFix(`Branch ${branchId} has been deleted since associated note ${noteId} is deleted.`);
});
// we do extra JOIN to eliminate orphan notes without branches (which are reported separately)
await findAndFixIssues(`
SELECT
DISTINCT noteId
FROM
notes
2019-02-02 16:26:57 +08:00
JOIN branches USING(noteId)
WHERE
(SELECT COUNT(*) FROM branches WHERE notes.noteId = branches.noteId AND branches.isDeleted = 0) = 0
AND notes.isDeleted = 0
`, async ({noteId}) => {
const branch = await new Branch({
parentNoteId: 'root',
noteId: noteId,
prefix: 'recovered'
}).save();
2019-02-02 16:26:57 +08:00
logFix(`Created missing branch ${branch.branchId} for note ${noteId}`);
});
2019-01-19 16:57:51 +08:00
await findIssues(`
SELECT
note_revisions.noteId,
noteRevisionId
FROM
note_revisions LEFT JOIN notes USING(noteId)
WHERE
notes.noteId IS NULL`,
2019-02-02 16:26:57 +08:00
({noteId, noteRevisionId}) => `Missing note ${noteId} for note revision ${noteRevisionId}`);
await findAndFixIssues(`
SELECT
noteId, parentNoteId
FROM
branches
WHERE
branches.isDeleted = 0
GROUP BY
branches.parentNoteId,
branches.noteId
HAVING
COUNT(*) > 1`,
async ({noteId, parentNoteId}) => {
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
const origBranch = branches.get(0);
// delete all but the first branch
for (const branch of branches.slice(1)) {
branch.isDeleted = true;
await branch.save();
2019-02-02 16:26:57 +08:00
logFix(`Removing branch ${branch.branchId} since it's parent-child duplicate of branch ${origBranch.branchId}`);
}
});
await findIssues( `
2018-01-21 10:56:03 +08:00
SELECT
noteId,
type
2018-01-21 10:56:03 +08:00
FROM
notes
WHERE
2018-11-08 17:11:00 +08:00
type != 'text'
AND type != 'code'
AND type != 'render'
AND type != 'file'
AND type != 'image'
AND type != 'search'
AND type != 'relation-map'`,
2019-02-02 16:26:57 +08:00
({noteId, type}) => `Note ${noteId} has invalid type=${type}`);
2018-01-21 10:56:03 +08:00
await findIssues(`
2018-11-20 06:11:36 +08:00
SELECT
noteId
2018-11-20 06:11:36 +08:00
FROM
notes
WHERE
isDeleted = 0
AND content IS NULL`,
2019-02-02 16:26:57 +08:00
({noteId}) => `Note ${noteId} content is null even though it is not deleted`);
2018-11-20 06:11:36 +08:00
await findIssues(`
SELECT
parentNoteId
FROM
2018-03-25 09:39:15 +08:00
branches
JOIN notes ON notes.noteId = branches.parentNoteId
WHERE
type == 'search'`,
2019-02-02 16:26:57 +08:00
({parentNoteId}) => `Search note ${parentNoteId} has children`);
await fixEmptyRelationTargets();
await findIssues(`
SELECT
attributeId,
type
FROM
attributes
WHERE
type != 'label'
AND type != 'label-definition'
AND type != 'relation'
AND type != 'relation-definition'`,
2019-02-02 16:26:57 +08:00
({attributeId, type}) => `Attribute ${attributeId} has invalid type '${type}'`);
await findIssues(`
SELECT
attributeId,
attributes.noteId
FROM
attributes
2019-02-02 16:26:57 +08:00
LEFT JOIN notes ON attributes.noteId = notes.noteId
WHERE
attributes.isDeleted = 0
AND notes.noteId IS NULL`,
2019-02-02 16:26:57 +08:00
({attributeId, noteId}) => `Attribute ${attributeId} reference to the owning note ${noteId} is broken`);
await findAndFixIssues(`
SELECT
attributeId,
attributes.noteId
FROM
attributes
JOIN notes ON attributes.noteId = notes.noteId
WHERE
attributes.isDeleted = 0
AND notes.isDeleted = 1`,
async ({attributeId, noteId}) => {
const attribute = await repository.getAttribute(attributeId);
attribute.isDeleted = true;
await attribute.save();
logFix(`Removed attribute ${attributeId} because owning note ${noteId} is also deleted.`);
});
await findIssues(`
SELECT
attributeId,
value as targetNoteId
FROM
attributes
LEFT JOIN notes AS targetNote ON attributes.value = targetNote.noteId AND targetNote.isDeleted = 0
WHERE
attributes.type = 'relation'
AND targetNote.noteId IS NULL`,
2019-02-02 16:26:57 +08:00
({attributeId, targetNoteId}) => `Relation ${attributeId} reference to the target note ${targetNoteId} is broken`);
await findIssues(`
SELECT
linkId
FROM
links
WHERE
type != 'image'
AND type != 'hyper'
AND type != 'relation-map'`,
2019-02-02 16:26:57 +08:00
({linkId, type}) => `Link ${linkId} has invalid type '${type}'`);
await findIssues(`
SELECT
linkId,
links.targetNoteId
FROM
links
LEFT JOIN notes AS targetNote ON targetNote.noteId = links.targetNoteId AND targetNote.isDeleted = 0
WHERE
links.isDeleted = 0
AND targetNote.noteId IS NULL`,
2019-02-02 16:26:57 +08:00
({linkId, targetNoteId}) => `Link ${linkId} to target note ${targetNoteId} is broken`);
await findIssues(`
SELECT
linkId,
links.noteId AS sourceNoteId
FROM
links
LEFT JOIN notes AS sourceNote ON sourceNote.noteId = links.noteId AND sourceNote.isDeleted = 0
WHERE
links.isDeleted = 0
AND sourceNote.noteId IS NULL`,
2019-02-02 16:26:57 +08:00
({linkId, sourceNoteId}) => `Link ${linkId} to source note ${sourceNoteId} is broken`);
await runSyncRowChecks("notes", "noteId");
await runSyncRowChecks("note_revisions", "noteRevisionId");
await runSyncRowChecks("branches", "branchId");
await runSyncRowChecks("recent_notes", "branchId");
await runSyncRowChecks("attributes", "attributeId");
await runSyncRowChecks("api_tokens", "apiTokenId");
await runSyncRowChecks("options", "name");
if (outstandingConsistencyErrors) {
2018-01-02 08:41:22 +08:00
// we run this only if basic checks passed since this assumes basic data consistency
await checkTreeCycles();
2018-01-02 08:41:22 +08:00
}
return !outstandingConsistencyErrors;
}
async function runChecks() {
let elapsedTimeMs;
let dbConsistent;
await syncMutexService.doExclusively(async () => {
const startTime = new Date();
dbConsistent = await runAllChecks();
elapsedTimeMs = new Date().getTime() - startTime.getTime();
});
2019-02-02 16:26:57 +08:00
if (fixedIssues) {
messagingService.sendMessageToAllClients({ type: 'refresh-tree' });
}
if (!dbConsistent) {
log.info(`Consistency checks failed (took ${elapsedTimeMs}ms)`);
2018-01-02 08:41:22 +08:00
messagingService.sendMessageToAllClients({type: 'consistency-checks-failed'});
}
else {
log.info(`All consistency checks passed (took ${elapsedTimeMs}ms)`);
}
2017-12-15 11:16:26 +08:00
}
2019-02-02 16:26:57 +08:00
function logFix(message) {
log.info("Consistency issue fixed: " + message);
}
function logError(message) {
log.info("Consistency error: " + message);
}
sqlInit.dbReady.then(() => {
setInterval(cls.wrap(runChecks), 60 * 60 * 1000);
2017-12-15 11:16:26 +08:00
// kickoff backup immediately
2019-02-02 16:26:57 +08:00
setTimeout(cls.wrap(runChecks), 0);
2017-12-15 11:16:26 +08:00
});
module.exports = {};