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 6 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();
29 changes: 29 additions & 0 deletions atd-vzd/triggers/update_crash_city_id.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- 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 jurisdictions.id
FROM atd_txdot_crashes AS crashes
INNER JOIN atd_jurisdictions jurisdictions
ON (jurisdictions.geometry && NEW.position) AND ST_Contains(jurisdictions.geometry, NEW.position)
WHERE crashes.crash_id = NEW.crash_id
Copy link
Member

@johnclary johnclary Jul 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👋 just eyeballing this sql, @roseeichelmann can you help me understand why it is necessary to join to atd_txdot_crashes? it looks to me like you only need to select from atd_jurisdictions to get the info you need.

Copy link
Contributor Author

@roseeichelmann roseeichelmann Jul 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no you are totally right, its not necessary! brain fart :p

just pushed up changes and recreated that function in staging

)
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$
;