From 9cfddd6f316ec5fce9dd01dafd242e2c8347fdd7 Mon Sep 17 00:00:00 2001 From: Pierre Bastianelli Date: Thu, 4 Nov 2021 15:52:18 -0700 Subject: [PATCH] feat: history gets recorded for projects --- schema/deploy/tables/form_history.sql | 76 +++++++++++++++++++++++++++ schema/revert/tables/form_history.sql | 7 +++ schema/sqitch.plan | 1 + schema/verify/tables/form_history.sql | 7 +++ 4 files changed, 91 insertions(+) create mode 100644 schema/deploy/tables/form_history.sql create mode 100644 schema/revert/tables/form_history.sql create mode 100644 schema/verify/tables/form_history.sql diff --git a/schema/deploy/tables/form_history.sql b/schema/deploy/tables/form_history.sql new file mode 100644 index 0000000000..1ebd46cfc0 --- /dev/null +++ b/schema/deploy/tables/form_history.sql @@ -0,0 +1,76 @@ +-- Deploy cif:tables/audit to pg + +begin; + +create table cif.form_history ( + id integer primary key generated always as identity, + old_form_data jsonb, + new_form_data jsonb, + query varchar(10000000), + form_data_schema_name varchar(1000), + form_data_table_name varchar(1000), + form_data_record_id integer, + change_status varchar(1000) default 'pending', + change_reason varchar(10000) +); + +create or replace function cif.audit_trigger() returns trigger as $$ +declare + old_data jsonb; + new_data jsonb; + existing_id integer; +begin + + existing_id := case when old is null then null else old.id end; + + insert into cif.form_history + ( + old_form_data, + new_form_data, + query, + form_data_schema_name, + form_data_table_name, + form_data_record_id, + change_status, + change_reason + ) + values + ( + row_to_json(old.*) , + row_to_json(new)::jsonb - 'id', + current_query(), + tg_table_schema, + tg_table_name, + existing_id, + 'pending', + 'audit_trigger inserted this row automatically' + ); + + return new; +end; +$$ language plpgsql; + +create trigger project_audit before insert or update or delete on cif.project for each row execute procedure cif.audit_trigger(); + + +create or replace function cif.form_history_apply_changes() returns trigger as $$ +declare + returned_id integer; +begin + + new.change_status := 'saved'; + + execute new.query into returned_id; + + raise notice 'ID: %d', returned_id; + + -- RETRIEVE ID? + + return new; + +end; +$$ language plpgsql; + +create trigger apply_changes after insert on cif.form_history for each row execute procedure cif.form_history_apply_changes(); + +commit; diff --git a/schema/revert/tables/form_history.sql b/schema/revert/tables/form_history.sql new file mode 100644 index 0000000000..b553da75f8 --- /dev/null +++ b/schema/revert/tables/form_history.sql @@ -0,0 +1,7 @@ +-- Revert cif:tables/audit from pg + +begin; + +drop table cif.form_history; + +commit; diff --git a/schema/sqitch.plan b/schema/sqitch.plan index 2aea3faed0..a3d33b74a2 100644 --- a/schema/sqitch.plan +++ b/schema/sqitch.plan @@ -23,3 +23,4 @@ tables/cif_user 2021-10-21T16:10:32Z Matthieu Foucault # Cr trigger_functions/set_user_id 2021-10-21T16:10:32Z Matthieu Foucault # Create the cif_private.set_user_id trigger function mutations/create_user_from_session 2021-10-21T16:10:32Z Matthieu Foucault # Create the cif.create_user_from_session mutation function tables/project 2021-11-04T20:34:23Z Pierre Bastianelli # Create the project table +tables/form_history 2021-11-04T20:58:49Z Pierre Bastianelli # Form history table to track changes to records diff --git a/schema/verify/tables/form_history.sql b/schema/verify/tables/form_history.sql new file mode 100644 index 0000000000..a6a77d1f5f --- /dev/null +++ b/schema/verify/tables/form_history.sql @@ -0,0 +1,7 @@ +-- Verify cif:tables/audit on pg + +begin; + +select pg_catalog.has_table_privilege('cif.form_history', 'select'); + +rollback;