Skip to content

Commit 1643cd1

Browse files
authored
Only read approved txns in flat_transactions and example queries (#192)
1 parent eea963b commit 1643cd1

4 files changed

Lines changed: 9 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ The relations are defined in [create-relations.sql](sqlite_export_for_ynab/ddl/c
6262

6363
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).
6464
1. Foreign keys are added as needed (ex: plan ID, transaction ID) so data across plans remains separate.
65-
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.
65+
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.
6666

6767
## Querying
6868

@@ -116,6 +116,7 @@ WITH used_payees AS (
116116
FROM transactions
117117
WHERE
118118
TRUE
119+
AND approved
119120
AND payee_id IS NOT NULL
120121
AND NOT deleted
121122
UNION

sqlite_export_for_ynab/ddl/create-relations.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ SELECT
181181
, t.plan_id
182182
, t.account_id
183183
, t.account_name
184-
, t.approved
185184
, t.cleared
186185
, t."date"
187186
, t.debt_transaction_type
@@ -227,6 +226,7 @@ INNER JOIN categories AS c
227226
)
228227
WHERE
229228
TRUE
229+
AND t.approved
230230
AND NOT COALESCE(st.deleted, t.deleted)
231231
;
232232

testing/fixtures.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
"amount": -10000,
208208
"amount_formatted": "$10.00",
209209
"amount_currency": 10.0,
210+
"approved": True,
210211
"category_id": CATEGORY_ID_3,
211212
"category_name": CATEGORY_NAME_3,
212213
"deleted": False,
@@ -239,6 +240,7 @@
239240
"amount": -15000,
240241
"amount_formatted": "$15.00",
241242
"amount_currency": 15.0,
243+
"approved": True,
242244
"category_id": CATEGORY_ID_2,
243245
"category_name": CATEGORY_NAME_2,
244246
"deleted": True,
@@ -250,6 +252,7 @@
250252
"amount": -19000,
251253
"amount_formatted": "$19.00",
252254
"amount_currency": 19.0,
255+
"approved": False,
253256
"category_id": CATEGORY_ID_4,
254257
"category_name": CATEGORY_NAME_4,
255258
"deleted": False,

tests/_main_test.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ def test_insert_transactions(cur):
314314
"amount": -10000,
315315
"amount_formatted": "$10.00",
316316
"amount_currency": 10.0,
317+
"approved": 1,
317318
"category_id": CATEGORY_ID_3,
318319
"category_name": CATEGORY_NAME_3,
319320
"deleted": False,
@@ -325,6 +326,7 @@ def test_insert_transactions(cur):
325326
"amount": -15000,
326327
"amount_formatted": "$15.00",
327328
"amount_currency": 15.0,
329+
"approved": 1,
328330
"category_id": CATEGORY_ID_2,
329331
"category_name": CATEGORY_NAME_2,
330332
"deleted": True,
@@ -336,6 +338,7 @@ def test_insert_transactions(cur):
336338
"amount": -19000,
337339
"amount_formatted": "$19.00",
338340
"amount_currency": 19.0,
341+
"approved": 0,
339342
"category_id": CATEGORY_ID_4,
340343
"category_name": CATEGORY_NAME_4,
341344
"deleted": False,
@@ -370,19 +373,6 @@ def test_insert_transactions(cur):
370373

371374
cur.execute("SELECT * FROM flat_transactions ORDER BY amount")
372375
assert [strip_nones(d) for d in cur.fetchall()] == [
373-
{
374-
"transaction_id": TRANSACTION_ID_3,
375-
"plan_id": PLAN_ID_1,
376-
"date": "2024-03-01",
377-
"id": TRANSACTION_ID_3,
378-
"amount": -19000,
379-
"amount_formatted": "$19.00",
380-
"amount_currency": 19.0,
381-
"category_id": CATEGORY_ID_4,
382-
"category_name": CATEGORY_NAME_4,
383-
"category_group_id": CATEGORY_GROUP_ID_2,
384-
"category_group_name": CATEGORY_GROUP_NAME_2,
385-
},
386376
{
387377
"transaction_id": TRANSACTION_ID_1,
388378
"subtransaction_id": SUBTRANSACTION_ID_1,

0 commit comments

Comments
 (0)