-
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 function making sure the committed changes are immutable
- Loading branch information
Showing
5 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
schema/deploy/trigger_functions/committed_changes_are_immutable.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,24 @@ | ||
-- Deploy cif:trigger_functions/committed_changes_are_immutable to pg | ||
|
||
begin; | ||
|
||
create or replace function cif_private.committed_changes_are_immutable() | ||
returns trigger as $$ | ||
begin | ||
if (select triggers_commit from cif.change_status where status=old.change_status) then | ||
raise exception 'Committed records cannot be modified'; | ||
end if; | ||
|
||
return new; | ||
end; | ||
$$ language plpgsql; | ||
|
||
grant execute on function cif_private.committed_changes_are_immutable to cif_internal, cif_external, cif_admin; | ||
|
||
comment on function cif_private.committed_changes_are_immutable() | ||
is $$ | ||
A trigger that raises an exception if changes happen on a record where the change_status triggers a commit. | ||
$$; | ||
|
||
|
||
commit; |
7 changes: 7 additions & 0 deletions
7
schema/revert/trigger_functions/committed_changes_are_immutable.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,7 @@ | ||
-- Revert cif:trigger_functions/committed_changes_are_immutable from pg | ||
|
||
begin; | ||
|
||
drop function cif_private.committed_changes_are_immutable; | ||
|
||
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 |
---|---|---|
|
@@ -29,6 +29,7 @@ tables/project [tables/operator] 2021-11-04T20:34:23Z Pierre Bastianelli <pierre | |
util_functions/camel_to_snake_case 2021-12-11T00:37:54Z Matthieu Foucault <[email protected]> # Add cif_private.camel_to_snake_case | ||
trigger_functions/commit_form_change [schemas/private util_functions/camel_to_snake_case] 2021-11-08T22:54:27Z Dylan Leard <[email protected]> # Trigger function to apply changes to a table once the change has been committed | ||
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 | ||
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 | ||
|
41 changes: 41 additions & 0 deletions
41
schema/test/unit/trigger_functions/committed_changes_are_immutable_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,41 @@ | ||
begin; | ||
|
||
select plan(3); | ||
|
||
-- Testing table with a deleted_at column | ||
|
||
insert into cif.change_status (status, triggers_commit, active) values ('testcommitted', true, true), ('testpending', false, true); | ||
|
||
create table test_table_with_status( | ||
test_col text, | ||
change_status varchar(1000) references cif.change_status | ||
); | ||
|
||
create trigger trigger_under_test before update on test_table_with_status for each row | ||
execute procedure cif_private.committed_changes_are_immutable(); | ||
|
||
insert into test_table_with_status(test_col, change_status) values ('test_active', 'testpending'), ('test_committed', 'testcommitted'); | ||
|
||
select lives_ok( | ||
$$ | ||
update test_table_with_status set test_col = 'test_changed_active' where test_col = 'test_active' | ||
$$, | ||
'doesnt throw if the change status isn''t committed' | ||
); | ||
|
||
select is( | ||
(select count(*) from test_table_with_status where test_col = 'test_changed_active'), | ||
1::bigint, | ||
'allows the record to be updated if the change status isn''t committed' | ||
); | ||
|
||
select throws_ok( | ||
$$ | ||
update test_table_with_status set test_col = 'test_changed_committed' where test_col = 'test_committed' | ||
$$, | ||
'Committed records cannot be modified', | ||
'throws if the change_status is committed' | ||
); | ||
|
||
|
||
rollback; |
7 changes: 7 additions & 0 deletions
7
schema/verify/trigger_functions/committed_changes_are_immutable.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,7 @@ | ||
-- Verify cif:trigger_functions/committed_changes_are_immutable on pg | ||
|
||
begin; | ||
|
||
select pg_get_functiondef('cif_private.committed_changes_are_immutable()'::regprocedure); | ||
|
||
rollback; |