Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 8 additions & 2 deletions sqlite_export_for_ynab/ddl/create-relations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
Expand All @@ -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
;
8 changes: 8 additions & 0 deletions testing/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,27 @@
"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,
},
],
},
{
"id": TRANSACTION_ID_2,
"date": "2024-02-01",
"amount": -15000,
"deleted": True,
"subtransactions": [],
},
]
Expand All @@ -163,22 +167,26 @@
{
"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,
},
],
},
{
"id": SCHEDULED_TRANSACTION_ID_2,
"amount": -11000,
"deleted": True,
"subtransactions": [],
},
]
Expand Down
23 changes: 8 additions & 15 deletions tests/_main_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
]

Expand All @@ -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,
Expand Down Expand Up @@ -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,
},
]

Expand All @@ -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,
Expand Down