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

Syncronize our SQLAlchemy type hints with db schema #2197

Merged
merged 9 commits into from
Dec 9, 2024

Conversation

jonathangreen
Copy link
Member

Description

Make sure what what is defined in our SQLAlchemy schema is accuratly reflected in the type hints:

  • If a relationship is not a collection, and its foreign key column is nullable make sure the annotation is Optional
    • There were some cases where this caused a large number of mypy failures, since we were widely assuming that these relationships were not optional. In these cases I queried all our production instances, and if the the foreign key was never null, I added a nullable=False to the foreign key column.
  • Appropriately set the columns annotation to not Optional if:
    • nullable=False
    • primary_key=True
  • Add nullable=False and appropriately set annotation for columns that have default set

Motivation and Context

Our sqlalchemy type hints have been added over time, and some of them did not accurately reflect what was in the DB schema. I noticed this while working on #2194.

These changes meant that I had to add some guard code for errors that mypy rightly flagged. It also meant that I could remove some guard code that was no longer necessary.

How Has This Been Tested?

  • Running unit tests
  • Querying our production DBs to make sure our production data doesn't have any nulls set

Checklist

  • I have updated the documentation accordingly.
  • All new and existing tests passed.

@jonathangreen jonathangreen added the DB migration This PR contains a DB migration label Nov 27, 2024
Copy link

codecov bot commented Nov 27, 2024

Codecov Report

Attention: Patch coverage is 94.73684% with 14 lines in your changes missing coverage. Please review.

Project coverage is 91.08%. Comparing base (7f9581a) to head (905a0b6).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
src/palace/manager/api/odl/api.py 64.28% 3 Missing and 2 partials ⚠️
src/palace/manager/util/notifications.py 0.00% 2 Missing and 1 partial ⚠️
src/palace/manager/api/opds_for_distributors.py 0.00% 1 Missing and 1 partial ⚠️
src/palace/manager/feed/acquisition.py 75.00% 1 Missing and 1 partial ⚠️
src/palace/manager/marc/annotator.py 75.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2197      +/-   ##
==========================================
- Coverage   91.09%   91.08%   -0.01%     
==========================================
  Files         363      363              
  Lines       41226    41200      -26     
  Branches     8835     8827       -8     
==========================================
- Hits        37555    37529      -26     
- Misses       2406     2407       +1     
+ Partials     1265     1264       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jonathangreen jonathangreen requested a review from a team November 27, 2024 20:09
@jonathangreen
Copy link
Member Author

jonathangreen commented Dec 4, 2024

This is okay to get a code review but I need to fix up the migrations parent before it gets merged due to the migration in #2198.

Copy link
Contributor

@tdilauro tdilauro left a comment

Choose a reason for hiding this comment

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

This looks good! 🗺️

There are some cases we have not added an annotation. Was that intentional? What is the rationale for whether we are adding one or not?

@jonathangreen
Copy link
Member Author

There are some cases we have not added an annotation. Was that intentional? What is the rationale for whether we are adding one or not?

@tdilauro every relationship mapped value should have a type hint after this change. If I missed any it wasn't intentional.

Only Column mapped values that vary from the default introspection provided by the sqlalchemy mypy plugin (see docs) were annotated. In practice, this means I only annotated Column values that had nullable=False or primary_key=True set on them.

@jonathangreen jonathangreen force-pushed the chore/update-sqlalchemy-types branch from db89e62 to 905a0b6 Compare December 9, 2024 14:43
@jonathangreen jonathangreen merged commit b4d189b into main Dec 9, 2024
21 checks passed
@jonathangreen jonathangreen deleted the chore/update-sqlalchemy-types branch December 9, 2024 16:56
@jonathangreen jonathangreen mentioned this pull request Dec 10, 2024
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
DB migration This PR contains a DB migration
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants