diff --git a/migrations/0056__fill_sync_again.sql b/migrations/0056__fill_sync_again.sql new file mode 100644 index 000000000..7b446c6e4 --- /dev/null +++ b/migrations/0056__fill_sync_again.sql @@ -0,0 +1,13 @@ +DELETE FROM recent_notes; +DELETE FROM sync; + +INSERT OR IGNORE INTO sync (entity_name, entity_id, sync_date, source_id) + SELECT 'notes', note_id, strftime('%Y-%m-%dT%H:%M:%S.000Z', 'now'), 'IMPORT' FROM notes; + +INSERT OR IGNORE INTO sync (entity_name, entity_id, sync_date, source_id) + SELECT 'notes_tree', note_tree_id, strftime('%Y-%m-%dT%H:%M:%S.000Z', 'now'), 'IMPORT' FROM notes_tree; + +INSERT OR IGNORE INTO sync (entity_name, entity_id, sync_date, source_id) + SELECT 'notes_history', note_history_id, strftime('%Y-%m-%dT%H:%M:%S.000Z', 'now'), 'IMPORT' FROM notes_history; + +UPDATE options SET opt_value = (SELECT MAX(id) FROM sync) WHERE opt_name IN ('last_synced_push', 'last_synced_pull'); \ No newline at end of file diff --git a/services/app_info.js b/services/app_info.js index 203e7d43b..fd2bf3baf 100644 --- a/services/app_info.js +++ b/services/app_info.js @@ -3,7 +3,7 @@ const build = require('./build'); const packageJson = require('../package'); -const APP_DB_VERSION = 55; +const APP_DB_VERSION = 56; module.exports = { app_version: packageJson.version, diff --git a/services/consistency_checks.js b/services/consistency_checks.js index c5ac9d48a..10ce17449 100644 --- a/services/consistency_checks.js +++ b/services/consistency_checks.js @@ -15,9 +15,12 @@ async function runCheck(query, errorText, errorList) { } } -async function runMissingSyncRowCheck(table, key, errorList) { - await runCheck("SELECT " + key + " FROM " + table + " LEFT JOIN sync ON sync.entity_name = '" + table + "' AND entity_id = " + key + " WHERE sync.id IS NULL", - "Missing sync records for " + key + " in table " + table, errorList); +async function runSyncRowChecks(table, key, errorList) { + await runCheck(`SELECT ${key} FROM ${table} LEFT JOIN sync ON sync.entity_name = '${table}' AND entity_id = ${key} WHERE sync.id IS NULL`, + `Missing sync records for ${key} in table ${table}`, errorList); + + await runCheck(`SELECT entity_id FROM sync LEFT JOIN ${table} ON entity_id = ${key} WHERE sync.entity_name = '${table}' AND ${key} IS NULL`, + `Missing ${table} records for existing sync rows`, errorList); } async function runChecks() { @@ -38,10 +41,10 @@ async function runChecks() { await runCheck("SELECT note_history_id || ' > ' || notes_history.note_id FROM notes_history LEFT JOIN notes USING(note_id) WHERE notes.note_id IS NULL", "Missing notes records for following note history ID > note ID", errorList); - await runMissingSyncRowCheck("notes", "note_id", errorList); - await runMissingSyncRowCheck("notes_history", "note_history_id", errorList); - await runMissingSyncRowCheck("notes_tree", "note_tree_id", errorList); - await runMissingSyncRowCheck("recent_notes", "note_tree_id", errorList); + await runSyncRowChecks("notes", "note_id", errorList); + await runSyncRowChecks("notes_history", "note_history_id", errorList); + await runSyncRowChecks("notes_tree", "note_tree_id", errorList); + await runSyncRowChecks("recent_notes", "note_tree_id", errorList); if (errorList.length > 0) { messaging.sendMessage({type: 'consistency-checks-failed'}); diff --git a/services/sync.js b/services/sync.js index b9ff4fb24..3cb4e90dc 100644 --- a/services/sync.js +++ b/services/sync.js @@ -128,7 +128,7 @@ async function pullSync(syncContext) { const resp = await syncRequest(syncContext, 'GET', "/api/sync/" + sync.entity_name + "/" + encodeURIComponent(sync.entity_id)); if (!resp || !resp.entity) { - log.error("Empty response to pull for " + sync.entity_name + ", id=" + sync.entity_id); + log.error(`Empty response to pull for sync #${sync.id} ${sync.entity_name}, id=${sync.entity_id}`); } else if (sync.entity_name === 'notes') { await syncUpdate.updateNote(resp.entity, syncContext.sourceId); @@ -149,7 +149,7 @@ async function pullSync(syncContext) { await syncUpdate.updateRecentNotes(resp, syncContext.sourceId); } else { - throw new Error("Unrecognized entity type " + sync.entity_name); + throw new Error(`Unrecognized entity type ${sync.entity_name} in sync #${sync.id}`); } await setLastSyncedPull(sync.id);