navapbc / template-application-flask

Apache License 2.0
7 stars 3 forks source link

Upgrade to SQLAlchemy 2.0 #197

Closed chouinar closed 11 months ago

chouinar commented 11 months ago

Ticket

https://github.com/navapbc/template-application-flask/issues/82

Changes

Upgraded to SQLAlchemy 2.0, see context section for major changes in this version

Context for reviewers

SQLAlchemy 2.0 comes with an incredibly thorough migration guide: https://docs.sqlalchemy.org/en/20/changelog/migration_20.html

Thankfully 1.4 started nudging usage of SQLAlchemy to use the new approaches, so the changes needed weren't that large.

Noteworthy changes:

Testing

Since the DB hits virtually everything, I tested everything as thoroughly as possible.

Because of how the SQLAlchemy-stubs we removed work, they need to be completely deleted otherwise MyPy thinks they should run. If you are running outside of the docker container, run poetry install --no-root --all-extras --with dev --sync which will delete extra packages.

Basics

Tests, formatting, linting, all working, only required a few fixes to make SQLAlchemy happy.

SQLAlchemy warnings

When running unit tests, SQLAlchemy will output warnings for deprecated features. The only still usable deprecated feature we had was our getqueries which were adjusted. Prior to the fix, we would see this warning: Screenshot 2023-09-20 at 11 00 38 AM

Migrations

They still work, adding a few new columns generates what we would expect and uses the mapping config added to the Base class. Screenshot 2023-09-19 at 3 36 16 PM

Screenshot 2023-09-19 at 3 36 11 PM

Swagger

Was able to successfully use the local swagger endpoints and create/update/read from the DB which was populated like so:

Screenshot 2023-09-20 at 11 34 39 AM