-
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 #1253 from cityofaustin/12744_replace_lambda_city_id
Replace lambda function to update city_id
- Loading branch information
Showing
2 changed files
with
34 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql
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,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(); |
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,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$ | ||
; |