Skip to content

Commit

Permalink
Merge pull request #1253 from cityofaustin/12744_replace_lambda_city_id
Browse files Browse the repository at this point in the history
Replace lambda function to update city_id
  • Loading branch information
roseeichelmann authored Jul 26, 2023
2 parents 65af31e + 512c42f commit c5000cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- This trigger updates the city id of a crash to be in Austin if its currently
-- not in Austin and the position value is moved into any of the Austin jurisdictions
CREATE OR REPLACE TRIGGER crashes_position_update_city_id
BEFORE UPDATE ON atd_txdot_crashes
FOR EACH ROW
WHEN (OLD.position IS DISTINCT FROM NEW.position AND NEW.city_id != 22)
EXECUTE FUNCTION update_crash_city_id();
27 changes: 27 additions & 0 deletions atd-vzd/triggers/update_crash_city_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-- This function updates the city id if a crashes position is moved into Austin
CREATE OR REPLACE FUNCTION update_crash_city_id()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
DECLARE
temprow RECORD;
BEGIN
-- loop through jurisdictions that contain the new position
-- if jurisdiction is in the list of valid jurisdictions then
-- update city id to be in Austin (22)
FOR temprow IN
(
SELECT id
FROM atd_jurisdictions
WHERE (atd_jurisdictions.geometry && NEW.position) AND ST_Contains(atd_jurisdictions.geometry, NEW.position)
)
LOOP
IF temprow.id IN (5, 3, 7, 8, 10) THEN
NEW.city_id = 22;
RETURN NEW;
END IF;
END LOOP;
RETURN NEW;
END
$function$
;

0 comments on commit c5000cb

Please sign in to comment.