Skip to content

Commit

Permalink
Merge pull request #2956 from bcgov/batch_spatial_trigger_fix
Browse files Browse the repository at this point in the history
make context autofill handle overlapping geometries
  • Loading branch information
micheal-w-wells authored Nov 1, 2023
2 parents 2facf04 + e306cd9 commit 2b56e0f
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions database/src/migrations/0074_batch_spatial_trigger_fix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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.context_autofill()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
UPDATE invasivesbc.activity_incoming_data
SET invasive_plant_management_areas = (
select
ipma
from
public.invasive_plant_management_areas
where
public.st_intersects(
public.st_geographyFromText(('POINT('::TEXT || (activity_payload->'form_data'->'activity_data'->>'longitude')::TEXT || ' '::TEXT || (activity_payload->'form_data'->'activity_data'->>'latitude')::TEXT || ')'::TEXT)::TEXT),
invasive_plant_management_areas.geog
) limit 1
),
regional_invasive_species_organization_areas = (
select
string_agg(agency, ', ')
from
public.regional_invasive_species_organization_areas
where
public.st_intersects(
public.st_geographyFromText(('POINT('::TEXT || (activity_payload->'form_data'->'activity_data'->>'longitude')::TEXT || ' '::TEXT || (activity_payload->'form_data'->'activity_data'->>'latitude')::TEXT || ')'::TEXT)::TEXT),
regional_invasive_species_organization_areas.geog
)
),
regional_districts = (
select
agency
from
public.regional_districts
where
public.st_intersects(
public.st_geographyFromText(('POINT('::TEXT || (activity_payload->'form_data'->'activity_data'->>'longitude')::TEXT || ' '::TEXT || (activity_payload->'form_data'->'activity_data'->>'latitude')::TEXT || ')'::TEXT)::TEXT),
regional_districts.geog
) limit 1
)
WHERE activity_id = NEW.activity_id;
RETURN NEW;
END;
$function$
;
`
);
}

export async function down(knex: Knex): Promise<void> {
await knex.raw(`
set search_path='invasivesbc';
`);
}

0 comments on commit 2b56e0f

Please sign in to comment.