Skip to content

Commit

Permalink
Merge pull request #2246 from bcgov/fix/gwp-values-correction
Browse files Browse the repository at this point in the history
Fix: gwp values correction
  • Loading branch information
dleard authored Jul 13, 2023
2 parents 4b693c5 + 46458ff commit 2978cfc
Show file tree
Hide file tree
Showing 8 changed files with 449 additions and 0 deletions.
12 changes: 12 additions & 0 deletions schema/deploy/data/correct_gwp_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Deploy ggircs-portal:data/correct_gwp_values to pg
-- requires: database_functions/correct_gwp_values

begin;

alter table ggircs_portal.form_result disable trigger _100_timestamps, disable trigger _immutable_form_result;

select ggircs_portal_private.correct_gwp_values();

alter table ggircs_portal.form_result enable trigger _100_timestamps, enable trigger _immutable_form_result;

commit;
113 changes: 113 additions & 0 deletions schema/deploy/database_functions/correct_gwp_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
-- Deploy ggircs-portal:database_functions/correct_gwp_values to pg

/**
Some applications during the 2023 reporting cycle (2022 reporting year) were created before the correct gwp
values were applied, which means when the applications were initialized the old AR4 values were applied.
This function updates the gwp values in place to the correct AR5 values.
**/

begin;

create or replace function ggircs_portal_private.correct_gwp_values()
returns void as $$

-- CH4
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 25', '"gwp": 28')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- N2O
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 298', '"gwp": 265')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- SF6
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 22800', '"gwp": 23500')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- CF4
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 7390', '"gwp": 6630')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- C2F6
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 12200', '"gwp": 11100')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- CH2F2
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 675', '"gwp": 677')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- CH2HF5
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 3500', '"gwp": 3170')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- C2H2F4
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 1430', '"gwp": 1300')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;

$$ language sql volatile;

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

begin;

alter table ggircs_portal.form_result disable trigger _100_timestamps, disable trigger _immutable_form_result;

-- CH4
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 28', '"gwp": 25')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- N2O
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 265', '"gwp": 298')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- SF6
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 23500', '"gwp": 22800')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- CF4
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 6630', '"gwp": 7390')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- C2F6
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 11100', '"gwp": 12200')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- CH2F2
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 677', '"gwp": 675')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- CH2HF5
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 3170', '"gwp": 3500')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;
-- C2H2F4
update ggircs_portal.form_result
set form_result = replace(form_result::text, '"gwp": 1300', '"gwp": 1430')::jsonb
where application_id in (
select application_id
from ggircs_portal.application_revision ar
join ggircs_portal.application a
on a.id = ar.application_id
and a.reporting_year = 2022
and ar.created_at < '2023-05-30' and ar.created_at > '2023-04-01'
)
and form_id=2;

alter table ggircs_portal.form_result enable trigger _100_timestamps, enable trigger _immutable_form_result;

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

begin;

drop function ggircs_portal_private.correct_gwp_values;

commit;
2 changes: 2 additions & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,5 @@ mutations/create_or_update_ciip_user [mutations/create_or_update_ciip_user@v2.22
@v2.22.1 2023-06-06T22:35:02Z Dylan Leard <dylan@button.is> # release v2.22.1
@v2.22.2 2023-06-13T22:39:59Z Dylan Leard <dylan@button.is> # release v2.22.2
trigger_functions/run_graphile_worker_job [trigger_functions/run_graphile_worker_job@v2.22.1] 2023-07-04T20:06:19Z Dylan Leard <dylan@button.is> # Migration: notify reporters of application submission
database_functions/correct_gwp_values 2023-07-13T18:31:01Z Dylan Leard <dylan@button.is> # Function to update gwp values that were set incorrectly
data/correct_gwp_values [database_functions/correct_gwp_values] 2023-07-13T18:43:50Z Dylan Leard <dylan@button.is> # Migration to correct incorrectly initialized GWP values
Loading

0 comments on commit 2978cfc

Please sign in to comment.