Migrate database setup from sync to async SQLAlchemy#108
Open
szyszkapiotr wants to merge 8 commits into
Open
Conversation
Misc adjustments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related issue: #97
Description
This PR intruduces change from existing synchronous SQLAlchemy setup to its async (
asyncpg) version and extends the generic CRUD/service layer with bulk and lookup helpers. Additionally, acommit: boolflag has been introduced to repository methods (default: True). This allows callers that compose multiple repository operations to passcommit=Falseand let the service layer own the transaction, committing only once at the end.Changes
Async migration
config.py.jinja: adddb_async_uriproperty (postgresql+asyncpg://).database.py: replace synccreate_engine/sessionmaker/Sessionwithcreate_async_engine/async_sessionmaker/AsyncSession. ExportAsyncDbSessiondependency,adb_session_ctx, andAsyncSessionLocal(
expire_on_commit=False). Dropengine,SessionLocal,DbSession.repositories.py,services/services.py: convert all CRUD/service methods toasync/await; queries moved fromsession.query(...)toselect()/delete().user/repositories/activity_repository.py,user/services/activity_mixin.py:migrate
is_user_activeto asyncselect().utils/healthcheck.py: asyncdb.execute, pool status viaasync_engine.utils/exceptions.py:handle_exceptionsnow branches onasyncio.iscoroutinefunctionso exception-to-HTTP translation works for asyncmethods (previously the sync wrapper returned the coroutine unawaited and never
caught anything).
New repository/service methods
create_many— bulk insert; flushes whencommit=False.get_from_ids— fetch a set of rows by id list.get_filtered_scalar— single-or-none lookup, raisesMultipleResultsFoundErrorwhen more than one row matches.delete_many— batched bulk delete; deletes per batch then commits once(atomic across batches).
commitflag added tocreate/update/deletefor unit-of-work composition.