From ff7be0d637d77b779366ebd87ad233ee374da5ac Mon Sep 17 00:00:00 2001
From: Matiss Janis Aboltins <matiss@mja.lv>
Date: Fri, 7 Jul 2023 18:06:25 +0100
Subject: [PATCH] :loud_sound:  improving 'invalid-schema' error logs (#1302)

The logs for `invalid-schema` were not logging the used query or the
metadata. So fixing this.

It would make debugging issues such as
https://github.com/actualbudget/actual/issues/1296 easier.
---
 packages/loot-core/src/server/sync/index.ts | 10 ++++++----
 upcoming-release-notes/1302.md              |  6 ++++++
 2 files changed, 12 insertions(+), 4 deletions(-)
 create mode 100644 upcoming-release-notes/1302.md

diff --git a/packages/loot-core/src/server/sync/index.ts b/packages/loot-core/src/server/sync/index.ts
index 6b0a1b9dc..c3ec89329 100644
--- a/packages/loot-core/src/server/sync/index.ts
+++ b/packages/loot-core/src/server/sync/index.ts
@@ -81,17 +81,17 @@ function apply(msg: Message, prev?: boolean) {
     try {
       if (prev) {
         query = {
-          sql: db.cache(`UPDATE ${dataset} SET ${column} = ? WHERE id = ?`),
+          sql: `UPDATE ${dataset} SET ${column} = ? WHERE id = ?`,
           params: [value, row],
         };
       } else {
         query = {
-          sql: db.cache(`INSERT INTO ${dataset} (id, ${column}) VALUES (?, ?)`),
+          sql: `INSERT INTO ${dataset} (id, ${column}) VALUES (?, ?)`,
           params: [row, value],
         };
       }
 
-      db.runQuery(query.sql, query.params);
+      db.runQuery(db.cache(query.sql), query.params);
     } catch (error) {
       throw new SyncError('invalid-schema', {
         error: { message: error.message, stack: error.stack },
@@ -564,11 +564,13 @@ export const fullSync = once(async function (): Promise<
         app.events.emit('sync', {
           type: 'error',
           subtype: 'out-of-sync',
+          meta: e.meta,
         });
       } else if (e.reason === 'invalid-schema') {
         app.events.emit('sync', {
           type: 'error',
           subtype: 'invalid-schema',
+          meta: e.meta,
         });
       } else if (
         e.reason === 'decrypt-failure' ||
@@ -580,7 +582,7 @@ export const fullSync = once(async function (): Promise<
           meta: e.meta,
         });
       } else {
-        app.events.emit('sync', { type: 'error' });
+        app.events.emit('sync', { type: 'error', meta: e.meta });
       }
     } else if (e instanceof PostError) {
       console.log(e);
diff --git a/upcoming-release-notes/1302.md b/upcoming-release-notes/1302.md
new file mode 100644
index 000000000..a42004436
--- /dev/null
+++ b/upcoming-release-notes/1302.md
@@ -0,0 +1,6 @@
+---
+category: Maintenance
+authors: [MatissJanis]
+---
+
+Improved error logs for `invalid-schema` issues
-- 
GitLab