Skip to content

Commit

Permalink
feat: remove form_result_status insert from trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Mar 13, 2021
1 parent 215a516 commit 701c338
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 8 deletions.
6 changes: 0 additions & 6 deletions schema/deploy/trigger_functions/checksum_form_results.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ begin
where fr.application_id = new.application_id
and fr.form_id = temp_row.form_id
and fr.version_number = new.version_number - 1)));

if new_form_result_hash != old_form_result_hash then
insert into ggircs_portal.form_result_status(application_id, form_id, form_result_status)
values (new.application_id, temp_row.form_id, 'needs attention');
end if;

end loop;
end if;

Expand Down
61 changes: 61 additions & 0 deletions schema/deploy/trigger_functions/checksum_form_results@v1.16.0.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
-- Deploy ggircs-portal:trigger_functions/checksum_form_results to pg
-- requires: tables/form_result

begin;

create or replace function ggircs_portal_private.checksum_form_results()
returns trigger as $$
declare
new_form_result_hash text;
old_form_result_hash text;
temp_row record;

begin

if new.version_number > 1 then

for temp_row in
select form_id from ggircs_portal.ciip_application_wizard
loop

new_form_result_hash := (select md5((select form_result::text from ggircs_portal.form_result fr
where fr.application_id = new.application_id
and fr.form_id = temp_row.form_id
and fr.version_number = new.version_number)));

old_form_result_hash := (select md5((select form_result::text from ggircs_portal.form_result fr
where fr.application_id = new.application_id
and fr.form_id = temp_row.form_id
and fr.version_number = new.version_number - 1)));

if new_form_result_hash != old_form_result_hash then
insert into ggircs_portal.form_result_status(application_id, form_id, form_result_status)
values (new.application_id, temp_row.form_id, 'needs attention');
end if;

end loop;
end if;

return new;
end;
$$ language plpgsql volatile;

grant execute on function ggircs_portal_private.checksum_form_results to ciip_administrator, ciip_analyst, ciip_industry_user;

comment on function ggircs_portal_private.checksum_form_results()
is $$
a trigger to checksum the form_result columns from the current and previous versions of an application
when the application_revision_status changes.
example usage:

create table some_schema.some_table (
...
application_revision_status jsonb
);
create trigger _checksum_form_results
before update of application_revision_status on some_schema.some_table
for each row
execute procedure ggircs_portal_private.checksum_form_results();
$$;

commit;
58 changes: 56 additions & 2 deletions schema/revert/trigger_functions/checksum_form_results.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,61 @@
-- Revert ggircs-portal:trigger_functions/checksum_form_results from pg
-- Deploy ggircs-portal:trigger_functions/checksum_form_results to pg
-- requires: tables/form_result

begin;

drop function ggircs_portal_private.checksum_form_results;
create or replace function ggircs_portal_private.checksum_form_results()
returns trigger as $$
declare
new_form_result_hash text;
old_form_result_hash text;
temp_row record;

begin

if new.version_number > 1 then

for temp_row in
select form_id from ggircs_portal.ciip_application_wizard
loop

new_form_result_hash := (select md5((select form_result::text from ggircs_portal.form_result fr
where fr.application_id = new.application_id
and fr.form_id = temp_row.form_id
and fr.version_number = new.version_number)));

old_form_result_hash := (select md5((select form_result::text from ggircs_portal.form_result fr
where fr.application_id = new.application_id
and fr.form_id = temp_row.form_id
and fr.version_number = new.version_number - 1)));

if new_form_result_hash != old_form_result_hash then
insert into ggircs_portal.form_result_status(application_id, form_id, form_result_status)
values (new.application_id, temp_row.form_id, 'needs attention');
end if;

end loop;
end if;

return new;
end;
$$ language plpgsql volatile;

grant execute on function ggircs_portal_private.checksum_form_results to ciip_administrator, ciip_analyst, ciip_industry_user;

comment on function ggircs_portal_private.checksum_form_results()
is $$
a trigger to checksum the form_result columns from the current and previous versions of an application
when the application_revision_status changes.
example usage:

create table some_schema.some_table (
...
application_revision_status jsonb
);
create trigger _checksum_form_results
before update of application_revision_status on some_schema.some_table
for each row
execute procedure ggircs_portal_private.checksum_form_results();
$$;

commit;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert ggircs-portal:trigger_functions/checksum_form_results from pg

begin;

drop function ggircs_portal_private.checksum_form_results;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -253,3 +253,4 @@ types/application_search_result [types/application_search_result@v1.16.0] 2021-0
computed_columns/form_result_form_result_statuses [computed_columns/form_result_form_result_statuses@v1.16.0] 2021-03-12T22:52:30Z Dylan Leard <dylan@button.is> # Migration: drop deprecated computed column
mutations/create_application_revision_mutation_chain [mutations/create_application_revision_mutation_chain@v1.16.0] 2021-03-12T22:57:07Z Dylan Leard <dylan@button.is> # Migration: do not create form_result_statuses
mutations/create_review_comment_mutation_chain [mutations/create_review_comment_mutation_chain@v1.16.0] 2021-03-12T22:59:39Z Dylan Leard <dylan@button.is> # Migration: remove form_result_status inserts
trigger_functions/checksum_form_results [trigger_functions/checksum_form_results@v1.16.0] 2021-03-12T23:02:03Z Dylan Leard <dylan@button.is> # Migration: remove form_result_status insert
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:trigger_functions/checksum_form_results on pg

begin;

select pg_get_functiondef('ggircs_portal_private.checksum_form_results()'::regprocedure);

rollback;

0 comments on commit 701c338

Please sign in to comment.