Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: annual co2e values correction #2249

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions schema/deploy/data/correct_annual_co2e_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Deploy ggircs-portal:data/correct_annual_co2e_values to pg
-- requires: database_functions/correct_annual_co2e_values

begin;

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

select ggircs_portal_private.correct_annual_co2e_values();

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

commit;
47 changes: 47 additions & 0 deletions schema/deploy/database_functions/correct_annual_co2e_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
-- Deploy ggircs-portal:database_functions/correct_annual_co2e_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 annual CO2e values in place to the correct AR5 values.
**/

begin;

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

update ggircs_portal.form_result
set form_result = jsonb_set(
form_result,
'{sourceTypes}',
(
select jsonb_agg(
jsonb_set(
sourceType,
'{gases}',
(
select jsonb_agg(
jsonb_set(
gas,
'{annualCO2e}',
((gas->>'gwp')::numeric * (gas->>'annualEmission')::numeric)::text::jsonb
)
)from jsonb_array_elements(sourceType->'gases') gas
)
)
) from jsonb_array_elements(form_result->'sourceTypes') sourceType
)
)
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-04-01'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can scope this further to only update the applications that were created before the gwp change was deployed to prod

) and form_id=2;

$$ language sql volatile;

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

begin;

-- No need to revert the function because it is just re-calculating values based on the gwp values and the annualEmission values

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

begin;

drop function ggircs_portal_private.correct_annual_co2e_values;

commit;
2 changes: 2 additions & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,5 @@ trigger_functions/run_graphile_worker_job [trigger_functions/run_graphile_worker
database_functions/correct_gwp_values 2023-07-13T18:31:01Z Dylan Leard <[email protected]> # 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 <[email protected]> # Migration to correct incorrectly initialized GWP values
@v2.22.3 2023-07-14T16:22:57Z Dylan Leard <[email protected]> # release v2.22.3
database_functions/correct_annual_co2e_values 2023-08-10T19:28:59Z Sepehr Sobhani <[email protected]> # Function to update annual CO2e values that were set incorrectly
data/correct_annual_co2e_values [database_functions/correct_annual_co2e_values] 2023-08-11T17:06:22Z Sepehr Sobhani <[email protected]> # Migration to update annual CO2e values that were set incorrectly
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;
select plan(8);

-- Clean schema
truncate ggircs_portal.application restart identity cascade;
alter table ggircs_portal.ciip_user disable trigger _welcome_email;
select test_helper.create_test_users();
select test_helper.mock_open_window();
-- Create facilities / applications
select test_helper.create_applications(3, True, True);

update ggircs_portal.application set reporting_year = 2022 where id != 3;
update ggircs_portal.application set reporting_year = 2021 where id = 3;
update ggircs_portal.application_revision set created_at = '2023-01-01'::timestamptz where application_id=1; -- created before April 1st 2023 so should be unaffected
update ggircs_portal.application_revision set created_at = '2023-05-01'::timestamptz where application_id=2; -- created after April 1st 2023 and has wrong annual CO2e values
update ggircs_portal.application_revision set created_at = '2023-06-01'::timestamptz where application_id=3; -- reporting year 2021 so should be unaffected

-- setting all form results to the same value intentionally to remove default values
update ggircs_portal.form_result set form_result='{"test": "dont change me"}'::jsonb;

-- Wrong annual CO2e values for application ID=2 and form ID=2
update ggircs_portal.form_result
set form_result =
'{"sourceTypes": [
{"gases": [
{"gwp": 10, "gasType": "CO2nonbio", "annualCO2e": 50, "annualEmission": 5, "gasDescription": "Carbon dioxide from non-biomass"},
{"gwp": 28, "gasType": "CH4", "annualCO2e": 40, "annualEmission": 20, "gasDescription": "Methane"}],
"sourceTypeName": "General Stationary Combustion"
},
{"gases": [
{"gwp": 1, "gasType": "CO2nonbio", "annualCO2e": 60, "annualEmission": 30, "gasDescription": "Carbon dioxide from non-biomass"},
{"gwp": 25, "gasType": "CH4", "annualCO2e": 90, "annualEmission": 45, "gasDescription": "Methane"},
{"gwp": 298, "gasType": "N2O", "annualCO2e": 100, "annualEmission": 50, "gasDescription": "Nitrous oxide"}],
"sourceTypeName": "General Stationary Combustion"
}
]}'
where application_id=2 and form_id=2;

-- Running the function
alter table ggircs_portal.form_result disable trigger _100_timestamps, disable trigger _immutable_form_result;

select ggircs_portal_private.correct_annual_co2e_values();

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

-- Test 1
select results_eq(
$$
select form_result from ggircs_portal.form_result where application_id=1;
$$,
$$
values
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb)
$$,
'Application with ID 1 was unaffected because it was created before April 1st 2023'
);

-- Test 2
select set_eq (
$$
select form_result from ggircs_portal.form_result where application_id=2 and form_id=2;
$$,
$$
values
('{"sourceTypes": [
{"gases": [
{"gwp": 10, "gasType": "CO2nonbio", "annualCO2e": 50, "annualEmission": 5, "gasDescription": "Carbon dioxide from non-biomass"},
{"gwp": 28, "gasType": "CH4", "annualCO2e": 560, "annualEmission": 20, "gasDescription": "Methane"}],
"sourceTypeName": "General Stationary Combustion"
},
{"gases": [
{"gwp": 1, "gasType": "CO2nonbio", "annualCO2e": 30, "annualEmission": 30, "gasDescription": "Carbon dioxide from non-biomass"},
{"gwp": 25, "gasType": "CH4", "annualCO2e": 1125, "annualEmission": 45, "gasDescription": "Methane"},
{"gwp": 298, "gasType": "N2O", "annualCO2e": 14900, "annualEmission": 50, "gasDescription": "Nitrous oxide"}],
"sourceTypeName": "General Stationary Combustion"
}
]}'::jsonb)
$$,
'Application with ID 2 and form ID 2 was updated because it was created after April 1st 2023'
);

-- Test 3
select results_eq(
$$
select form_result from ggircs_portal.form_result where application_id=2 and form_id!=2;
$$,
$$
values
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb)
$$,
'Application with ID 2 and form IDs except 2 were not changed'
);

-- Test 4
select results_eq(
$$
select form_result from ggircs_portal.form_result where application_id=3;
$$,
$$
values
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb)
$$,
'Application with ID 3 was unaffected because it is from the 2021 reporting year'
);

-- Below assertions are to make sure that the function is idempotent
alter table ggircs_portal.form_result disable trigger _100_timestamps, disable trigger _immutable_form_result;

select ggircs_portal_private.correct_annual_co2e_values();

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

-- Test 5
select results_eq(
$$
select form_result from ggircs_portal.form_result where application_id=1;
$$,
$$
values
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb)
$$,
'Application with ID 1 was unaffected because it was created before April 1st 2023'
);

-- Test 6
select set_eq (
$$
select form_result from ggircs_portal.form_result where application_id=2 and form_id=2;
$$,
$$
values
('{"sourceTypes": [
{"gases": [
{"gwp": 10, "gasType": "CO2nonbio", "annualCO2e": 50, "annualEmission": 5, "gasDescription": "Carbon dioxide from non-biomass"},
{"gwp": 28, "gasType": "CH4", "annualCO2e": 560, "annualEmission": 20, "gasDescription": "Methane"}],
"sourceTypeName": "General Stationary Combustion"
},
{"gases": [
{"gwp": 1, "gasType": "CO2nonbio", "annualCO2e": 30, "annualEmission": 30, "gasDescription": "Carbon dioxide from non-biomass"},
{"gwp": 25, "gasType": "CH4", "annualCO2e": 1125, "annualEmission": 45, "gasDescription": "Methane"},
{"gwp": 298, "gasType": "N2O", "annualCO2e": 14900, "annualEmission": 50, "gasDescription": "Nitrous oxide"}],
"sourceTypeName": "General Stationary Combustion"
}
]}'::jsonb)
$$,
'Application with ID 2 and form ID 2 was updated because it was created after April 1st 2023'
);

-- Test 7
select results_eq(
$$
select form_result from ggircs_portal.form_result where application_id=2 and form_id!=2;
$$,
$$
values
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb)
$$,
'Application with ID 2 and form IDs except 2 were not changed'
);

-- Test 8
select results_eq(
$$
select form_result from ggircs_portal.form_result where application_id=3;
$$,
$$
values
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb),
('{"test": "dont change me"}'::jsonb)
$$,
'Application with ID 3 was unaffected because it is from the 2021 reporting year'
);

select finish();

rollback;
7 changes: 7 additions & 0 deletions schema/verify/data/correct_annual_co2e_values.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:data/correct_annual_co2e_values on pg

begin;

-- noverify

rollback;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:database_functions/correct_annual_co2e_values on pg

begin;

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

rollback;
Loading