From 8a4e56f44d3a12aecedf171fcf8e43025be1cb25 Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 9 Oct 2024 00:31:22 +0100 Subject: [PATCH 1/5] don't consider payees in rules as orphaned --- packages/loot-core/src/server/db/index.ts | 31 ++++++++++++++--------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts index 412f1527007..1d7a90e39de 100644 --- a/packages/loot-core/src/server/db/index.ts +++ b/packages/loot-core/src/server/db/index.ts @@ -580,22 +580,29 @@ export function getCommonPayees() { `); } +const orphanedPayeesQuery = ` + SELECT p.id + FROM payees p + LEFT JOIN payee_mapping pm ON pm.id = p.id + LEFT JOIN v_transactions_internal_alive t ON t.payee = pm.targetId + WHERE p.tombstone = 0 + AND p.transfer_acct IS NULL + AND t.id IS NULL + AND NOT EXISTS ( + SELECT 1 + FROM rules r, + json_each(r.conditions) as cond + WHERE json_extract(cond.value, '$.field') = 'description' + AND json_extract(cond.value, '$.value') = pm.targetId + ); +`; + export function syncGetOrphanedPayees() { - return all(` - SELECT p.id FROM payees p - LEFT JOIN payee_mapping pm ON pm.id = p.id - LEFT JOIN v_transactions_internal_alive t ON t.payee = pm.targetId - WHERE p.tombstone = 0 AND p.transfer_acct IS NULL AND t.id IS NULL -`); + return all(orphanedPayeesQuery); } export async function getOrphanedPayees() { - const rows = await all(` - SELECT p.id FROM payees p - LEFT JOIN payee_mapping pm ON pm.id = p.id - LEFT JOIN v_transactions_internal_alive t ON t.payee = pm.targetId - WHERE p.tombstone = 0 AND p.transfer_acct IS NULL AND t.id IS NULL - `); + const rows = await all(orphanedPayeesQuery); return rows.map(row => row.id); } From 3e62b309dc9ddd31250c7be363f292de5bc5b2ee Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 9 Oct 2024 00:32:51 +0100 Subject: [PATCH 2/5] release note --- upcoming-release-notes/3609.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 upcoming-release-notes/3609.md diff --git a/upcoming-release-notes/3609.md b/upcoming-release-notes/3609.md new file mode 100644 index 00000000000..7c6492fa1ba --- /dev/null +++ b/upcoming-release-notes/3609.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [matt-fidd] +--- + +Don't consider payees used in rules as orphaned From de45e1b645e844ccbc01814c790191cc4512be3d Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 9 Oct 2024 00:45:13 +0100 Subject: [PATCH 3/5] lint --- packages/loot-core/src/server/db/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts index 1d7a90e39de..478b2f07fdb 100644 --- a/packages/loot-core/src/server/db/index.ts +++ b/packages/loot-core/src/server/db/index.ts @@ -580,6 +580,7 @@ export function getCommonPayees() { `); } +/* eslint-disable rulesdir/typography */ const orphanedPayeesQuery = ` SELECT p.id FROM payees p @@ -596,6 +597,7 @@ const orphanedPayeesQuery = ` AND json_extract(cond.value, '$.value') = pm.targetId ); `; +/* eslint-enable rulesdir/typography */ export function syncGetOrphanedPayees() { return all(orphanedPayeesQuery); From 565aeeb3429b4e8f3d7c46901593f4e73cf7eb77 Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 9 Oct 2024 00:56:37 +0100 Subject: [PATCH 4/5] indentation --- packages/loot-core/src/server/db/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts index 478b2f07fdb..fe0276516a3 100644 --- a/packages/loot-core/src/server/db/index.ts +++ b/packages/loot-core/src/server/db/index.ts @@ -590,10 +590,10 @@ const orphanedPayeesQuery = ` AND p.transfer_acct IS NULL AND t.id IS NULL AND NOT EXISTS ( - SELECT 1 - FROM rules r, - json_each(r.conditions) as cond - WHERE json_extract(cond.value, '$.field') = 'description' + SELECT 1 + FROM rules r, + json_each(r.conditions) as cond + WHERE json_extract(cond.value, '$.field') = 'description' AND json_extract(cond.value, '$.value') = pm.targetId ); `; From f7dd31379a622655b9a54a213aa0c37496d69bcd Mon Sep 17 00:00:00 2001 From: matt Date: Wed, 9 Oct 2024 15:24:23 +0100 Subject: [PATCH 5/5] ignore deleted rules --- packages/loot-core/src/server/db/index.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts index fe0276516a3..0893c2aadc8 100644 --- a/packages/loot-core/src/server/db/index.ts +++ b/packages/loot-core/src/server/db/index.ts @@ -593,7 +593,8 @@ const orphanedPayeesQuery = ` SELECT 1 FROM rules r, json_each(r.conditions) as cond - WHERE json_extract(cond.value, '$.field') = 'description' + WHERE r.tombstone = 0 + AND json_extract(cond.value, '$.field') = 'description' AND json_extract(cond.value, '$.value') = pm.targetId ); `;