-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7bdaada
create trigger
roseeichelmann e2a02f5
create trigger function
roseeichelmann f686fc4
newline
roseeichelmann a0169fa
update wording
roseeichelmann 0d3ec70
missing semicolons
roseeichelmann e35b8cf
fix parenthesis
roseeichelmann 512c42f
dont need to join to crashes table for select
roseeichelmann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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 | ||
) | ||
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$ | ||
; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 fromatd_jurisdictions
to get the info you need.There was a problem hiding this comment.
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