Skip to content

Commit

Permalink
fix: rework org/facility import function with new on conflict keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dleard committed Jun 25, 2020
1 parent f380885 commit d247720
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 6 deletions.
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;
82 changes: 80 additions & 2 deletions schema/revert/swrs_functions/import_swrs_organisation_facility.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,85 @@
-- Revert ggircs-portal:function_import_swrs_organisation_facility from pg
-- Deploy ggircs-portal:function_import_swrs_organisation_facility to pg

begin;

drop function ggircs_portal.import_swrs_organisation_facility;
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert ggircs-portal:function_import_swrs_organisation_facility from pg

begin;

drop function ggircs_portal.import_swrs_organisation_facility;

commit;
1 change: 1 addition & 0 deletions schema/sqitch.plan
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,4 @@ search_functions/search_certification_requests [search_functions/search_certific
policies/facility_policies [policies/facility_policies@v1.0.0-rc.5] 2020-06-25T18:19:13Z Dylan Leard <dylan@button.is> # Migration: add organisation request approved requirement
database_functions/get_valid_applications_for_reporter [database_functions/get_valid_applications_for_reporter@v1.0.0-rc.5] 2020-06-25T18:22:04Z Dylan Leard <dylan@button.is> # Migration: add organisation request approved requirement to rls
computed_columns/facility_has_swrs_report [computed_columns/facility_has_swrs_report@v1.0.0-rc.5 function_opened_reporting_year] 2020-06-24T23:54:56Z Dylan Leard <dylan@button.is> # Migration: removes reporting_year parameter & retrieves current year via a function
swrs_functions/import_swrs_organisation_facility [swrs_functions/import_swrs_organisation_facility@v1.0.0-rc.5] 2020-06-25T21:18:57Z Dylan Leard <dylan@button.is> # Migration: update on conflict columns
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 d247720

Please sign in to comment.