Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update code to name function for new postgres version #2980

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 170 additions & 0 deletions database/src/migrations/0077_update_code_to_name_function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
import { Knex } from 'knex';

export async function up(knex: Knex): Promise<void> {
await knex.raw(
`
SET search_path = invasivesbc,public;

CREATE OR REPLACE FUNCTION invasivesbc.invasives_array_coalesce_to_jsonb(input jsonb)
RETURNS jsonb
LANGUAGE plpgsql
AS
$function$
BEGIN
if jsonb_typeof(input) <> 'array' or input = '[]' then
return '[null]';
else
return jsonb_array_elements(input);
END if;
END
$function$
;

CREATE OR REPLACE FUNCTION invasivesbc.invasives_array_coalesce_to_text(input jsonb)
RETURNS text
LANGUAGE plpgsql
AS
$function$
BEGIN
if jsonb_typeof(input) <> 'array' or input = '[]' then
return '[null]';
else
return jsonb_array_elements_text(input);
END if;
END
$function$
;


CREATE OR REPLACE FUNCTION invasivesbc.code_to_name()
RETURNS trigger
LANGUAGE plpgsql
AS
$function$
BEGIN
set search_path = invasivesbc,public;


WITH species_code_by_activity_id AS (SELECT aid.activity_incoming_data_id,
aid.activity_id,

invasives_array_coalesce_to_text(species_positive) as positive_code,
invasives_array_coalesce_to_text(species_negative) as negative_code,
invasives_array_coalesce_to_text(species_treated) as treated_code,

invasives_array_coalesce_to_jsonb(coalesce(activity_payload #>
'{form_data, activity_subtype_data, Biocontrol_Collection_Information}',
activity_payload #>
'{form_data, activity_subtype_data, Monitoring_BiocontrolDispersal_Information}',
activity_payload #>
'{form_data, activity_subtype_data, Monitoring_BiocontrolRelease_TerrestrialPlant_Information}',
activity_payload #>
'{form_data, activity_subtype_data, Biocontrol_Release_Information}'))
AS biocontrol_code

FROM invasivesbc.activity_incoming_data aid
inner join invasivesbc.activity_current ac
on ac.incoming_data_id = aid.activity_incoming_data_id

where ac.activity_id = new.activity_id
GROUP BY activity_incoming_data_id),
species_positive_translated_by_activity_id as (select im.char_code,
sid.positive_code,
im.invbc_name,
sid.activity_id,
sid.activity_incoming_data_id
from iapp_invbc_mapping im
inner join species_code_by_activity_id sid on sid.positive_code = im.char_code),
species_negative_translated_by_activity_id as (select im.char_code,
sid.negative_code,
im.invbc_name,
sid.activity_id,
sid.activity_incoming_data_id
from iapp_invbc_mapping im
inner join species_code_by_activity_id sid on sid.negative_code = im.char_code),
species_treated_translated_by_activity_id as (select im.char_code,
sid.treated_code,
im.invbc_name,
sid.activity_id,
sid.activity_incoming_data_id
from iapp_invbc_mapping im
inner join species_code_by_activity_id sid on sid.treated_code = im.char_code),
species_biocontrol_translated_by_activity_id as (select im.code_name,
sid.biocontrol_code ->> 'biological_agent_code',
im.code_description,
sid.activity_id,
sid.activity_incoming_data_id
from code im
inner join species_code_by_activity_id sid
on sid.biocontrol_code ->> 'biological_agent_code' = im.code_name
where im.code_header_id = 43),
species_positive_description_aggregated_by_activity_id
as (select array_to_string(ARRAY_AGG(st.invbc_name), ', ') as species_positive_full,
st.activity_id,
st.activity_incoming_data_id
from species_positive_translated_by_activity_id st
group by st.activity_id, st.activity_incoming_data_id),
species_negative_description_aggregated_by_activity_id
as (select array_to_string(ARRAY_AGG(st.invbc_name), ', ') as species_negative_full,
st.activity_id,
st.activity_incoming_data_id
from species_negative_translated_by_activity_id st
group by st.activity_id, st.activity_incoming_data_id),
species_treated_description_aggregated_by_activity_id
as (select array_to_string(ARRAY_AGG(st.invbc_name), ', ') as species_treated_full,
st.activity_id,
st.activity_incoming_data_id
from species_treated_translated_by_activity_id st
group by st.activity_id, st.activity_incoming_data_id),
species_biocontrol_description_aggregated_by_activity_id
as (select array_to_string(ARRAY_AGG(st.code_description), ', ') as species_biocontrol_full,
st.activity_id,
st.activity_incoming_data_id
from species_biocontrol_translated_by_activity_id st
group by st.activity_id, st.activity_incoming_data_id),
species_full_descriptions_by_activity_id as (select ac.activity_id,
ac.incoming_data_id AS activity_incoming_data_id,
species_positive_full,
species_negative_full,
species_treated_full,
species_biocontrol_full
from activity_current ac
left join species_positive_description_aggregated_by_activity_id sp
on sp.activity_id = ac.activity_id
left join species_negative_description_aggregated_by_activity_id sn
on sn.activity_id = ac.activity_id
left join species_treated_description_aggregated_by_activity_id st
on st.activity_id = ac.activity_id
left join species_biocontrol_description_aggregated_by_activity_id sb
on sb.activity_id = ac.activity_id

where ac.activity_id = new.activity_id)

UPDATE activity_incoming_data aid
set species_positive_full = sf.species_positive_full,
species_negative_full = sf.species_negative_full,
species_treated_full = sf.species_treated_full,
species_biocontrol_full = sf.species_biocontrol_full

FROM species_full_descriptions_by_activity_id sf

WHERE aid.activity_incoming_data_id = sf.activity_incoming_data_id
and aid.activity_incoming_data_id = new.activity_incoming_data_id
and aid.activity_id = new.activity_id;

RETURN NEW;
END
$function$
;

`
);
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`

set search_path=invasivesbc,public;

`);
}
Loading