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

Remove crashes edits layer #1572

merged 49 commits into from
Oct 31, 2024

Conversation

roseeichelmann
Copy link
Contributor

@roseeichelmann roseeichelmann commented Oct 7, 2024

Associated issues

Closes cityofaustin/atd-data-tech#19185
PR #1551 revealed that there is need for our data model to enable null user overrides to CRIS values. This PR is a proof of concept on the crashes tables that implements the 2 layer data model structure i proposed awhile back in the data model prototyping stage. There are only two tables, the crashes_cris table which only contains values straight from cris, and the crashes table which acts as the unified table that contains both cris values and user edits if there was a user override. This is the table that users will be updating. We are basically just nixing the whole _edits layer.

Testing

URL to test:
Local

Steps to test:

  1. Check out this branch, get a fresh replica of prod data. Apply hasura migrations and metadata.
  2. Lets test a user edit to the crashes table:
select rpt_street_name from crashes where id = 1;
select rpt_street_name from crashes_cris where id = 1;
update crashes set rpt_street_name = 'VZ EDIT' where id = 1;
select rpt_street_name from crashes where id = 1;
  1. Lets test a cris update to that same record, the unified record should show the VZ edit:
update crashes_cris set rpt_street_name = 'CRIS UPDATE' where id = 1;
select rpt_street_name from crashes where id = 1;
select rpt_street_name from crashes_cris where id = 1;
  1. Lets test a cris update to a non edited value, the unified record should show the new cris value:
select rpt_street_name from crashes where id = 2;
select rpt_street_name from crashes_cris where id = 2;
update crashes_cris set rpt_street_name = 'CRIS UPDATE' where id = 2;
select rpt_street_name from crashes where id = 2;
select rpt_street_name from crashes_cris where id = 1;
  1. Now lets test a user null override. The unified record should show null instead of the cris value
select rpt_street_name from crashes where id = 10;
select rpt_street_name from crashes_cris where id = 10;
update crashes set rpt_street_name = null where id = 10;
select rpt_street_name from crashes where id = 10;
select rpt_street_name from crashes_cris where id = 10;
  1. Next we can test the cris import script. You can download some extract zips from the AWS S3 bucket and put them in /vision-zero/etl/cris_import/extracts then follow the test steps in cris_import/README.md to run the cris import docker image and run the following command to process those extract zips:
./cris_import.py --csv
  1. You can also inspect the change_log_crashes and change_log_crashes_cris and make sure you are seeing your changes there as expected
  2. Test the down migrations

Ship list

  • Check migrations for any conflicts with latest migrations in main branch
  • Confirm Hasura role permissions for necessary access
  • Code reviewed
  • Product manager approved

@roseeichelmann roseeichelmann added the WIP Work in progress label Oct 7, 2024
@@ -17,15 +17,6 @@ object_relationships:
- name: crashes_cri
using:
foreign_key_constraint_on: id
- name: crashes_list_view
Copy link
Contributor Author

@roseeichelmann roseeichelmann Oct 7, 2024

Choose a reason for hiding this comment

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

in the next PR where changes to the VZE are addressed i will need to recreate this view without references to the edits table, same for the locations view

@@ -325,3 +316,138 @@ select_permissions:
- position
filter: {}
comment: ""
update_permissions:
- role: editor
Copy link
Contributor Author

Choose a reason for hiding this comment

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

crashes table now has update permissions

new.wthr_cond_id
);
-- insert new (editable) vz record (only record ID)
INSERT INTO public.crashes_edits (id) values (new.id);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the part of the function i deleted in the up migration

-- if the new cris value doesn't match the old cris value
if(new_cris_jb -> column_name <> old_cris_jb -> column_name) then
-- see if the unified record has the same value as the old cris value
if (unified_record_jb -> column_name = old_cris_jb -> column_name) then
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this line is updated. now we are checking in the unified record value is equal to the old cris value, if so then there hasnt been a user edit so we want update the unified record with the new cris value.

Copy link
Contributor

Choose a reason for hiding this comment

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

cool! this makes sense to me, and it is pretty nice to take advantage of the availability of the new and old values of the CRIS record and compare to the current crashes record to determine what is or is not an edit. 😎

It's been a while since I've looked at this code so it was helpful for me to see the change isolated to the crashes table too. 💯

-- if the new value doesn't match the old
if(new_cris_jb -> column_name <> old_cris_jb -> column_name) then
-- see if the vz record has a value for this field
if (edit_record_jb ->> column_name is null) then
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the part we changed in the up migration

Copy link
Member

@johnclary johnclary left a comment

Choose a reason for hiding this comment

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

wowww—it works so well! code looks great; test steps look great. my only real feedback is related to the down migrations, which are missing some changes that were applied since we launched v2.0.

one other note wrt to testing, i added to your last test like so:

-- original test
select rpt_street_name from crashes where id = 10;
select rpt_street_name from crashes_cris where id = 10;
update crashes set rpt_street_name = null where id = 10;
select rpt_street_name from crashes where id = 10;
select rpt_street_name from crashes_cris where id = 10;
-- extra test - reset the unified value to match the _cris schema
update crashes set rpt_street_name = 'BELL' where id = 10;
-- edit the cris value
update crashes_cris set rpt_street_name = 'CRIS EDIT' where id = 10;
-- observe that the unified value is now back in sync with the cris value
select rpt_street_name from crashes where id = 10;

amazing stuff, Rose—thank you!

i'll hold off on approving this since it's now the collector branch for this feature 🚀

Copy link
Member

Choose a reason for hiding this comment

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

looks like down is missing investigator_narrative_ocr_processed_at column, and also change log insert trigger is missing. it might be good to use pg_dump with the --schema-only flag to make sure we captured everything that needs to be restored. but i might be overthinking it.

Copy link
Member

@johnclary johnclary Oct 8, 2024

Choose a reason for hiding this comment

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

ah, yeah—also the on delete constraints on these foreign key references have changed to restrict. so i think the best move is to run a fresh schema dump and copy it into this migration.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks for catching this -- just updated with new and improved down migra 🚀


drop trigger if exists insert_new_crashes_cris on public.crashes_cris;

drop function if exists public.crashes_cris_insert_rows;
Copy link
Member

Choose a reason for hiding this comment

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

nbd, but i don't think you need drop the trigger of function since you're using if exists later on (ditto for the up migration)

Copy link
Contributor Author

@roseeichelmann roseeichelmann Oct 9, 2024

Choose a reason for hiding this comment

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

i was able to delete this drop trigger part by adding cascade to the end of the drop function commands :)


SET default_tablespace = '';

SET default_table_access_method = heap;
Copy link
Member

@johnclary johnclary Oct 9, 2024

Choose a reason for hiding this comment

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

looks good! you can remove this cruft ☝️and also the statements down below that set ownership to visionzero. i think that's more of a best practice than anything.

Copy link
Contributor

@mddilley mddilley left a comment

Choose a reason for hiding this comment

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

Awesome! I walked through your test steps, and I saw the expected results from the queries. I was able to trace the changes through the change log entries really easily as well. And, the import ran without error.

Really cool to see the power and flexibility of the json iterating triggers! 🚀 The future of nullability is looking bright!

-- if the new cris value doesn't match the old cris value
if(new_cris_jb -> column_name <> old_cris_jb -> column_name) then
-- see if the unified record has the same value as the old cris value
if (unified_record_jb -> column_name = old_cris_jb -> column_name) then
Copy link
Contributor

Choose a reason for hiding this comment

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

cool! this makes sense to me, and it is pretty nice to take advantage of the availability of the new and old values of the CRIS record and compare to the current crashes record to determine what is or is not an edit. 😎

It's been a while since I've looked at this code so it was helpful for me to see the change isolated to the crashes table too. 💯

@mddilley
Copy link
Contributor

@johnclary @roseeichelmann I'm reviewing this now, but I wanted to make sure y'all knew that I needed to merge main branch into this one to get the local migrations working for testing.

Going through the front end right now, and it is looking good. 😎

@mddilley
Copy link
Contributor

mddilley commented Oct 30, 2024

@roseeichelmann still reviewing but I caught one place where engineering_area needs to be updated to engineering_area_id. The fatalities view export is not working.

Copy link
Contributor

@mddilley mddilley left a comment

Choose a reason for hiding this comment

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

I tested all of the UI with the admin role, and I only found one tiny change in the fatalities export config that I noted above. All else looks great and good to go! Happy to see all this work come together! 🙌 🎉

@johnclary
Copy link
Member

thanks, Mike! wanted to mention that i am also starting a review of this based on a full replica of prod that includes the change log tables 🧑‍🔬

Copy link
Member

@johnclary johnclary left a comment

Choose a reason for hiding this comment

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

Aside from the issue Mike flagged, everything looks great to me. I think it would be good to merge main before we merge back, just for good measure.

🚢

@johnclary
Copy link
Member

I patched this, removed Mike's review request, and i'm merging it now

@johnclary johnclary merged commit 9498ef0 into main Oct 31, 2024
4 of 5 checks passed
@johnclary johnclary deleted the 19185_null_overrides_spike branch October 31, 2024 17:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Spike] Explore data model refactor to enable null overrides
4 participants