trilium/src/services/consistency_checks.js

269 lines
8 KiB
JavaScript
Raw Normal View History

2017-12-15 11:16:26 +08:00
"use strict";
const sql = require('./sql');
const log = require('./log');
const messaging = require('./messaging');
const sync_mutex = require('./sync_mutex');
2018-01-07 04:56:00 +08:00
const utils = require('./utils');
2017-12-15 11:16:26 +08:00
async function runCheck(query, errorText, errorList) {
2018-01-07 04:56:00 +08:00
utils.assertArguments(query, errorText, errorList);
const result = await sql.getColumn(query);
2017-12-15 11:16:26 +08:00
if (result.length > 0) {
2017-12-24 02:16:18 +08:00
const resultText = result.map(val => "'" + val + "'").join(', ');
const err = errorText + ": " + resultText;
2017-12-15 11:16:26 +08:00
errorList.push(err);
log.error(err);
}
}
2018-01-02 08:41:22 +08:00
async function checkTreeCycles(errorList) {
const childToParents = {};
const rows = await sql.getRows("SELECT noteId, parentNoteId FROM note_tree 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
if (!childToParents[childNoteId]) {
childToParents[childNoteId] = [];
}
childToParents[childNoteId].push(parentNoteId);
}
function checkTreeCycle(noteId, path, errorList) {
if (noteId === 'root') {
return;
}
for (const parentNoteId of childToParents[noteId]) {
if (path.includes(parentNoteId)) {
errorList.push(`Tree cycle detected at parent-child relationship: ${parentNoteId} - ${noteId}, whole path: ${path}`);
}
else {
const newPath = path.slice();
newPath.push(noteId);
checkTreeCycle(parentNoteId, newPath, errorList);
}
}
}
const noteIds = Object.keys(childToParents);
for (const noteId of noteIds) {
checkTreeCycle(noteId, [], errorList);
}
}
async function runSyncRowChecks(table, key, errorList) {
await runCheck(`
SELECT
${key}
FROM
${table}
2018-01-29 08:30:14 +08:00
LEFT JOIN sync ON sync.entityName = '${table}' AND entityId = ${key}
WHERE
sync.id IS NULL`,
`Missing sync records for ${key} in table ${table}`, errorList);
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
entityId
FROM
sync
2018-01-29 08:30:14 +08:00
LEFT JOIN ${table} ON entityId = ${key}
WHERE
2018-01-29 08:30:14 +08:00
sync.entityName = '${table}'
AND ${key} IS NULL`,
`Missing ${table} records for existing sync rows`, errorList);
2017-12-15 11:16:26 +08:00
}
async function runAllChecks() {
2017-12-15 11:16:26 +08:00
const errorList = [];
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
noteId
FROM
notes
LEFT JOIN note_tree USING(noteId)
WHERE
2018-01-29 08:30:14 +08:00
noteId != 'root'
AND note_tree.noteTreeId IS NULL`,
"Missing note_tree records for following note IDs", errorList);
2017-12-15 11:16:26 +08:00
await runCheck(`
SELECT
noteTreeId || ' > ' || note_tree.noteId
FROM
note_tree
2018-01-29 08:30:14 +08:00
LEFT JOIN notes USING(noteId)
WHERE
2018-01-29 08:30:14 +08:00
notes.noteId IS NULL`,
"Missing notes records for following note tree ID > note ID", errorList);
2017-12-15 11:16:26 +08:00
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
noteTreeId
FROM
note_tree
2018-01-29 08:30:14 +08:00
JOIN notes USING(noteId)
WHERE
2018-01-29 08:30:14 +08:00
notes.isDeleted = 1
AND note_tree.isDeleted = 0`,
2017-12-15 11:16:26 +08:00
"Note tree is not deleted even though main note is deleted for following note tree IDs", errorList);
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
child.noteTreeId
FROM
note_tree AS child
WHERE
2018-01-29 08:30:14 +08:00
child.isDeleted = 0
AND child.parentNoteId != 'root'
AND (SELECT COUNT(*) FROM note_tree AS parent WHERE parent.noteId = child.parentNoteId
2018-01-29 08:30:14 +08:00
AND parent.isDeleted = 0) = 0`,
2018-01-15 10:39:21 +08:00
"All parent note trees are deleted but child note tree is not for these child note tree IDs", errorList);
// we do extra JOIN to eliminate orphan notes without note tree (which are reported separately)
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
DISTINCT noteId
FROM
notes
JOIN note_tree USING(noteId)
WHERE
(SELECT COUNT(*) FROM note_tree WHERE notes.noteId = note_tree.noteId AND note_tree.isDeleted = 0) = 0
2018-01-29 08:30:14 +08:00
AND notes.isDeleted = 0
2018-01-07 04:56:00 +08:00
`, 'No undeleted note trees for note IDs', errorList);
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
child.parentNoteId || ' > ' || child.noteId
FROM note_tree
AS child
LEFT JOIN note_tree AS parent ON parent.noteId = child.parentNoteId
WHERE
2018-01-29 08:30:14 +08:00
parent.noteId IS NULL
AND child.parentNoteId != 'root'`,
2017-12-15 11:16:26 +08:00
"Not existing parent in the following parent > child relations", errorList);
await runCheck(`
SELECT
noteRevisionId || ' > ' || note_revisions.noteId
FROM
note_revisions LEFT JOIN notes USING(noteId)
WHERE
2018-01-29 08:30:14 +08:00
notes.noteId IS NULL`,
2017-12-15 11:16:26 +08:00
"Missing notes records for following note history ID > note ID", errorList);
await runCheck(`
SELECT
note_tree.parentNoteId || ' > ' || note_tree.noteId
FROM
note_tree
WHERE
note_tree.isDeleted = 0
GROUP BY
note_tree.parentNoteId,
note_tree.noteId
HAVING
COUNT(*) > 1`,
"Duplicate undeleted parent note <-> note relationship - parent note ID > note ID", errorList);
2018-01-07 22:22:55 +08:00
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
images.imageId
2018-01-07 22:22:55 +08:00
FROM
images
LEFT JOIN note_images ON note_images.imageId = images.imageId
2018-01-07 22:22:55 +08:00
WHERE
note_images.noteImageId IS NULL`,
2018-01-07 22:22:55 +08:00
"Image with no note relation", errorList);
2018-01-08 09:16:31 +08:00
await runCheck(`
SELECT
note_images.noteImageId
2018-01-08 09:16:31 +08:00
FROM
note_images
2018-01-29 08:30:14 +08:00
JOIN images USING(imageId)
2018-01-08 09:16:31 +08:00
WHERE
note_images.isDeleted = 0
2018-01-29 08:30:14 +08:00
AND images.isDeleted = 1`,
"Note image is not deleted while image is deleted for noteImageId", errorList);
2018-01-08 09:16:31 +08:00
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
noteId
FROM
notes
WHERE
2018-01-29 08:30:14 +08:00
isDeleted = 0
AND (title IS NULL OR content IS NULL)`,
"Note has null title or text", errorList);
2018-01-21 10:56:03 +08:00
await runCheck(`
SELECT
2018-01-29 08:30:14 +08:00
noteId
2018-01-21 10:56:03 +08:00
FROM
notes
WHERE
2018-03-11 22:49:22 +08:00
type != 'text' AND type != 'code' AND type != 'render' AND type != 'file' AND type != 'search'`,
2018-01-21 10:56:03 +08:00
"Note has invalid type", errorList);
2018-01-29 08:30:14 +08:00
await runSyncRowChecks("notes", "noteId", errorList);
await runSyncRowChecks("note_revisions", "noteRevisionId", errorList);
await runSyncRowChecks("note_tree", "noteTreeId", errorList);
2018-01-29 08:30:14 +08:00
await runSyncRowChecks("recent_notes", "noteTreeId", errorList);
await runSyncRowChecks("images", "imageId", errorList);
await runSyncRowChecks("note_images", "noteImageId", errorList);
2018-02-11 13:18:59 +08:00
await runSyncRowChecks("attributes", "attributeId", errorList);
await runSyncRowChecks("api_tokens", "apiTokenId", errorList);
2018-01-02 08:41:22 +08:00
if (errorList.length === 0) {
// we run this only if basic checks passed since this assumes basic data consistency
await checkTreeCycles(errorList);
}
return errorList;
}
async function runChecks() {
let errorList;
let elapsedTimeMs;
await sync_mutex.doExclusively(async () => {
const startTime = new Date();
errorList = await runAllChecks();
elapsedTimeMs = new Date().getTime() - startTime.getTime();
});
if (errorList.length > 0) {
log.info(`Consistency checks failed (took ${elapsedTimeMs}ms) with these errors: ` + JSON.stringify(errorList));
2018-01-02 08:41:22 +08:00
messaging.sendMessageToAllClients({type: 'consistency-checks-failed'});
}
else {
log.info(`All consistency checks passed (took ${elapsedTimeMs}ms)`);
}
2017-12-15 11:16:26 +08:00
}
sql.dbReady.then(() => {
setInterval(runChecks, 60 * 60 * 1000);
// kickoff backup immediately
setTimeout(runChecks, 10000);
2017-12-15 11:16:26 +08:00
});
module.exports = {
runChecks
};