-
Notifications
You must be signed in to change notification settings - Fork 5
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
Replace encode/databases with psycopg & pydantic model validation #128
Conversation
5006a6a
to
b5a4b77
Compare
@spwoodcock @Pradip-p I have started continuing this refactor process beginning with Drone module. |
…ry placeholders, result fetching, and exception handling. Adjusted token expiration handling and password hashing. Updated user and profile operations to align with psycopg syntax.
for more information, see https://pre-commit.ci
Looks like this introduced a lot of errors that weren't picked up on initial testing? What sort of errors are you experiencing @Pradip-p? This is quite a large refactor! 😅 |
@spwoodcock I've fixed most of the errors that came up. I did catch a lot of the issues during initial testing, but since then, we've updated some logic and made changes to the main branch, which introduced new challenges. 😅 |
Oh ok, that makes sense! We can merge this as soon as it's ready to prevent other conflicts 👍 |
@nrjadkry @spwoodcock TODO:
|
@spwoodcock @Pradip-p |
4 less keystrokes 😆 |
Full details here: hotosm/fmtm#254
psycopg vs asyncpg driver
While
asyncpg
may be more performant and good for many use cases, I actually think thatpsycopg
has an edge for our application:libpq
official Postgres driver, which in theory should have better longevity (asyncpg uses a custom implementation that would need to be maintained...).Static Type Checking / Pydantic Integration
https://github.com/pydantic/pydantic/issues/9406#issuecomment-2104224328
row_factory
on the database call: https://www.psycopg.org/psycopg3/docs/advanced/typing.html#example-returning-records-as-pydantic-modelsOther options
encode/databases
is a great wrapper aroundasyncpg
, but I do worry about it's support. It hasn't been updated to supportpsycopg
in a long time. The syntax may be slightly nicer, but it doesn't do Pydantic model validation & provides little benefit when used with raw SQL (the bundled SQLAlchemy Core syntax may be helpful though).SQLModel
is also a good option, allowing for Pydantic model validation built-in. This has a much larger community & will likely be supported for some time. Three concerns though:Example Logic
TODO
Init db / main logic
Project routes
Task routes
User routes
Drone routes
I have already updated all the logic getting the db connection and user in each endpoint.
We just need to update the remaining route logic, changing the usage of db.execute slightly from
:project_id
placeholders to%(project_id)s
and ensure the raw SQL logic works as intended.Also update the usage of
project_crud.get_project_by_id
to use theproject_deps.get_project_by_id
dependency in the endpoints (then delete project_crud.get_project_by_id).