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
sync recovery fixes
zadam474baa7d951d
diff --git a/src/services/content_hash.js b/src/services/content_hash.jsindex 207b94e83b..efdd37b679 100644--- a/src/services/content_hash.js+++ b/src/services/content_hash.js@@ -12,11 +12,15 @@ const RecentNote = require('../entities/recent_note');const Option = require('../entities/option');async function getSectorHashes(tableName, primaryKeyName, whereBranch) {- // subselect is necessary to have correct ordering in GROUP_CONCAT- const query = `SELECT SUBSTR(${primaryKeyName}, 1, 1), GROUP_CONCAT(hash) FROM ${tableName} `- + (whereBranch ? `WHERE ${whereBranch} ` : '') + `GROUP BY SUBSTR(${primaryKeyName}, 1, 1) ORDER BY ${primaryKeyName}`;+ const hashes = await sql.getRows(`SELECT ${primaryKeyName} AS id, hash FROM ${tableName} `+ + (whereBranch ? `WHERE ${whereBranch} ` : '')+ + ` ORDER BY ${primaryKeyName}`);- const map = await sql.getMap(query);+ const map = {};++ for (const {id, hash} of hashes) {+ map[id[0]] = (map[id[0]] || "") + hash;+ }for (const key in map) {map[key] = utils.hash(map[key]);@@ -55,7 +59,7 @@ async function checkContentHashes(otherHashes) {const thisSectorHashes = entityHashes[entityName];const otherSectorHashes = otherHashes[entityName];- const sectors = new Set(Object.keys(entityHashes).concat(Object.keys(otherHashes)));+ const sectors = new Set(Object.keys(thisSectorHashes).concat(Object.keys(otherSectorHashes)));for (const sector of sectors) {if (thisSectorHashes[sector] !== otherSectorHashes[sector]) {diff --git a/src/services/sync_update.js b/src/services/sync_update.jsindex 08a99693f3..21c76ed793 100644--- a/src/services/sync_update.js+++ b/src/services/sync_update.js@@ -53,7 +53,7 @@ async function updateEntity(sync, entity, sourceId) {async function updateNote(entity, sourceId) {const origNote = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [entity.noteId]);- if (!origNote || origNote.utcDateModified < entity.utcDateModified) {+ if (!origNote || origNote.utcDateModified < entity.utcDateModified || origNote.hash !== entity.hash) {await sql.transactional(async () => {await sql.replace("notes", entity);@@ -67,7 +67,7 @@ async function updateNote(entity, sourceId) {async function updateNoteContent(entity, sourceId) {const origNoteContent = await sql.getRow("SELECT * FROM note_contents WHERE noteId = ?", [entity.noteId]);- if (!origNoteContent || origNoteContent.utcDateModified < entity.utcDateModified) {+ if (!origNoteContent || origNoteContent.utcDateModified < entity.utcDateModified || origNoteContent.hash !== entity.hash) {entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');await sql.transactional(async () => {@@ -84,7 +84,7 @@ async function updateBranch(entity, sourceId) {const orig = await sql.getRowOrNull("SELECT * FROM branches WHERE branchId = ?", [entity.branchId]);await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified) {+ if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {// isExpanded is not synced unless it's a new branch instance// otherwise in case of full new sync we'll get all branches (even root) collapsed.if (orig) {@@ -104,7 +104,7 @@ async function updateNoteRevision(entity, sourceId) {const orig = await sql.getRowOrNull("SELECT * FROM note_revisions WHERE noteRevisionId = ?", [entity.noteRevisionId]);await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified) {+ if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {await sql.replace('note_revisions', entity);await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);@@ -118,7 +118,7 @@ async function updateNoteRevisionContent(entity, sourceId) {const orig = await sql.getRowOrNull("SELECT * FROM note_revision_contents WHERE noteRevisionId = ?", [entity.noteRevisionId]);await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified) {+ if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {entity.content = entity.content === null ? null : Buffer.from(entity.content, 'base64');await sql.replace('note_revision_contents', entity);@@ -148,7 +148,7 @@ async function updateOptions(entity, sourceId) {}await sql.transactional(async () => {- if (orig === null || orig.utcDateModified < entity.utcDateModified) {+ if (orig === null || orig.utcDateModified < entity.utcDateModified || orig.hash !== entity.hash) {await sql.replace('options', entity);await syncTableService.addOptionsSync(entity.name, sourceId);@@ -159,7 +159,7 @@ async function updateOptions(entity, sourceId) {async function updateRecentNotes(entity, sourceId) {const orig = await sql.getRowOrNull("SELECT * FROM recent_notes WHERE noteId = ?", [entity.noteId]);- if (orig === null || orig.utcDateCreated < entity.utcDateCreated) {+ if (orig === null || orig.utcDateCreated < entity.utcDateCreated || orig.hash !== entity.hash) {await sql.transactional(async () => {await sql.replace('recent_notes', entity);@@ -171,7 +171,7 @@ async function updateRecentNotes(entity, sourceId) {async function updateAttribute(entity, sourceId) {const origAttribute = await sql.getRow("SELECT * FROM attributes WHERE attributeId = ?", [entity.attributeId]);- if (!origAttribute || origAttribute.utcDateModified <= entity.utcDateModified) {+ if (!origAttribute || origAttribute.utcDateModified <= entity.utcDateModified || origAttribute.hash !== entity.hash) {await sql.transactional(async () => {await sql.replace("attributes", entity);diff --git a/src/services/utils.js b/src/services/utils.jsindex 4b5afce3df..0f1bc2c6a8 100644--- a/src/services/utils.js+++ b/src/services/utils.js@@ -54,7 +54,7 @@ function sanitizeSql(str) {}function sanitizeSqlIdentifier(str) {- return str.replace(/[A-Za-z0-9_]/g, "");+ return str.replace(/[^A-Za-z0-9_]/g, "");}function prepareSqlForLike(prefix, str, suffix) {