Skip to content

Commit

Permalink
feat: trigger function making sure the committed changes are immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Jan 7, 2022
1 parent b062c0e commit 162f3f8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
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;
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;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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;
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;

0 comments on commit 162f3f8

Please sign in to comment.