Skip to content

Commit

Permalink
feat: add trigger_function to create/refresh review_step
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Mar 12, 2021
1 parent 71c3cd9 commit 9fa2c21
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
47 changes: 47 additions & 0 deletions schema/deploy/trigger_functions/create_or_refresh_review_step.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- Deploy ggircs-portal:trigger_functions/create_or_refresh_review_step to pg
-- requires: tables/review_step

begin;

create or replace function ggircs_portal_private.create_or_refresh_review_step()
returns trigger as $$

declare
temp_row record;

begin

for temp_row in
select id, step_name from ggircs_portal.review_step_name where is_active = true
loop
if exists(select * from ggircs_portal.review_step where application_id = new.application_id and review_step_name_id = temp_row.id) then
update ggircs_portal.review_step set is_complete = false where application_id = new.application_id and review_step_name_id = temp_row.id;
else
insert into ggircs_portal.review_step(application_id, review_step_name_id)
values (new.application_id, temp_row.id);
end if;
end loop;

return new;
end;
$$ language plpgsql volatile;

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

comment on function ggircs_portal_private.create_or_refresh_review_step()
is $$
a trigger to create a review step for an application if it does not exist else reset the is_complete value to false
when the application_revision_status changes to submitted.
example usage:

create table some_schema.some_table (
...
application_revision_status ggircs_portal.application_revision_status
);
create trigger _create_or_refresh_review_step
before insert of application_revision_status on some_schema.some_table
for each row
execute procedure ggircs_portal_private.create_or_refresh_review_step();
$$;

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

begin;

drop function ggircs_portal_private.create_or_refresh_review_step;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,4 @@ tables/review_step_name [schema_ggircs_portal] 2021-03-09T22:42:29Z Dylan Leard
policies/review_step_name_policies [tables/review_step_name] 2021-03-10T19:00:05Z Dylan Leard <dylan@button.is> # RLS policies for table review_step_name
tables/review_step [tables/application] 2021-03-10T00:00:08Z Dylan Leard <dylan@button.is> # review_step table contains data on each review_step an application must go through before a status change
policies/review_step_policies [tables/review_step] 2021-03-10T00:25:14Z Dylan Leard <dylan@button.is> # Row level security policy definitions for the review_step table
trigger_functions/create_or_refresh_review_step [tables/review_step] 2021-03-10T00:33:49Z Dylan Leard <dylan@button.is> # Trigger function creates review steps on application submit if they do not exist, else refreshes the is_complete column
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:trigger_functions/create_or_refresh_review_step on pg

begin;

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

rollback;

0 comments on commit 9fa2c21

Please sign in to comment.