-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2956 from bcgov/batch_spatial_trigger_fix
make context autofill handle overlapping geometries
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
`); | ||
} |