Skip to content

Commit

Permalink
feat: trigger to propagate project_revision deletion to the form_chan…
Browse files Browse the repository at this point in the history
…ge records
  • Loading branch information
pbastia committed Jan 13, 2022
1 parent 7adb159 commit 0a42457
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 0 deletions.
6 changes: 6 additions & 0 deletions schema/deploy/tables/project_revision.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ create trigger commit_project_revision
for each row
execute procedure cif_private.commit_project_revision();

create trigger discard_project_revision
after insert or update of deleted_at on cif.project_revision
for each row
when (new.deleted_at is not null)
execute procedure cif_private.discard_project_revision();

do
$grant$
begin
Expand Down
17 changes: 17 additions & 0 deletions schema/deploy/trigger_functions/discard_project_revision.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- Deploy cif:trigger_functions/discard_project_revision to pg

begin;

create or replace function cif_private.discard_project_revision()
returns trigger as $$
declare
begin
update cif.form_change
set deleted_at = now()
where project_revision_id=new.id;

return new;
end;
$$ language plpgsql;

commit;
7 changes: 7 additions & 0 deletions schema/revert/trigger_functions/discard_project_revision.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert cif:trigger_functions/discard_project_revision from pg

begin;

drop function cif_private.discard_project_revision;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ trigger_functions/commit_form_change [schemas/private util_functions/camel_to_sn
tables/change_status [schemas/main] 2021-11-08T22:46:03Z Pierre Bastianelli <[email protected]> # Table to constrain the status of a form_change row
trigger_functions/committed_changes_are_immutable 2022-01-06T23:40:27Z Pierre Bastianelli <[email protected]> # Trigger to prevent committed changes from being altered
trigger_functions/commit_project_revision 2021-12-07T00:37:33Z Pierre Bastianelli <[email protected]> # A trigger function executed when a whole project revision is committed, triggering commit on the individual form_change records
trigger_functions/discard_project_revision 2022-01-12T16:58:56Z Pierre Bastianelli <[email protected]> # A trigger to propagate the deletion of a project revision to the individual form_change records
tables/project_revision 2021-12-07T00:39:51Z Pierre Bastianelli <[email protected]> # A table to track global project revisions - containing multiple changes
tables/form_change [tables/change_status] 2021-11-04T20:58:49Z Pierre Bastianelli <[email protected]> # Form history table to track changes to records
tables/attachment 2021-11-24T21:24:45Z Alex Zorkin <[email protected]> # Add schema for attachments
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@


begin;

select plan(2);


insert into cif.project_revision (
change_status
) values
('pending'),('pending');

insert into cif.form_change(
new_form_data,
operation,
form_data_schema_name,
form_data_table_name,
form_data_record_id,
project_revision_id,
change_status,
change_reason
) values (
'{"revision": "one" }',
'UPDATE',
'fake_schema',
'fake_table',
999,
(select id from cif.project_revision order by id desc limit 1),
'pending',
'Testing form_change delete propagation'
),(
'{"revision": "two" }',
'UPDATE',
'fake_schema',
'fake_table',
123,
(select id from cif.project_revision order by id desc limit 1 offset 1),
'pending',
'Testing form_change delete propagation'
);


select is(
(select new_form_data->>'revision' from cif.form_change where form_data_table_name = 'fake_table' and deleted_at is not null),
null,
'form_change records are not marked deleted upon creation'
);

update cif.project_revision set deleted_at = now() where id = (select id from cif.project_revision order by id desc limit 1);


select is(
(select new_form_data->>'revision' from cif.form_change where form_data_table_name = 'fake_table' and deleted_at is not null),
'one'::text,
'only the form_change from the deleted revision has been deleted'
);

select finish();

rollback;
7 changes: 7 additions & 0 deletions schema/verify/trigger_functions/discard_project_revision.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify cif:trigger_functions/discard_project_revision on pg

begin;

select pg_get_functiondef('cif_private.discard_project_revision()'::regprocedure);

rollback;

0 comments on commit 0a42457

Please sign in to comment.