-
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: trigger to propagate project_revision deletion to the form_chan…
…ge records
- Loading branch information
Showing
6 changed files
with
98 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
17 changes: 17 additions & 0 deletions
17
schema/deploy/trigger_functions/discard_project_revision.sql
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,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; |
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:trigger_functions/discard_project_revision from pg | ||
|
||
begin; | ||
|
||
drop function cif_private.discard_project_revision; | ||
|
||
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 |
---|---|---|
|
@@ -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 | ||
|
60 changes: 60 additions & 0 deletions
60
schema/test/unit/trigger_functions/discard_project_revision_test.sql
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,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; |
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:trigger_functions/discard_project_revision on pg | ||
|
||
begin; | ||
|
||
select pg_get_functiondef('cif_private.discard_project_revision()'::regprocedure); | ||
|
||
rollback; |