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

Replace lambda function to update city_id #1253

Merged
merged 7 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
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
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$
;
Loading