Skip to content

Commit

Permalink
feat: import facility bc ghg id from facility table
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Apr 27, 2021
1 parent e3132fa commit d13496e
Show file tree
Hide file tree
Showing 7 changed files with 199 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ begin
swrs.facility.swrs_facility_id,
swrs.facility.facility_name,
swrs.facility.facility_type,
swrs.identifier.identifier_value
swrs.facility.facility_bc_ghg_id
from swrs.facility
join latest_reports on report_id = latest_reports.id
join swrs.report on facility.report_id = report.id
left join swrs.identifier on facility.id = identifier.facility_bcghgid_id
join ggircs_portal.organisation on ggircs_portal.organisation.swrs_organisation_id = swrs.report.swrs_organisation_id
) on conflict(swrs_facility_id) do update
set organisation_id = excluded.organisation_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- Deploy ggircs-portal:function_import_swrs_organisation_facility to pg

begin;

create or replace function ggircs_portal.import_swrs_organisation_facility()
returns void as $function$
begin
with latest_reporting_period as (
select organisation.swrs_organisation_id, max(reporting_period_duration) as last_reporting_period
from swrs.organisation
join swrs.report on organisation.report_id = report.id
group by organisation.swrs_organisation_id
),
latest_reports as (
-- Pick the report from the latest reporting year that was inserted last
-- This applies if there are multiple facilities per organisation
-- The organisation data is expected to be the same for both facilities.
select max(id) as id, report.swrs_organisation_id from swrs.report
join latest_reporting_period
on report.swrs_organisation_id = latest_reporting_period.swrs_organisation_id
and report.reporting_period_duration = latest_reporting_period.last_reporting_period
group by report.swrs_organisation_id
)
insert into ggircs_portal.organisation(report_id, swrs_report_id, swrs_organisation_id, operator_name, cra_business_number)
(
select report_id, swrs_report_id,
swrs.organisation.swrs_organisation_id,
business_legal_name, cra_business_number from swrs.organisation
join latest_reports on report_id = latest_reports.id
join swrs.report on organisation.report_id = report.id
) on conflict(swrs_organisation_id) do update
set report_id = excluded.report_id,
swrs_report_id = excluded.swrs_report_id,
operator_name = excluded.operator_name,
cra_business_number = excluded.cra_business_number;

with latest_reporting_period as (
select facility.swrs_facility_id, max(reporting_period_duration) as last_reporting_period
from swrs.facility
join swrs.report on facility.report_id = report.id
and facility.facility_type != 'LFO' and facility.facility_type != 'EIO'
group by facility.swrs_facility_id
),
latest_reports as (
select id from swrs.report
join latest_reporting_period
on report.swrs_facility_id = latest_reporting_period.swrs_facility_id
and report.reporting_period_duration = latest_reporting_period.last_reporting_period
)
insert into ggircs_portal.facility
(
organisation_id,
report_id,
swrs_report_id,
swrs_facility_id,
facility_name,
facility_type,
bcghgid
)
(
select ggircs_portal.organisation.id,
swrs.report.id,
swrs.report.swrs_report_id,
swrs.facility.swrs_facility_id,
swrs.facility.facility_name,
swrs.facility.facility_type,
swrs.identifier.identifier_value
from swrs.facility
join latest_reports on report_id = latest_reports.id
join swrs.report on facility.report_id = report.id
left join swrs.identifier on facility.id = identifier.facility_bcghgid_id
join ggircs_portal.organisation on ggircs_portal.organisation.swrs_organisation_id = swrs.report.swrs_organisation_id
) on conflict(swrs_facility_id) do update
set organisation_id = excluded.organisation_id,
report_id = excluded.report_id,
swrs_report_id = excluded.swrs_report_id,
facility_name = excluded.facility_name,
facility_type = excluded.facility_type,
bcghgid = excluded.bcghgid;
end;
$function$ language plpgsql strict volatile;

grant execute on function ggircs_portal.import_swrs_organisation_facility to ciip_administrator, ciip_analyst, ciip_industry_user;

commit;
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ begin
business_legal_name, cra_business_number from swrs.organisation
join latest_reports on report_id = latest_reports.id
join swrs.report on organisation.report_id = report.id
) on conflict(swrs_report_id) do update
) on conflict(swrs_organisation_id) do update
set report_id = excluded.report_id,
swrs_organisation_id = excluded.swrs_organisation_id,
swrs_report_id = excluded.swrs_report_id,
operator_name = excluded.operator_name,
cra_business_number = excluded.cra_business_number;

Expand Down Expand Up @@ -70,10 +70,10 @@ begin
join swrs.report on facility.report_id = report.id
left join swrs.identifier on facility.id = identifier.facility_bcghgid_id
join ggircs_portal.organisation on ggircs_portal.organisation.swrs_organisation_id = swrs.report.swrs_organisation_id
) on conflict(swrs_report_id) do update
) on conflict(swrs_facility_id) do update
set organisation_id = excluded.organisation_id,
report_id = excluded.report_id,
swrs_facility_id = excluded.swrs_facility_id,
swrs_report_id = excluded.swrs_report_id,
facility_name = excluded.facility_name,
facility_type = excluded.facility_type,
bcghgid = excluded.bcghgid;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- Deploy ggircs-portal:function_import_swrs_organisation_facility to pg

begin;

create or replace function ggircs_portal.import_swrs_organisation_facility()
returns void as $function$
begin
with latest_reporting_period as (
select organisation.swrs_organisation_id, max(reporting_period_duration) as last_reporting_period
from swrs.organisation
join swrs.report on organisation.report_id = report.id
group by organisation.swrs_organisation_id
),
latest_reports as (
-- Pick the report from the latest reporting year that was inserted last
-- This applies if there are multiple facilities per organisation
-- The organisation data is expected to be the same for both facilities.
select max(id) as id, report.swrs_organisation_id from swrs.report
join latest_reporting_period
on report.swrs_organisation_id = latest_reporting_period.swrs_organisation_id
and report.reporting_period_duration = latest_reporting_period.last_reporting_period
group by report.swrs_organisation_id
)
insert into ggircs_portal.organisation(report_id, swrs_report_id, swrs_organisation_id, operator_name, cra_business_number)
(
select report_id, swrs_report_id,
swrs.organisation.swrs_organisation_id,
business_legal_name, cra_business_number from swrs.organisation
join latest_reports on report_id = latest_reports.id
join swrs.report on organisation.report_id = report.id
) on conflict(swrs_report_id) do update
set report_id = excluded.report_id,
swrs_organisation_id = excluded.swrs_organisation_id,
operator_name = excluded.operator_name,
cra_business_number = excluded.cra_business_number;

with latest_reporting_period as (
select facility.swrs_facility_id, max(reporting_period_duration) as last_reporting_period
from swrs.facility
join swrs.report on facility.report_id = report.id
and facility.facility_type != 'LFO' and facility.facility_type != 'EIO'
group by facility.swrs_facility_id
),
latest_reports as (
select id from swrs.report
join latest_reporting_period
on report.swrs_facility_id = latest_reporting_period.swrs_facility_id
and report.reporting_period_duration = latest_reporting_period.last_reporting_period
)
insert into ggircs_portal.facility
(
organisation_id,
report_id,
swrs_report_id,
swrs_facility_id,
facility_name,
facility_type,
bcghgid
)
(
select ggircs_portal.organisation.id,
swrs.report.id,
swrs.report.swrs_report_id,
swrs.facility.swrs_facility_id,
swrs.facility.facility_name,
swrs.facility.facility_type,
swrs.identifier.identifier_value
from swrs.facility
join latest_reports on report_id = latest_reports.id
join swrs.report on facility.report_id = report.id
left join swrs.identifier on facility.id = identifier.facility_bcghgid_id
join ggircs_portal.organisation on ggircs_portal.organisation.swrs_organisation_id = swrs.report.swrs_organisation_id
) on conflict(swrs_report_id) do update
set organisation_id = excluded.organisation_id,
report_id = excluded.report_id,
swrs_facility_id = excluded.swrs_facility_id,
facility_name = excluded.facility_name,
facility_type = excluded.facility_type,
bcghgid = excluded.bcghgid;
end;
$function$ language plpgsql strict volatile;

grant execute on function ggircs_portal.import_swrs_organisation_facility to ciip_administrator, ciip_analyst, ciip_industry_user;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,4 @@ swrs_functions/get_carbon_tax_data [swrs_functions/get_carbon_tax_data@v2.1.0] 2
tables/application_003 [tables/application_002] 2021-04-26T16:50:55Z Dylan Leard <dylan@button.is> # Migration: add swrs_report_id to application table
mutations/create_application_mutation_chain [mutations/create_application_mutation_chain@v2.1.0] 2021-04-26T17:34:19Z Dylan Leard <dylan@button.is> # Migration: insert swrs_report_id on new application creation
swrs_functions/refresh_swrs_version_data [swrs_functions/refresh_swrs_version_data@v2.1.0 tables/application_003] 2021-04-26T17:49:38Z Dylan Leard <dylan@button.is> # Migration: use swrs_report_id to connect swrs.report to ggircs_portal.application
swrs_functions/import_swrs_organisation_facility [swrs_functions/import_swrs_organisation_facility@v2.1.0] 2021-04-26T23:58:45Z Matthieu Foucault <matthieu@button.is> # import facility bc ghg id from facility table
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ reset client_min_messages;

begin;

select plan(9);
select plan(10);

select has_function(
'ggircs_portal', 'import_swrs_organisation_facility',
Expand Down Expand Up @@ -41,6 +41,21 @@ select set_eq(
'select swrs_facility_id from ggircs_portal.facility',
'The import function imports every swrs_facility_id');

select set_eq(
$$
with facility_latest_report as (
select max(r.reporting_period_duration) as reporting_period_duration, r.swrs_facility_id
from swrs.report r join swrs.facility f on r.id = f.report_id group by r.swrs_facility_id
)
select facility_bc_ghg_id
from swrs.facility f
join swrs.report r on r.id = f.report_id
join facility_latest_report flr on flr.swrs_facility_id = r.swrs_facility_id
and flr.reporting_period_duration = r.reporting_period_duration;
$$,
'select bcghgid from ggircs_portal.facility',
'The import function imports the bc ghg id from the latest report of each facility');

set client_min_messages to warning;
truncate table ggircs_portal.organisation restart identity cascade;
truncate table swrs.report restart identity cascade;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Verify ggircs-portal:function_import_swrs_organisation_facility on pg

begin;

select pg_get_functiondef('ggircs_portal.import_swrs_organisation_facility()'::regprocedure);

rollback;

0 comments on commit d13496e

Please sign in to comment.