From 7bdaada2d9839815f80569f4412ce976f9239345 Mon Sep 17 00:00:00 2001 From: rose Date: Fri, 14 Jul 2023 15:26:39 -0500 Subject: [PATCH 1/7] create trigger --- .../migration_atd_txdot_crashes_2023_07_14--1325.sql | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql diff --git a/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql b/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql new file mode 100644 index 000000000..bef9a3621 --- /dev/null +++ b/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql @@ -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 valid 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(); From e2a02f598c9357a95ca136cbc5f12fc8f9249a5c Mon Sep 17 00:00:00 2001 From: rose Date: Fri, 14 Jul 2023 15:26:48 -0500 Subject: [PATCH 2/7] create trigger function --- atd-vzd/triggers/update_crash_city_id.sql | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 atd-vzd/triggers/update_crash_city_id.sql diff --git a/atd-vzd/triggers/update_crash_city_id.sql b/atd-vzd/triggers/update_crash_city_id.sql new file mode 100644 index 000000000..bcf9914be --- /dev/null +++ b/atd-vzd/triggers/update_crash_city_id.sql @@ -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$ +; \ No newline at end of file From f686fc448bbc052abb5af896ea0303f511b40e78 Mon Sep 17 00:00:00 2001 From: rose Date: Mon, 17 Jul 2023 11:04:11 -0500 Subject: [PATCH 3/7] newline --- atd-vzd/triggers/update_crash_city_id.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atd-vzd/triggers/update_crash_city_id.sql b/atd-vzd/triggers/update_crash_city_id.sql index bcf9914be..393afd8c6 100644 --- a/atd-vzd/triggers/update_crash_city_id.sql +++ b/atd-vzd/triggers/update_crash_city_id.sql @@ -26,4 +26,4 @@ temprow RECORD; RETURN NEW END $function$ -; \ No newline at end of file +; From a0169fa204bb87ae1b3ff8fe787973aca42f206d Mon Sep 17 00:00:00 2001 From: rose Date: Mon, 17 Jul 2023 11:04:18 -0500 Subject: [PATCH 4/7] update wording --- .../migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql b/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql index bef9a3621..12ac5b802 100644 --- a/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql +++ b/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql @@ -1,5 +1,5 @@ -- 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 valid Austin jurisdictions +-- 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 From 0d3ec70dcb73ca0c8a19421ae53f8652dd776ad3 Mon Sep 17 00:00:00 2001 From: rose Date: Mon, 17 Jul 2023 11:11:25 -0500 Subject: [PATCH 5/7] missing semicolons --- .../migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql b/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql index 12ac5b802..75768df00 100644 --- a/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql +++ b/atd-vzd/migrations/migration_atd_txdot_crashes_2023_07_14--1325.sql @@ -3,5 +3,5 @@ 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) + WHEN (OLD.position IS DISTINCT FROM NEW.position AND NEW.city_id != 22) EXECUTE FUNCTION update_crash_city_id(); From e35b8cf5d20b4617bb0c9c1d0f1ac5c6984212a5 Mon Sep 17 00:00:00 2001 From: rose Date: Mon, 17 Jul 2023 11:11:49 -0500 Subject: [PATCH 6/7] fix parenthesis --- atd-vzd/triggers/update_crash_city_id.sql | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/atd-vzd/triggers/update_crash_city_id.sql b/atd-vzd/triggers/update_crash_city_id.sql index 393afd8c6..e2134e21f 100644 --- a/atd-vzd/triggers/update_crash_city_id.sql +++ b/atd-vzd/triggers/update_crash_city_id.sql @@ -19,11 +19,11 @@ temprow RECORD; ) LOOP IF temprow.id IN (5, 3, 7, 8, 10) THEN - NEW.city_id = 22 - RETURN NEW + NEW.city_id = 22; + RETURN NEW; END IF; END LOOP; - RETURN NEW + RETURN NEW; END $function$ ; From 512c42f8d38afdd4107570bddc56459e7e285440 Mon Sep 17 00:00:00 2001 From: rose Date: Wed, 26 Jul 2023 10:55:55 -0500 Subject: [PATCH 7/7] dont need to join to crashes table for select --- atd-vzd/triggers/update_crash_city_id.sql | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/atd-vzd/triggers/update_crash_city_id.sql b/atd-vzd/triggers/update_crash_city_id.sql index e2134e21f..4827a6998 100644 --- a/atd-vzd/triggers/update_crash_city_id.sql +++ b/atd-vzd/triggers/update_crash_city_id.sql @@ -11,11 +11,9 @@ temprow RECORD; -- 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 + 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