diff --git a/schema/deploy/data/correct_annual_co2e_values.sql b/schema/deploy/data/correct_annual_co2e_values.sql new file mode 100644 index 000000000..4e645ccc1 --- /dev/null +++ b/schema/deploy/data/correct_annual_co2e_values.sql @@ -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; diff --git a/schema/deploy/database_functions/correct_annual_co2e_values.sql b/schema/deploy/database_functions/correct_annual_co2e_values.sql new file mode 100644 index 000000000..70a51d9f2 --- /dev/null +++ b/schema/deploy/database_functions/correct_annual_co2e_values.sql @@ -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; diff --git a/schema/revert/data/correct_annual_co2e_values.sql b/schema/revert/data/correct_annual_co2e_values.sql new file mode 100644 index 000000000..d9d130455 --- /dev/null +++ b/schema/revert/data/correct_annual_co2e_values.sql @@ -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; diff --git a/schema/revert/database_functions/correct_annual_co2e_values.sql b/schema/revert/database_functions/correct_annual_co2e_values.sql new file mode 100644 index 000000000..26133bd4f --- /dev/null +++ b/schema/revert/database_functions/correct_annual_co2e_values.sql @@ -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; diff --git a/schema/sqitch.plan b/schema/sqitch.plan index 5a2750152..b28d0e1b0 100644 --- a/schema/sqitch.plan +++ b/schema/sqitch.plan @@ -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 # 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 # Migration to correct incorrectly initialized GWP values @v2.22.3 2023-07-14T16:22:57Z Dylan Leard # release v2.22.3 +database_functions/correct_annual_co2e_values 2023-08-10T19:28:59Z Sepehr Sobhani # 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 # Migration to update annual CO2e values that were set incorrectly diff --git a/schema/test/unit/database_functions/correct_annual_co2e_values_test.sql b/schema/test/unit/database_functions/correct_annual_co2e_values_test.sql new file mode 100644 index 000000000..3c89d82c9 --- /dev/null +++ b/schema/test/unit/database_functions/correct_annual_co2e_values_test.sql @@ -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; diff --git a/schema/verify/data/correct_annual_co2e_values.sql b/schema/verify/data/correct_annual_co2e_values.sql new file mode 100644 index 000000000..97c5b4443 --- /dev/null +++ b/schema/verify/data/correct_annual_co2e_values.sql @@ -0,0 +1,7 @@ +-- Verify ggircs-portal:data/correct_annual_co2e_values on pg + +begin; + +-- noverify + +rollback; diff --git a/schema/verify/database_functions/correct_annual_co2e_values.sql b/schema/verify/database_functions/correct_annual_co2e_values.sql new file mode 100644 index 000000000..382b70638 --- /dev/null +++ b/schema/verify/database_functions/correct_annual_co2e_values.sql @@ -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;