diff --git a/README.md b/README.md index c04d954..643089f 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,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: budget ID, transaction ID) so data across budgets remains separate. -1. Two new views called `flat_transactions` and `scheduled_flat_transactions` allow you to query split and non-split transactions easily, without needing to also query `subtransactions` and `scheduled_subtransactions` respectively. They also include fields to improve quality of life (ex: `amount_major` to convert from [YNAB's milliunits](https://api.ynab.com/#formats) to [major units](https://en.wikipedia.org/wiki/ISO_4217) i.e. dollars). +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 include fields to improve quality of life (ex: `amount_major` to convert from [YNAB's milliunits](https://api.ynab.com/#formats) to [major units](https://en.wikipedia.org/wiki/ISO_4217) i.e. dollars) and filter out deleted transactions/subtransactions. ## Querying diff --git a/sqlite_export_for_ynab/ddl/create-relations.sql b/sqlite_export_for_ynab/ddl/create-relations.sql index 3a47f47..eb67809 100644 --- a/sqlite_export_for_ynab/ddl/create-relations.sql +++ b/sqlite_export_for_ynab/ddl/create-relations.sql @@ -171,7 +171,6 @@ SELECT COALESCE(st.transfer_account_id, t.transfer_account_id) IS NULL THEN COALESCE(st.category_name, t.category_name) END AS category_name - , COALESCE(st.deleted, t.deleted) AS deleted , COALESCE(st.memo, t.memo) AS memo , COALESCE(st.payee_id, t.payee_id) AS payee_id , COALESCE(st.payee_name, t.payee_name) AS payee_name @@ -188,6 +187,10 @@ LEFT JOIN subtransactions AS st t.budget_id = st.budget_id AND t.id = st.transaction_id ) +WHERE + TRUE + AND NOT t.deleted + AND NOT st.deleted ; CREATE TABLE IF NOT EXISTS scheduled_transactions ( @@ -261,7 +264,6 @@ SELECT COALESCE(st.transfer_account_id, t.transfer_account_id) IS NULL THEN COALESCE(st.category_name, t.category_name) END AS category_name - , COALESCE(st.deleted, t.deleted) AS deleted , COALESCE(st.memo, t.memo) AS memo , COALESCE(st.payee_id, t.payee_id) AS payee_id , COALESCE(st.transfer_account_id, t.transfer_account_id) @@ -273,4 +275,8 @@ LEFT JOIN scheduled_subtransactions AS st t.budget_id = st.budget_id AND t.id = st.scheduled_transaction_id ) +WHERE + TRUE + AND NOT t.deleted + AND NOT st.deleted ; diff --git a/testing/fixtures.py b/testing/fixtures.py index c98dde6..11e64b4 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -132,16 +132,19 @@ "id": TRANSACTION_ID_1, "date": "2024-01-01", "amount": -10000, + "deleted": False, "subtransactions": [ { "id": SUBTRANSACTION_ID_1, "transaction_id": TRANSACTION_ID_1, "amount": -7500, + "deleted": False, }, { "id": SUBTRANSACTION_ID_2, "transaction_id": TRANSACTION_ID_1, "amount": -2500, + "deleted": False, }, ], }, @@ -149,6 +152,7 @@ "id": TRANSACTION_ID_2, "date": "2024-02-01", "amount": -15000, + "deleted": True, "subtransactions": [], }, ] @@ -163,15 +167,18 @@ { "id": SCHEDULED_TRANSACTION_ID_1, "amount": -12000, + "deleted": False, "subtransactions": [ { "id": SCHEDULED_SUBTRANSACTION_ID_1, "scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1, + "deleted": False, "amount": -8040, }, { "id": SCHEDULED_SUBTRANSACTION_ID_2, "scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1, + "deleted": False, "amount": -2960, }, ], @@ -179,6 +186,7 @@ { "id": SCHEDULED_TRANSACTION_ID_2, "amount": -11000, + "deleted": True, "subtransactions": [], }, ] diff --git a/tests/_main_test.py b/tests/_main_test.py index 6c3498e..783843f 100644 --- a/tests/_main_test.py +++ b/tests/_main_test.py @@ -242,12 +242,14 @@ def test_insert_transactions(cur): "budget_id": BUDGET_ID_1, "date": "2024-01-01", "amount": -10000, + "deleted": False, }, { "id": TRANSACTION_ID_2, "budget_id": BUDGET_ID_1, "date": "2024-02-01", "amount": -15000, + "deleted": True, }, ] @@ -258,25 +260,19 @@ def test_insert_transactions(cur): "transaction_id": TRANSACTION_ID_1, "budget_id": BUDGET_ID_1, "amount": -7500, + "deleted": False, }, { "id": SUBTRANSACTION_ID_2, "transaction_id": TRANSACTION_ID_1, "budget_id": BUDGET_ID_1, "amount": -2500, + "deleted": False, }, ] cur.execute("SELECT * FROM flat_transactions ORDER BY amount") assert [strip_nones(d) for d in cur.fetchall()] == [ - { - "transaction_id": TRANSACTION_ID_2, - "budget_id": BUDGET_ID_1, - "date": "2024-02-01", - "id": TRANSACTION_ID_2, - "amount": -15000, - "amount_major": pytest.approx(15), - }, { "transaction_id": TRANSACTION_ID_1, "subtransaction_id": SUBTRANSACTION_ID_1, @@ -311,11 +307,13 @@ def test_insert_scheduled_transactions(cur): "id": SCHEDULED_TRANSACTION_ID_1, "budget_id": BUDGET_ID_1, "amount": -12000, + "deleted": False, }, { "id": SCHEDULED_TRANSACTION_ID_2, "budget_id": BUDGET_ID_1, "amount": -11000, + "deleted": True, }, ] @@ -326,24 +324,19 @@ def test_insert_scheduled_transactions(cur): "scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1, "budget_id": BUDGET_ID_1, "amount": -8040, + "deleted": False, }, { "id": SCHEDULED_SUBTRANSACTION_ID_2, "scheduled_transaction_id": SCHEDULED_TRANSACTION_ID_1, "budget_id": BUDGET_ID_1, "amount": -2960, + "deleted": False, }, ] cur.execute("SELECT * FROM scheduled_flat_transactions ORDER BY amount") assert [strip_nones(d) for d in cur.fetchall()] == [ - { - "transaction_id": SCHEDULED_TRANSACTION_ID_2, - "budget_id": BUDGET_ID_1, - "id": SCHEDULED_TRANSACTION_ID_2, - "amount": -11000, - "amount_major": pytest.approx(11), - }, { "transaction_id": SCHEDULED_TRANSACTION_ID_1, "subtransaction_id": SCHEDULED_SUBTRANSACTION_ID_1,