From c01827c3e4105d990e7e3c7a0266df78b5c7db28 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 23 Aug 2019 01:06:02 -0400 Subject: also populate subtransactions --- bin/load | 1 + data/schema.sql | 11 +++++++++++ src/main.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/bin/load b/bin/load index eda089f..6b5de19 100755 --- a/bin/load +++ b/bin/load @@ -5,3 +5,4 @@ psql -U metabase metabase -c 'COPY category_groups FROM STDIN' < category_groups psql -U metabase metabase -c 'COPY categories FROM STDIN' < categories.tsv psql -U metabase metabase -c 'COPY payees FROM STDIN' < payees.tsv psql -U metabase metabase -c 'COPY transactions FROM STDIN' < transactions.tsv +psql -U metabase metabase -c 'COPY subtransactions FROM STDIN' < subtransactions.tsv diff --git a/data/schema.sql b/data/schema.sql index fe690be..8a9fc3b 100644 --- a/data/schema.sql +++ b/data/schema.sql @@ -1,3 +1,4 @@ +DROP TABLE IF EXISTS subtransactions; DROP TABLE IF EXISTS transactions; DROP TABLE IF EXISTS payees; DROP TABLE IF EXISTS categories; @@ -54,3 +55,13 @@ CREATE TABLE transactions ( category_id text REFERENCES categories(id), transfer_account_id text REFERENCES accounts(id) ); + +CREATE TABLE subtransactions ( + id text PRIMARY KEY, + transaction_id text REFERENCES transactions(id) NOT NULL, + amount bigint NOT NULL, + memo text, + payee_id text REFERENCES payees(id), + category_id text REFERENCES categories(id), + transfer_account_id text REFERENCES accounts(id) +); diff --git a/src/main.rs b/src/main.rs index 55f47d4..45e2a72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -184,4 +184,39 @@ fn main() { file.write_all(b"\n").unwrap(); } file.sync_all().unwrap(); + + let mut file = std::fs::File::create("subtransactions.tsv").unwrap(); + for subtransaction in budget.subtransactions.unwrap() { + if subtransaction.deleted { + continue; + } + file.write_all( + [ + subtransaction.id.as_ref(), + subtransaction.transaction_id.as_ref(), + format!("{}", subtransaction.amount).as_ref(), + subtransaction + .memo + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + subtransaction + .payee_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + subtransaction + .category_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + subtransaction + .transfer_account_id + .unwrap_or_else(|| "\\N".to_string()) + .as_ref(), + ] + .join("\t") + .as_bytes(), + ) + .unwrap(); + file.write_all(b"\n").unwrap(); + } + file.sync_all().unwrap(); } -- cgit v1.2.3