Skip to content

Commit

Permalink
feat: adding discard mutation that removes emission_intensity report
Browse files Browse the repository at this point in the history
  • Loading branch information
gurjmatharu committed Jul 25, 2022
1 parent d841d4e commit 50c3705
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
44 changes: 44 additions & 0 deletions schema/deploy/mutations/discard_emission_intensity_report.sql
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 schema/revert/mutations/discard_emission_intensity_report.sql
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 schema/verify/mutations/discard_emission_intensity_report.sql
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;

0 comments on commit 50c3705

Please sign in to comment.