From f388591f5e69f8e82869c3650003d2cd21ebc4f3 Mon Sep 17 00:00:00 2001 From: Max R Date: Fri, 24 Apr 2026 15:09:07 +0200 Subject: [PATCH 1/3] fix --- README.md | 12 +++++++++--- .../ddl/create-relations.sql | 2 +- testing/fixtures.py | 3 +++ tests/_main_test.py | 18 +++++------------- 4 files changed, 18 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6e85f7e..0176ca6 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ The relations are defined in [create-relations.sql](sqlite_export_for_ynab/ddl/c 1. Some objects are pulled out into their own tables so they can be more cleanly modeled in SQLite (ex: subtransactions, loan account periodic values). 1. Foreign keys are added as needed (ex: plan ID, transaction ID) so data across plans remains separate. -1. Two new views called `flat_transactions` and `scheduled_flat_transactions`. These allow you to query split and non-split transactions easily, without needing to also query `subtransactions` and `scheduled_subtransactions` respectively. They also filter out deleted transactions/subtransactions and project payee/category fields to make querying more ergonomic. +1. Two new views called `flat_transactions` and `scheduled_flat_transactions`. These allow you to query split and non-split transactions easily, without needing to also query `subtransactions` and `scheduled_subtransactions` respectively. They also filter out deleted/unapproved transactions/subtransactions and project payee/category fields to make querying more ergonomic. ## Querying @@ -92,7 +92,9 @@ WITH ranked_payees AS ( AS rnk FROM flat_transactions AS t INNER JOIN plans AS pl ON t.plan_id = pl.id WHERE - t.payee_name != 'Starting Balance' AND t.transfer_account_id IS NULL + TRUE + AND t.payee_name != 'Starting Balance' + AND t.transfer_account_id IS NULL GROUP BY pl.id, t.payee_id ) @@ -116,6 +118,7 @@ WITH used_payees AS ( FROM transactions WHERE TRUE + AND approved AND payee_id IS NOT NULL AND NOT deleted UNION @@ -189,7 +192,9 @@ FROM ( , amount_currency FROM flat_transactions WHERE - category_name = 'Apps' + TRUE + AND approved + AND category_name = 'Apps' AND SUBSTR("date", 1, 7) = SUBSTR(DATE(), 1, 7) UNION ALL SELECT @@ -245,6 +250,7 @@ WITH interest_by_account AS ( FROM flat_transactions WHERE TRUE + AND approved AND payee_name = COALESCE(NULLIF(@interest_payee_name, ''), 'Interest') AND SUBSTR("date", 1, 4) = CAST(@year AS TEXT) AND (COALESCE(@plan_id, '') = '' OR plan_id = @plan_id) diff --git a/sqlite_export_for_ynab/ddl/create-relations.sql b/sqlite_export_for_ynab/ddl/create-relations.sql index 6ff0162..075c58a 100644 --- a/sqlite_export_for_ynab/ddl/create-relations.sql +++ b/sqlite_export_for_ynab/ddl/create-relations.sql @@ -181,7 +181,6 @@ SELECT , t.plan_id , t.account_id , t.account_name - , t.approved , t.cleared , t."date" , t.debt_transaction_type @@ -227,6 +226,7 @@ INNER JOIN categories AS c ) WHERE TRUE + AND t.approved AND NOT COALESCE(st.deleted, t.deleted) ; diff --git a/testing/fixtures.py b/testing/fixtures.py index fc8df45..8a1edcd 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -207,6 +207,7 @@ "amount": -10000, "amount_formatted": "$10.00", "amount_currency": 10.0, + "approved": True, "category_id": CATEGORY_ID_3, "category_name": CATEGORY_NAME_3, "deleted": False, @@ -239,6 +240,7 @@ "amount": -15000, "amount_formatted": "$15.00", "amount_currency": 15.0, + "approved": True, "category_id": CATEGORY_ID_2, "category_name": CATEGORY_NAME_2, "deleted": True, @@ -250,6 +252,7 @@ "amount": -19000, "amount_formatted": "$19.00", "amount_currency": 19.0, + "approved": False, "category_id": CATEGORY_ID_4, "category_name": CATEGORY_NAME_4, "deleted": False, diff --git a/tests/_main_test.py b/tests/_main_test.py index 1c31670..b3704fe 100644 --- a/tests/_main_test.py +++ b/tests/_main_test.py @@ -314,6 +314,7 @@ def test_insert_transactions(cur): "amount": -10000, "amount_formatted": "$10.00", "amount_currency": 10.0, + "approved": 1, "category_id": CATEGORY_ID_3, "category_name": CATEGORY_NAME_3, "deleted": False, @@ -325,6 +326,7 @@ def test_insert_transactions(cur): "amount": -15000, "amount_formatted": "$15.00", "amount_currency": 15.0, + "approved": 1, "category_id": CATEGORY_ID_2, "category_name": CATEGORY_NAME_2, "deleted": True, @@ -336,6 +338,7 @@ def test_insert_transactions(cur): "amount": -19000, "amount_formatted": "$19.00", "amount_currency": 19.0, + "approved": 0, "category_id": CATEGORY_ID_4, "category_name": CATEGORY_NAME_4, "deleted": False, @@ -370,23 +373,11 @@ def test_insert_transactions(cur): cur.execute("SELECT * FROM flat_transactions ORDER BY amount") assert [strip_nones(d) for d in cur.fetchall()] == [ - { - "transaction_id": TRANSACTION_ID_3, - "plan_id": PLAN_ID_1, - "date": "2024-03-01", - "id": TRANSACTION_ID_3, - "amount": -19000, - "amount_formatted": "$19.00", - "amount_currency": 19.0, - "category_id": CATEGORY_ID_4, - "category_name": CATEGORY_NAME_4, - "category_group_id": CATEGORY_GROUP_ID_2, - "category_group_name": CATEGORY_GROUP_NAME_2, - }, { "transaction_id": TRANSACTION_ID_1, "subtransaction_id": SUBTRANSACTION_ID_1, "plan_id": PLAN_ID_1, + "approved": 1, "date": "2024-01-01", "id": SUBTRANSACTION_ID_1, "amount": -7500, @@ -401,6 +392,7 @@ def test_insert_transactions(cur): "transaction_id": TRANSACTION_ID_1, "subtransaction_id": SUBTRANSACTION_ID_2, "plan_id": PLAN_ID_1, + "approved": 1, "date": "2024-01-01", "id": SUBTRANSACTION_ID_2, "amount": -2500, From f95a6d0ba1e7382dfbbe3b3459cc0092877f7a1c Mon Sep 17 00:00:00 2001 From: Max R Date: Fri, 24 Apr 2026 18:04:06 +0200 Subject: [PATCH 2/3] Only consider approved transactions --- README.md | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0176ca6..4150a21 100644 --- a/README.md +++ b/README.md @@ -92,9 +92,7 @@ WITH ranked_payees AS ( AS rnk FROM flat_transactions AS t INNER JOIN plans AS pl ON t.plan_id = pl.id WHERE - TRUE - AND t.payee_name != 'Starting Balance' - AND t.transfer_account_id IS NULL + t.payee_name != 'Starting Balance' AND t.transfer_account_id IS NULL GROUP BY pl.id, t.payee_id ) @@ -192,9 +190,7 @@ FROM ( , amount_currency FROM flat_transactions WHERE - TRUE - AND approved - AND category_name = 'Apps' + category_name = 'Apps' AND SUBSTR("date", 1, 7) = SUBSTR(DATE(), 1, 7) UNION ALL SELECT @@ -250,7 +246,6 @@ WITH interest_by_account AS ( FROM flat_transactions WHERE TRUE - AND approved AND payee_name = COALESCE(NULLIF(@interest_payee_name, ''), 'Interest') AND SUBSTR("date", 1, 4) = CAST(@year AS TEXT) AND (COALESCE(@plan_id, '') = '' OR plan_id = @plan_id) From 5a2224092f3a5a4c71722c13bb2f9d6cae03ced4 Mon Sep 17 00:00:00 2001 From: Max R Date: Fri, 24 Apr 2026 16:53:07 -0400 Subject: [PATCH 3/3] update transaction view tests --- tests/_main_test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/_main_test.py b/tests/_main_test.py index b3704fe..aafb359 100644 --- a/tests/_main_test.py +++ b/tests/_main_test.py @@ -377,7 +377,6 @@ def test_insert_transactions(cur): "transaction_id": TRANSACTION_ID_1, "subtransaction_id": SUBTRANSACTION_ID_1, "plan_id": PLAN_ID_1, - "approved": 1, "date": "2024-01-01", "id": SUBTRANSACTION_ID_1, "amount": -7500, @@ -392,7 +391,6 @@ def test_insert_transactions(cur): "transaction_id": TRANSACTION_ID_1, "subtransaction_id": SUBTRANSACTION_ID_2, "plan_id": PLAN_ID_1, - "approved": 1, "date": "2024-01-01", "id": SUBTRANSACTION_ID_2, "amount": -2500,