-
Notifications
You must be signed in to change notification settings - Fork 2
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0df10ac
fix: add function to correct incorrectly calculated annual CO2e values
Sepehr-Sobhani a12895f
chore: add data migration file
Sepehr-Sobhani fd6ad33
test: add pgTap tests for migration function
Sepehr-Sobhani 6731be8
chore: handle pre-commit issues
Sepehr-Sobhani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
47
schema/deploy/database_functions/correct_annual_co2e_values.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) and form_id=2; | ||
|
||
$$ language sql volatile; | ||
|
||
commit; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 7 additions & 0 deletions
7
schema/revert/database_functions/correct_annual_co2e_values.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 |
217 changes: 217 additions & 0 deletions
217
schema/test/unit/database_functions/correct_annual_co2e_values_test.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
7 changes: 7 additions & 0 deletions
7
schema/verify/database_functions/correct_annual_co2e_values.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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