Skip to content

Commit

Permalink
feat: history gets recorded for projects
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia authored and dleard committed Nov 24, 2021
1 parent e4be32d commit 9cfddd6
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
76 changes: 76 additions & 0 deletions schema/deploy/tables/form_history.sql
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions schema/revert/tables/form_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:tables/audit from pg

begin;

drop table cif.form_history;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ tables/cif_user 2021-10-21T16:10:32Z Matthieu Foucault <[email protected]> # Cr
trigger_functions/set_user_id 2021-10-21T16:10:32Z Matthieu Foucault <[email protected]> # Create the cif_private.set_user_id trigger function
mutations/create_user_from_session 2021-10-21T16:10:32Z Matthieu Foucault <[email protected]> # Create the cif.create_user_from_session mutation function
tables/project 2021-11-04T20:34:23Z Pierre Bastianelli <[email protected]> # Create the project table
tables/form_history 2021-11-04T20:58:49Z Pierre Bastianelli <[email protected]> # Form history table to track changes to records
7 changes: 7 additions & 0 deletions schema/verify/tables/form_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify cif:tables/audit on pg

begin;

select pg_catalog.has_table_privilege('cif.form_history', 'select');

rollback;

0 comments on commit 9cfddd6

Please sign in to comment.