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

Remove crashes edits layer #1572

Merged
merged 49 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
e1bf846
remove crashes edits layer metadata related stuff
roseeichelmann Oct 7, 2024
ce7b658
drop crashes edits table and crashes edits change log
roseeichelmann Oct 7, 2024
753540e
update the cris crash insert trigger function
roseeichelmann Oct 7, 2024
1bf450f
update the cris crash update trigger function
roseeichelmann Oct 7, 2024
b02db85
update this variable name to unified_record for clarity
roseeichelmann Oct 7, 2024
36b11c7
delete these migrations
roseeichelmann Oct 7, 2024
e5759fa
delete these migrations
roseeichelmann Oct 7, 2024
413c1f7
delete these migrations
roseeichelmann Oct 7, 2024
ec7195c
consolidate migrations into one file
roseeichelmann Oct 7, 2024
e5b4f54
change order of migra file
roseeichelmann Oct 7, 2024
74fafb7
didnt mean to delete this view from being tracked
roseeichelmann Oct 7, 2024
43b56e2
update up/down migras
roseeichelmann Oct 9, 2024
653078c
remove cruft
roseeichelmann Oct 9, 2024
9aed6ea
drop edits tablss
roseeichelmann Oct 14, 2024
fde2658
drop relationships
roseeichelmann Oct 14, 2024
4104db6
drop units edits layer and recreate functions & triggers
roseeichelmann Oct 14, 2024
9f198f7
drop people edits layer and recreate functions & triggers
roseeichelmann Oct 14, 2024
228071e
remove annoying crufty pg dump comments
roseeichelmann Oct 14, 2024
ae5ac9e
adding permissions and relationships
roseeichelmann Oct 21, 2024
454df50
recreating views that were dropped
roseeichelmann Oct 21, 2024
4218b50
updating these mutations to target the unified tables
roseeichelmann Oct 21, 2024
662dbdd
fix this
roseeichelmann Oct 21, 2024
bac84ef
newline
roseeichelmann Oct 21, 2024
66b1673
update to NO DATA
roseeichelmann Oct 22, 2024
e125fe3
update this to be an array relationship and use crash_pk
roseeichelmann Oct 23, 2024
b8af043
update manual geocode to check if unified value is diff from cris value
roseeichelmann Oct 23, 2024
b9446dd
update OCR mutation to update crashes table
roseeichelmann Oct 23, 2024
2e5d201
update readme to remove _edits layer
roseeichelmann Oct 23, 2024
a5b289a
new model diagram
roseeichelmann Oct 23, 2024
2a55636
remove old _edits table change logs from ignore list
roseeichelmann Oct 23, 2024
7f762ab
update this wording
roseeichelmann Oct 23, 2024
daaf15d
update diagram
roseeichelmann Oct 23, 2024
3079a08
update this wording
roseeichelmann Oct 23, 2024
d03c530
updates to wording
roseeichelmann Oct 23, 2024
0a1cc63
fix typos, make things more clear
roseeichelmann Oct 25, 2024
638774c
export new datamodel excalidraw and delete old ones
roseeichelmann Oct 25, 2024
e1bee6c
rename data_model png and delete unused/old db_overview
roseeichelmann Oct 25, 2024
9674a87
fix path to data model diagram
roseeichelmann Oct 28, 2024
81f9602
make it clear that ordering matters
roseeichelmann Oct 28, 2024
ded1fde
remove this
roseeichelmann Oct 28, 2024
87a693f
Merge pull request #1587 from cityofaustin/19367_update_docs_two_layer
roseeichelmann Oct 30, 2024
ee554f1
Merge pull request #1586 from cityofaustin/19534_update_ocr_narrative
roseeichelmann Oct 30, 2024
2963ccd
Restore aggregation permissions
roseeichelmann Oct 30, 2024
6e7b8bd
Merge branch '19366_two_layer_model_vze' of github.com:cityofaustin/v…
roseeichelmann Oct 30, 2024
9bb5ba1
Restore aggregation queries for locations view
roseeichelmann Oct 30, 2024
3d9aa7b
Merge pull request #1582 from cityofaustin/19366_two_layer_model_vze
roseeichelmann Oct 30, 2024
597ea55
Merge pull request #1578 from cityofaustin/19398_remove_units_people_…
roseeichelmann Oct 30, 2024
16c8626
Merge branch 'main' into 19185_null_overrides_spike
johnclary Oct 30, 2024
d3e4b03
fix engineering area field in fatality view export
johnclary Oct 31, 2024
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
34 changes: 16 additions & 18 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,27 +51,25 @@ The CRIS data in our database consists of four record types:

#### Design

The core challenge that the database schema solves is to preserve the integrity of staff members' edits while simultaneously allowing crash record updates to flow into the database from CRIS. The editing environment is achieved by managing multiple copies of each of the `crashes`, `units`, and `people` tables, so that CRIS edits and Vision Zero staff edits remain isolated.
The core challenge that the database schema solves is to preserve the integrity of staff members' edits while simultaneously allowing crash record updates to flow into the database from CRIS. The editing environment is achieved by managing two copies of each of the `crashes`, `units`, and `people` tables, so that CRIS edits can have their own table and thus be isolated from Vision Zero staff edits.

For example, the `crashes` records are managed in three tables:
For example, the `crashes` records are managed in two tables:

- `crashes_cris`: records that are created and updated by TxDOT CRIS through the [CRIS import ETL](../etl/cris_import/README.md)
- `crashes_edits`: record edits created by Vision Zero staff through the Vision Zero Editor web app
- `crashes`: a unified version of each record which combines the values in `crashes_cris` plus any values in `crashes_edits`
- `crashes`: a unified version of each record which combines the values in `crashes_cris` plus any edits created by Vision zero staff through the Vision Zero Editor web app. This table functions as the final source of truth, where user edits take precedence over CRIS values.

As pictured in the diagram below, the typical data flow for a crash record is as follows:

1. A new record is inserted into the `crashes_cris` table through the [CRIS import ETL](../etl/cris_import/README.md).
2. On insert into `crashes_cris`, an "empty" copy of the record is inserted into the `crashes_edits` table. The record is inserted into the `crashes_edits` table with null values in every column except the `id`, which has a foreign key constraint referencing the `crashes_cris.id` column.
3. At the same time, a complete copy of the record is inserted into the `crashes` table.
4. A Vision Zero Editor user may update crash records by updating rows in the `crashes_edits` table. When an update is received, a trigger function coalesces each value in the `crashes_edits` table with the corresponding value in the `crashes_cris` table. The resulting record—which contains the original CRIS-provided values plus any edit values made through user edits—is applied as an update to the corresponding record in the `crashes` table.
5. Similarly, when an existing `crashes_cris` record is updated through the CRIS import ETL, the updated record is coalesced against the corresponding row in the `crashes_edits` table, and the result is saved in the `crashes` table.
6. Finally, once a record is updated in the `crashes` table, additional trigger functions apply various business rules and enrich the row with spatial attributes based on its location. These trigger functions are reserved for values that require heavy computation—additional business rules can be applied through table views.
2. On insert into `crashes_cris`, a complete copy of the record is inserted into the `crashes` table.
3. A Vision Zero Editor user may update crash records by updating rows in the `crashes` table.
4. When an existing `crashes_cris` record is updated through the CRIS import ETL, a trigger function compares the old value in the `crashes_cris` table to the value in the unified `crashes` table and if they are the same, meaning there have been no user edits, the `crashes` value is also updated with the new value. If there has been a user edit, the updates to the `crashes_cris` table do not propagate to the `crashes` table.
5. Once a record is updated in the `crashes` table, additional trigger functions apply various business rules and enrich the row with spatial attributes based on its location. These trigger functions are reserved for values that require heavy computation—additional business rules can be applied through table views.

![CRIS editing model](../docs/images/cris_data_model.png)
![CRIS editing model](../docs/images/data_model.png)
_The "layered" editing environment of the Vision Zero Database_

The process for updating `units` and `people` behaves in the same manner as `crashes`. To ensure proper data flow and trigger behavior, **records should never be directly inserted into the `_edits` or unified tables**.
The process for updating `units` and `people` behaves in the same manner as `crashes`. To ensure proper data flow and trigger behavior, **records should never be directly inserted into the unified tables**.

#### CRIS Extract configuration and accounts

Expand Down Expand Up @@ -177,7 +175,7 @@ See the [Common maintenance tasks](#common-maintenance-tasks) section for more d

Charges records are provided by CRIS and describe a legal charge filed by the responding law enforcement agency. These records require special handling in our database because CRIS does not provide a unique primary key column for charges. Here's what you should know about these records:

1. Charge records only exist in the `charges_cris` table and do not have corresponding `charges_edits` or `charges` tables.
1. Charge records only exist in the `charges_cris` table and do not have a corresponding `charges` table.
2. During the CRIS import, all charge records are **deleted** from the database for any crash ID present in the CRIS extract. After deletion, the CRIS import ETL inserts all charges records present in the extract.
3. The CRIS import ETL filters out charge records where the `charge` value is `NO CHARGE`—this reduces the number of charge records in the database by many thousands.
4. Because charges are subject to deletion, they are not editable through the VZE/graphql API and should be considered read-only.
Expand All @@ -201,7 +199,7 @@ This table outlines the primary key columns in the database and how they relate

Because there can be a lag time of weeks, even months, before law enforcement investigators submit their crash report to TxDOT, the Vision Zero team needs the ability to manually create "temporary" records so that reporting metrics are more timely/accurate.

The VZE makes this possible by allowing users to insert crash, unit, and people records directly into the database. User-created records must be inserted into the `_cris` tables to ensure the proper data flow across the `_edits` and unified tables.
The VZE makes this possible by allowing users to insert crash, unit, and people records directly into the database. User-created records must be inserted into the `_cris` tables to ensure proper data flow into the unified tables, as the database expects that every single record that exists in the unified tables exists in the `_cris` tables and vice versa.

User-created records do not have a `cris_crash_id` column. Because `cris_crash_id` is central to the VZE for searching and navigating to crash pages, we use a generated column, `crashes.record_locator`, as a pseudo-crash ID. The `record_locator` column is generated as either `T<crashes.id>` (for temp records) or `<cris_crash_id>` (for CRIS records), and is rendered throughout the VZE.

Expand Down Expand Up @@ -252,8 +250,8 @@ We have a number of tables which function as geospatial layers which are referen
Follow these steps to add a new column to the database that will be sourced from CRIS. See [PR #1546](https://github.com/cityofaustin/vision-zero/pull/1546) as an example.

1. Remember that all database operations should be deployed through migrations. See the [development and deployment](#development-and-deployment) docs.
2. Add the new column to all three tables of the given record type. For example, if this is a crash-level column, add the column to the `crashes_cris`, `crashes_edits`, and `crashes` tables.
3. Modify the trigger function that inserts new rows into the `_edits` and unified table that corresponds to the record type you are modifying: either the `crashes_cris_insert_rows()`, `units_cris_insert_rows()`, ot the `people_cris_insert_rows()` function. Locate the part of the function that selects all values from the new `_cris` record and inserts into the unified table. This should be obvious, because all column names are listed in this function. Add your new column name to function accordingly.
2. Add the new column to both tables of the given record type. For example, if this is a crash-level column, add the column to the `crashes_cris` and `crashes` tables.
3. Modify the trigger function that inserts new rows into the unified table that corresponds to the record type you are modifying: either the `crashes_cris_insert_rows()`, `units_cris_insert_rows()`, or the `people_cris_insert_rows()` function. Locate the part of the function that inserts into the unified table and add your column name to end of it, then locate the part that selects all values from the new `_cris` record and do the same. **Make sure that the order of the columns in the insert and select parts of the function match up**
4. Next, you will need to add your new column to the `_column_metadata` table, so that the CRIS import ETL is aware that this column should be included in imports. For example:

```sql
Expand All @@ -278,9 +276,9 @@ Follow these steps to add a custom, non-CRIS column to `crashes`, `units`, or `p

1. Remember that all database operations should be deployed through migrations. See the [development and deployment](#development-and-deployment) docs.

2. Add your new column to the `_edits` and unified table for the given record type. For example, if this is a crash-level column, add the column to the `crashes_edits` and `crashes` tables.
2. Add your new column to the unified table for the given record type. For example, if this is a crash-level column, add the column to the `crashes` table.

3. Unlike adding a CRIS-managed column, you do not need to modify any trigger functions, because the `_edits` trigger functions (`crashes_edits_update`, `units_edits_update`, `people_edits_update`) do not rely on hard-coded column names.
3. Unlike adding a CRIS-managed column, you do not need to modify any trigger functions.

4. Next, add your new column to the `_column_metadata` table and indicate that it should _not_ be imported by CRIS.

Expand Down Expand Up @@ -318,7 +316,7 @@ Todo: see the helper script readme.

### Debugging record triggers

The various record insert and update trigger functions which manage the `_cris` and `_edits` data flows have debugging statements embedded. Debug messaging can be enabled on a per-client-session basis by executing the command `set client_min_messages to debug;` in your SQL client. Your SQL client will now log debug messages when you use it to make record inserts and updates.
The various record insert and update trigger functions which manage the `_cris` to unified table data flows have debugging statements embedded. Debug messaging can be enabled on a per-client-session basis by executing the command `set client_min_messages to debug;` in your SQL client. Your SQL client will now log debug messages when you use it to make record inserts and updates.

### Parsing change log data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: investigat_agency_id
table:
name: crashes_edits
schema: public
- name: crashess
Copy link
Member

Choose a reason for hiding this comment

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

not related to this PR, because its in production but it just jumped out to me here, why is this named crashess, its like this in all of the lookups relationships

Copy link
Member

@johnclary johnclary Oct 21, 2024

Choose a reason for hiding this comment

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

it's Hasura trying to be clever with pluralization. related—units_cri: #1495 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

thanks for flagging it—let's leave it as-is for now. fortunately, we're not using these array relationships afaik so it won't be too painful to change.

Copy link
Member

Choose a reason for hiding this comment

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

ahhh now I remember -- units_cri

crashess has grown on me, like largesse

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it makes it feel more magnificent. countess, duchess, crashess

using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: rpt_autonomous_level_engaged_id
table:
name: units_edits
schema: public
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: autonomous_unit_id
table:
name: units_edits
schema: public
7 changes: 0 additions & 7 deletions database/metadata/databases/default/tables/lookups_city.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: rpt_city_id
table:
name: crashes_edits
schema: public
- name: crashess
using:
foreign_key_constraint_on:
Expand Down
7 changes: 0 additions & 7 deletions database/metadata/databases/default/tables/lookups_cnty.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: rpt_cris_cnty_id
table:
name: crashes_edits
schema: public
- name: crashess
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: fhe_collsn_id
table:
name: crashes_edits
schema: public
- name: crashess
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,6 @@ array_relationships:
table:
name: units_cris
schema: public
- name: unitsEditsByContribFactr2Id
using:
foreign_key_constraint_on:
column: contrib_factr_2_id
table:
name: units_edits
schema: public
- name: unitsEditsByContribFactr3Id
using:
foreign_key_constraint_on:
column: contrib_factr_3_id
table:
name: units_edits
schema: public
- name: unitsEditsByContribFactrP1Id
using:
foreign_key_constraint_on:
column: contrib_factr_p1_id
table:
name: units_edits
schema: public
- name: unitsEditsByContribFactrP2Id
using:
foreign_key_constraint_on:
column: contrib_factr_p2_id
table:
name: units_edits
schema: public
- name: unitsUnifiedsByContribFactr2Id
using:
foreign_key_constraint_on:
Expand Down Expand Up @@ -100,13 +72,6 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: contrib_factr_1_id
table:
name: units_edits
schema: public
select_permissions:
- role: editor
permission:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: people_cris
schema: public
- name: people_edits
using:
foreign_key_constraint_on:
column: prsn_ethnicity_id
table:
name: people_edits
schema: public
- name: peoples
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: e_scooter_id
table:
name: units_edits
schema: public
7 changes: 0 additions & 7 deletions database/metadata/databases/default/tables/lookups_gndr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: people_cris
schema: public
- name: people_edits
using:
foreign_key_constraint_on:
column: prsn_gndr_id
table:
name: people_edits
schema: public
- name: peoples
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: first_harm_evt_inv_id
table:
name: units_edits
schema: public
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: people_cris
schema: public
- name: people_edits
using:
foreign_key_constraint_on:
column: prsn_helmet_id
table:
name: people_edits
schema: public
- name: peoples
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: people_cris
schema: public
- name: people_edits
using:
foreign_key_constraint_on:
column: prsn_injry_sev_id
table:
name: people_edits
schema: public
- name: peoples
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: intrsct_relat_id
table:
name: crashes_edits
schema: public
- name: crashess
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: light_cond_id
table:
name: crashes_edits
schema: public
- name: crashess
using:
foreign_key_constraint_on:
Expand Down
7 changes: 0 additions & 7 deletions database/metadata/databases/default/tables/lookups_movt.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: units
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: movement_id
table:
name: units_edits
schema: public
select_permissions:
- role: editor
permission:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: crashes_cris
schema: public
- name: crashes_edits
using:
foreign_key_constraint_on:
column: obj_struck_id
table:
name: crashes_edits
schema: public
- name: crashess
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,6 @@ array_relationships:
table:
name: people_cris
schema: public
- name: people_edits
using:
foreign_key_constraint_on:
column: prsn_occpnt_pos_id
table:
name: people_edits
schema: public
- name: peoples
using:
foreign_key_constraint_on:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: pbcat_pedalcyclist_id
table:
name: units_edits
schema: public
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: pbcat_pedestrian_id
table:
name: units_edits
schema: public
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,3 @@ array_relationships:
table:
name: units_cris
schema: public
- name: units_edits
using:
foreign_key_constraint_on:
column: pedalcyclist_action_id
table:
name: units_edits
schema: public
Loading
Loading