Skip to content

Commit

Permalink
feat: add refres_swrs_version_data db function
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Mar 29, 2021
1 parent aff729f commit 83e1785
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
54 changes: 54 additions & 0 deletions schema/deploy/swrs_functions/refresh_swrs_version_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
-- Deploy ggircs-portal:swrs_functions/refresh_swrs_version_data to pg
-- requires: tables/form_result
-- requires: swrs_functions/init_application_administration_form_result
-- requires: swrs_functions/init_application_emission_form_result
-- requires: swrs_functions/init_application_fuel_form_result

begin;

create or replace function ggircs_portal_private.refresh_swrs_version_data() returns void
as $function$
declare
report_imported_at timestamptz;
application_temp_row record;
form_json_temp_row record;
init_function varchar(1000);
new_form_result jsonb;
query text;
begin

for application_temp_row in select * from ggircs_portal.application
loop
report_imported_at := (select imported_at from swrs.report r where r.id = temp_row.report_id);

for form_json_temp_row in select form_id from ggircs_portal.ciip_application_wizard where is_active=true
loop

if (select updated_at from ggircs_portal.form_result fr
where fr.form_id = form_json_temp_row.form_id
and fr.application_id = application_temp_row.id
and fr.version_number = 0) < report_imported_at
then

select form_result_init_function from ggircs_portal.form_json fj where fj.id = form_json_temp_row.form_id into init_function;
if (init_function is not null) then
query := format('select * from ggircs_portal.%I($1, $2);', init_function);
execute query
using application_temp_row.facility_id, application_temp_row.reporting_year
into new_form_result;
update ggircs_portal.form_result fr set fr.form_result=new_form_result
where fr.application_id = application_temp_row.id
and fr.version_number = 0
and fr.form_id = form_json_temp_row.form_id;
end if;
end if;
end loop;
end loop;

end;

$function$ language plpgsql volatile;

comment on function ggircs_portal_private.refresh_swrs_version_data is 'This function updates the form results relating to the swrs version of an application (version 0) if the form results were updated prior to the import date of the report';

commit;
7 changes: 7 additions & 0 deletions schema/revert/swrs_functions/refresh_swrs_version_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert ggircs-portal:swrs_functions/refresh_swrs_version_data from pg

begin;

drop function ggircs_portal_private.refresh_swrs_version_data;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,4 @@ trigger_functions/run_graphile_worker_job [trigger_functions/run_graphile_worker
tables/certification_url_001 [tables/certification_url] 2021-03-15T21:50:02Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # dropping triggers for removed functions
trigger_functions/signature_md5 [trigger_functions/signature_md5@v1.16.0] 2021-03-15T22:58:28Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # removing since we don't certify applications anymore
trigger_functions/set_user_id [trigger_functions/set_user_id@v1.16.0] 2021-03-15T23:10:29Z Pierre Bastianelli <pierre.bastianelli@gov.bc.ca> # removing certification option
swrs_functions/refresh_swrs_version_data [tables/form_result swrs_functions/init_application_administration_form_result swrs_functions/init_application_emission_form_result swrs_functions/init_application_fuel_form_result] 2021-03-24T23:42:37Z Dylan Leard <dylan@button.is> # Function refreshes the swrs version (version 0) of an application with updated data from swrs
7 changes: 7 additions & 0 deletions schema/verify/swrs_functions/refresh_swrs_version_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:swrs_functions/refresh_swrs_version_data on pg

begin;

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

rollback;

0 comments on commit 83e1785

Please sign in to comment.