-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding discard mutation that removes emission_intensity report
- Loading branch information
1 parent
d841d4e
commit 50c3705
Showing
3 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
schema/deploy/mutations/discard_emission_intensity_report.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,44 @@ | ||
-- Deploy cif:mutations/discard_emission_intensity_report to pg | ||
-- requires: tables/form_change | ||
-- requires: tables/project_revision | ||
|
||
begin; | ||
|
||
/** | ||
Removing or archiving a emission intensitry report is a chained operation. The data for milestones is spread across two tables: | ||
- reporting_requirement (base table, common to all reports) | ||
- discard_emission_intensity_report (data specific to milestone reports) | ||
Because this data is spread across two tables we have to remove or archive two form_change records within one transaction, one for each table. | ||
**/ | ||
|
||
create or replace function cif.discard_emission_intensity_report(revision_id int) | ||
returns setof cif.form_change | ||
as $discard_emission_intensity_report$ | ||
declare | ||
form_change_record record; | ||
|
||
begin | ||
|
||
for form_change_record in select * from cif.form_change | ||
where project_revision_id = $1 | ||
and ( | ||
(form_data_table_name = 'reporting_requirement' and new_form_data->>'reportType' = 'TEIMP') | ||
or | ||
form_data_table_name = 'emission_intensity_report' | ||
) | ||
loop | ||
if form_change_record.operation = 'create' then | ||
delete from cif.form_change where id = form_change_record.id; | ||
return next form_change_record; | ||
else | ||
update cif.form_change set operation = 'archive' where id = form_change_record.id; | ||
return next form_change_record; | ||
end if; | ||
end loop; | ||
|
||
end; | ||
|
||
$discard_emission_intensity_report$ language plpgsql volatile; | ||
grant execute on function cif.discard_emission_intensity_report to cif_internal, cif_external, cif_admin; | ||
|
||
commit; |
8 changes: 8 additions & 0 deletions
8
schema/revert/mutations/discard_emission_intensity_report.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,8 @@ | ||
-- Revert cif:mutations/discard_emission_intensity_report from pg | ||
|
||
begin; | ||
|
||
drop function cif.discard_emission_intensity_report; | ||
|
||
commit; | ||
|
7 changes: 7 additions & 0 deletions
7
schema/verify/mutations/discard_emission_intensity_report.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 cif:mutations/discard_emission_intensity_report on pg | ||
|
||
BEGIN; | ||
|
||
-- XXX Add verifications here. | ||
|
||
ROLLBACK; |