Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
consistency checks fixes
zadam78a2c1753e28
diff --git a/src/entities/note.js b/src/entities/note.jsindex caac1885b2..6c759691a2 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -117,7 +117,6 @@ class Note extends Entity {return JSON.parse(content);}- /** @returns {Promise} */setContent(content) {if (content === null || content === undefined) {throw new Error(`Cannot set null content to note ${this.noteId}`);diff --git a/src/entities/note_revision.js b/src/entities/note_revision.jsindex ae4c2135ef..41ec8159cd 100644--- a/src/entities/note_revision.js+++ b/src/entities/note_revision.js@@ -100,7 +100,6 @@ class NoteRevision extends Entity {}}- /** @returns {Promise} */setContent(content) {// force updating note itself so that utcDateModified is represented correctly even for the contentthis.forcedChange = true;diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.jsindex fc2bd651e4..19588f79b8 100644--- a/src/services/consistency_checks.js+++ b/src/services/consistency_checks.js@@ -24,7 +24,7 @@ class ConsistencyChecks {for (const res of results) {try {- fixerCb(res);+ sql.transactional(() => fixerCb(res));if (this.autoFix) {this.fixedIssues = true;@@ -94,7 +94,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT branchId, branches.noteIdFROM branches- LEFT JOIN notes USING (noteId)+ LEFT JOIN notes USING (noteId)WHERE branches.isDeleted = 0AND notes.noteId IS NULL`,({branchId, noteId}) => {@@ -112,7 +112,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT branchId, branches.noteId AS parentNoteIdFROM branches- LEFT JOIN notes ON notes.noteId = branches.parentNoteId+ LEFT JOIN notes ON notes.noteId = branches.parentNoteIdWHERE branches.isDeleted = 0AND branches.branchId != 'root'AND notes.noteId IS NULL`,@@ -131,7 +131,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT attributeId, attributes.noteIdFROM attributes- LEFT JOIN notes USING (noteId)+ LEFT JOIN notes USING (noteId)WHERE attributes.isDeleted = 0AND notes.noteId IS NULL`,({attributeId, noteId}) => {@@ -149,7 +149,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT attributeId, attributes.value AS noteIdFROM attributes- LEFT JOIN notes ON notes.noteId = attributes.value+ LEFT JOIN notes ON notes.noteId = attributes.valueWHERE attributes.isDeleted = 0AND attributes.type = 'relation'AND notes.noteId IS NULL`,@@ -176,7 +176,7 @@ class ConsistencyChecks {SELECT branchId,noteIdFROM branches- JOIN notes USING (noteId)+ JOIN notes USING (noteId)WHERE notes.isDeleted = 1AND branches.isDeleted = 0`,({branchId, noteId}) => {@@ -195,7 +195,7 @@ class ConsistencyChecks {SELECT branchId,parentNoteIdFROM branches- JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteId+ JOIN notes AS parentNote ON parentNote.noteId = branches.parentNoteIdWHERE parentNote.isDeleted = 1AND branches.isDeleted = 0`, ({branchId, parentNoteId}) => {@@ -213,7 +213,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT DISTINCT notes.noteIdFROM notes- LEFT JOIN branches ON notes.noteId = branches.noteId AND branches.isDeleted = 0+ LEFT JOIN branches ON notes.noteId = branches.noteId AND branches.isDeleted = 0WHERE notes.isDeleted = 0AND branches.branchId IS NULL`, ({noteId}) => {@@ -285,7 +285,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT notes.noteIdFROM notes- LEFT JOIN note_contents USING (noteId)+ LEFT JOIN note_contents USING (noteId)WHERE note_contents.noteId IS NULL`,({noteId}) => {if (this.autoFix) {@@ -316,7 +316,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT noteIdFROM notes- JOIN note_contents USING (noteId)+ JOIN note_contents USING (noteId)WHERE isDeleted = 0AND isProtected = 0AND content IS NULL`,@@ -335,7 +335,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT noteIdFROM notes- JOIN note_contents USING (noteId)+ JOIN note_contents USING (noteId)WHERE isErased = 1AND content IS NOT NULL`,({noteId}) => {@@ -363,7 +363,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT noteId, noteRevisionIdFROM notes- JOIN note_revisions USING (noteId)+ JOIN note_revisions USING (noteId)WHERE notes.isErased = 1AND note_revisions.isErased = 0`,({noteId, noteRevisionId}) => {@@ -382,7 +382,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT note_revisions.noteRevisionIdFROM note_revisions- LEFT JOIN note_revision_contents USING (noteRevisionId)+ LEFT JOIN note_revision_contents USING (noteRevisionId)WHERE note_revision_contents.noteRevisionId IS NULLAND note_revisions.isProtected = 0`,({noteRevisionId}) => {@@ -401,7 +401,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT noteRevisionIdFROM note_revisions- JOIN note_revision_contents USING (noteRevisionId)+ JOIN note_revision_contents USING (noteRevisionId)WHERE isErased = 0AND content IS NULL`,({noteRevisionId}) => {@@ -454,7 +454,7 @@ class ConsistencyChecks {this.findAndFixIssues(`SELECT parentNoteIdFROM branches- JOIN notes ON notes.noteId = branches.parentNoteId+ JOIN notes ON notes.noteId = branches.parentNoteIdWHERE notes.isDeleted = 0AND notes.type == 'search'AND branches.isDeleted = 0`,@@ -519,7 +519,7 @@ class ConsistencyChecks {SELECT attributeId,attributes.noteIdFROM attributes- JOIN notes ON attributes.noteId = notes.noteId+ JOIN notes ON attributes.noteId = notes.noteIdWHERE attributes.isDeleted = 0AND notes.isDeleted = 1`,({attributeId, noteId}) => {@@ -538,7 +538,7 @@ class ConsistencyChecks {SELECT attributeId,attributes.value AS targetNoteIdFROM attributes- JOIN notes ON attributes.value = notes.noteId+ JOIN notes ON attributes.value = notes.noteIdWHERE attributes.type = 'relation'AND attributes.isDeleted = 0AND notes.isDeleted = 1`,@@ -605,7 +605,7 @@ class ConsistencyChecks {this.runSyncRowChecks("options", "name");}- runAllChecks() {+ runAllChecksAndFixers() {this.unrecoveredConsistencyErrors = false;this.fixedIssues = false;@@ -639,34 +639,39 @@ class ConsistencyChecks {}runDbDiagnostics() {- this.showEntityStat("Notes", `SELECT isDeleted, count(1)- FROM notes- GROUP BY isDeleted`);- this.showEntityStat("Note revisions", `SELECT isErased, count(1)- FROM note_revisions- GROUP BY isErased`);- this.showEntityStat("Branches", `SELECT isDeleted, count(1)- FROM branches- GROUP BY isDeleted`);- this.showEntityStat("Attributes", `SELECT isDeleted, count(1)- FROM attributes- GROUP BY isDeleted`);- this.showEntityStat("API tokens", `SELECT isDeleted, count(1)- FROM api_tokens- GROUP BY isDeleted`);+ this.showEntityStat("Notes",+ `SELECT isDeleted, count(1)+ FROM notes+ GROUP BY isDeleted`);+ this.showEntityStat("Note revisions",+ `SELECT isErased, count(1)+ FROM note_revisions+ GROUP BY isErased`);+ this.showEntityStat("Branches",+ `SELECT isDeleted, count(1)+ FROM branches+ GROUP BY isDeleted`);+ this.showEntityStat("Attributes",+ `SELECT isDeleted, count(1)+ FROM attributes+ GROUP BY isDeleted`);+ this.showEntityStat("API tokens",+ `SELECT isDeleted, count(1)+ FROM api_tokens+ GROUP BY isDeleted`);}async runChecks() {let elapsedTimeMs;await syncMutexService.doExclusively(() => {- const startTime = new Date();+ const startTimeMs = Date.now();this.runDbDiagnostics();- this.runAllChecks();+ this.runAllChecksAndFixers();- elapsedTimeMs = Date.now() - startTime.getTime();+ elapsedTimeMs = Date.now() - startTimeMs;});if (this.unrecoveredConsistencyErrors) {diff --git a/src/services/utils.js b/src/services/utils.jsindex 7e4bc76f6b..c180bea5ba 100644--- a/src/services/utils.js+++ b/src/services/utils.js@@ -158,7 +158,8 @@ function getContentDisposition(filename) {const STRING_MIME_TYPES = ["application/x-javascript", "image/svg+xml"];function isStringNote(type, mime) {- return ["text", "code", "relation-map", "search"].includes(type)+ // render and book are string note in the sense that they are expected to contain empty string+ return ["text", "code", "relation-map", "search", "render", "book"].includes(type)|| mime.startsWith('text/')|| STRING_MIME_TYPES.includes(mime);}