From 9348a36809f3b0ff8a12a4569bfacdf76255e9cd Mon Sep 17 00:00:00 2001 From: Peter Wood Date: Sat, 4 Mar 2023 18:23:05 -0500 Subject: [PATCH] moving to postgres --- .../functions}/fn_GetHoliday.sql | 0 .../functions}/tvf_BudgetInPeriod.sql | 0 .../functions}/tvf_GetHolidays.sql | 0 holidays.sql => mysql/holidays.sql | Bin .../procedures}/proc_CreateTransaction.sql | 0 .../procedures}/proc_GetTransactions.sql | 0 .../procedures}/proc_MonthlyIncomeExpense.sql | 0 .../procedures}/proc_UpdateHolidaysTable.sql | 0 {tables => mysql/tables}/BUDGET_CATEGORY.sql | 0 {tables => mysql/tables}/BUDGET_DETAIL.sql | 0 {tables => mysql/tables}/HOLIDAYS.sql | 0 .../views}/vw_BudgetRunningBalance.sql | 0 .../views}/vw_CurrentMonthBudget.sql | 0 postgres/create uuid-ossp extension.sql | 1 + .../procedures/import_budget_from_csv.sql | 18 +++++++ .../procedures/import_budget_from_import.sql | 13 +++++ postgres/tables/budgetdetails.sql | 19 ++++++++ postgres/tables/budgetimport.sql | 11 +++++ postgres/temp.sql | 46 ++++++++++++++++++ postgres/views/runbal.sql | 18 +++++++ 20 files changed, 126 insertions(+) rename {functions => mysql/functions}/fn_GetHoliday.sql (100%) rename {functions => mysql/functions}/tvf_BudgetInPeriod.sql (100%) rename {functions => mysql/functions}/tvf_GetHolidays.sql (100%) rename holidays.sql => mysql/holidays.sql (100%) rename {procedures => mysql/procedures}/proc_CreateTransaction.sql (100%) rename {procedures => mysql/procedures}/proc_GetTransactions.sql (100%) rename {procedures => mysql/procedures}/proc_MonthlyIncomeExpense.sql (100%) rename {procedures => mysql/procedures}/proc_UpdateHolidaysTable.sql (100%) rename {tables => mysql/tables}/BUDGET_CATEGORY.sql (100%) rename {tables => mysql/tables}/BUDGET_DETAIL.sql (100%) rename {tables => mysql/tables}/HOLIDAYS.sql (100%) rename {views => mysql/views}/vw_BudgetRunningBalance.sql (100%) rename {views => mysql/views}/vw_CurrentMonthBudget.sql (100%) create mode 100644 postgres/create uuid-ossp extension.sql create mode 100644 postgres/procedures/import_budget_from_csv.sql create mode 100644 postgres/procedures/import_budget_from_import.sql create mode 100644 postgres/tables/budgetdetails.sql create mode 100644 postgres/tables/budgetimport.sql create mode 100644 postgres/temp.sql create mode 100644 postgres/views/runbal.sql diff --git a/functions/fn_GetHoliday.sql b/mysql/functions/fn_GetHoliday.sql similarity index 100% rename from functions/fn_GetHoliday.sql rename to mysql/functions/fn_GetHoliday.sql diff --git a/functions/tvf_BudgetInPeriod.sql b/mysql/functions/tvf_BudgetInPeriod.sql similarity index 100% rename from functions/tvf_BudgetInPeriod.sql rename to mysql/functions/tvf_BudgetInPeriod.sql diff --git a/functions/tvf_GetHolidays.sql b/mysql/functions/tvf_GetHolidays.sql similarity index 100% rename from functions/tvf_GetHolidays.sql rename to mysql/functions/tvf_GetHolidays.sql diff --git a/holidays.sql b/mysql/holidays.sql similarity index 100% rename from holidays.sql rename to mysql/holidays.sql diff --git a/procedures/proc_CreateTransaction.sql b/mysql/procedures/proc_CreateTransaction.sql similarity index 100% rename from procedures/proc_CreateTransaction.sql rename to mysql/procedures/proc_CreateTransaction.sql diff --git a/procedures/proc_GetTransactions.sql b/mysql/procedures/proc_GetTransactions.sql similarity index 100% rename from procedures/proc_GetTransactions.sql rename to mysql/procedures/proc_GetTransactions.sql diff --git a/procedures/proc_MonthlyIncomeExpense.sql b/mysql/procedures/proc_MonthlyIncomeExpense.sql similarity index 100% rename from procedures/proc_MonthlyIncomeExpense.sql rename to mysql/procedures/proc_MonthlyIncomeExpense.sql diff --git a/procedures/proc_UpdateHolidaysTable.sql b/mysql/procedures/proc_UpdateHolidaysTable.sql similarity index 100% rename from procedures/proc_UpdateHolidaysTable.sql rename to mysql/procedures/proc_UpdateHolidaysTable.sql diff --git a/tables/BUDGET_CATEGORY.sql b/mysql/tables/BUDGET_CATEGORY.sql similarity index 100% rename from tables/BUDGET_CATEGORY.sql rename to mysql/tables/BUDGET_CATEGORY.sql diff --git a/tables/BUDGET_DETAIL.sql b/mysql/tables/BUDGET_DETAIL.sql similarity index 100% rename from tables/BUDGET_DETAIL.sql rename to mysql/tables/BUDGET_DETAIL.sql diff --git a/tables/HOLIDAYS.sql b/mysql/tables/HOLIDAYS.sql similarity index 100% rename from tables/HOLIDAYS.sql rename to mysql/tables/HOLIDAYS.sql diff --git a/views/vw_BudgetRunningBalance.sql b/mysql/views/vw_BudgetRunningBalance.sql similarity index 100% rename from views/vw_BudgetRunningBalance.sql rename to mysql/views/vw_BudgetRunningBalance.sql diff --git a/views/vw_CurrentMonthBudget.sql b/mysql/views/vw_CurrentMonthBudget.sql similarity index 100% rename from views/vw_CurrentMonthBudget.sql rename to mysql/views/vw_CurrentMonthBudget.sql diff --git a/postgres/create uuid-ossp extension.sql b/postgres/create uuid-ossp extension.sql new file mode 100644 index 0000000..682131d --- /dev/null +++ b/postgres/create uuid-ossp extension.sql @@ -0,0 +1 @@ +CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; \ No newline at end of file diff --git a/postgres/procedures/import_budget_from_csv.sql b/postgres/procedures/import_budget_from_csv.sql new file mode 100644 index 0000000..a0bd7f5 --- /dev/null +++ b/postgres/procedures/import_budget_from_csv.sql @@ -0,0 +1,18 @@ +create or replace procedure import_budget_from_csv() +language plpgsql +as $procedure$ +begin + +truncate table public.budgetimport; + +copy + public.budgetimport(dt, amount, description) +from + '/usr/share/budget.csv' +delimiter ',' +csv +header; +commit; +end; +$procedure$; +GRANT EXECUTE ON PROCEDURE public.update_budget_from_import() TO acedanger; diff --git a/postgres/procedures/import_budget_from_import.sql b/postgres/procedures/import_budget_from_import.sql new file mode 100644 index 0000000..dd2b853 --- /dev/null +++ b/postgres/procedures/import_budget_from_import.sql @@ -0,0 +1,13 @@ +CREATE OR REPLACE PROCEDURE public.update_budget_from_import() + LANGUAGE plpgsql +AS $procedure$ +begin +truncate public.budgetdetails; + +insert into public.budgetdetails (trxdescription, trxdate, trxamount) +select description, cast(dt as date) as dt_converted, amount +from public.budgetimport; + +end +$procedure$; +GRANT EXECUTE ON PROCEDURE public.update_budget_from_import() TO acedanger; diff --git a/postgres/tables/budgetdetails.sql b/postgres/tables/budgetdetails.sql new file mode 100644 index 0000000..6a768d3 --- /dev/null +++ b/postgres/tables/budgetdetails.sql @@ -0,0 +1,19 @@ +-- public.budgetdetails definition + +-- Drop table +DROP TABLE public.budgetdetails; + +CREATE TABLE public.budgetdetails ( + trxid uuid NOT NULL DEFAULT uuid_generate_v4(), + trxdescription text NOT NULL, + trxdate date NOT NULL, + trxamount money NOT NULL, + insertdttm timestamp NULL DEFAULT now(), + CONSTRAINT budgetdetails_pkey PRIMARY KEY (trxid) +); + +-- Permissions + +ALTER TABLE public.budgetdetails OWNER TO acedanger; +GRANT ALL ON TABLE public.budgetdetails TO acedanger; +GRANT ALL ON TABLE public.budgetdetails TO budgetuser; \ No newline at end of file diff --git a/postgres/tables/budgetimport.sql b/postgres/tables/budgetimport.sql new file mode 100644 index 0000000..ad398dc --- /dev/null +++ b/postgres/tables/budgetimport.sql @@ -0,0 +1,11 @@ +DROP TABLE public.budgetimport; + +create table public.budgetimport ( + dt bpchar(10) NULL, + amount numeric(10, 2) NULL, + description bpchar(200) NULL +); + +ALTER TABLE public.budgetimport OWNER TO acedanger; +GRANT ALL ON TABLE public.budgetimport TO acedanger; +GRANT ALL ON TABLE public.budgetimport TO budgetuser; \ No newline at end of file diff --git a/postgres/temp.sql b/postgres/temp.sql new file mode 100644 index 0000000..3fa8989 --- /dev/null +++ b/postgres/temp.sql @@ -0,0 +1,46 @@ + +/* +truncate table budgetdetails; + +CALL public.import_budget_from_csv(); +CALL public.update_budget_from_import(); +*/ + + +select + lower(trxdescription) trxdescription + , avg(abs(trxamount)) avg_amt + , min(abs(trxamount)) min_amt + , max(abs(trxamount)) max_amt + , count(trxdescription) rec_cnt +from + public.budgetdetails b +where + trxdate <= current_date + and abs(trxamount) > 0 +group by + lower(trxdescription) +having + count(1) > 2 +order by + rec_cnt desc + , trxdescription; + + +select + bal.trxdate + , bal.day_of_week + , age(trxdate, current_date) as days_from_today + , bal.trxdescription + , bal.trxamount + , bal.runningbal +from + public.runbal bal +where + trxdate between current_date and ( + current_date + interval '2 weeks' + )::date +; + +select * from budgetimport b +where dt like '2023-03%' diff --git a/postgres/views/runbal.sql b/postgres/views/runbal.sql new file mode 100644 index 0000000..b846395 --- /dev/null +++ b/postgres/views/runbal.sql @@ -0,0 +1,18 @@ +drop view public.runbal; + +create view public.runbal +as +select + trxdate + , trxdescription + , trxamount + , to_char(trxdate, 'day') day_of_week + , sum(trxamount) over( + order by + trxdate + , trxamount desc rows unbounded preceding + ) runningbal +from + public.budgetdetails + + \ No newline at end of file