-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: history gets recorded for projects
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |