Support async DB driver: replace psycopg with asyncpg and adopt async Alembic template#2270
Open
MohamedMostafa259 wants to merge 4 commits intofastapi:masterfrom
Open
Support async DB driver: replace psycopg with asyncpg and adopt async Alembic template#2270MohamedMostafa259 wants to merge 4 commits intofastapi:masterfrom
MohamedMostafa259 wants to merge 4 commits intofastapi:masterfrom
Conversation
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.
Summary
This PR comprehensively migrates the database layer from the synchronous
psycopgdriver to the asynchronousasyncpgdriver, and updates Alembic to use the async template. All database operations, CRUD functions, and route handlers now use async/await patterns. These changes align the entire stack (FastAPI + SQLAlchemy + AsyncPG + Alembic) for seamless async operations and better runtime performance.Changes
Dependencies
psycopg[binary]<4.0.0,>=3.1.13asyncpg>=0.31.0Configuration
SQLALCHEMY_DATABASE_URIscheme frompostgresql+psycopgtopostgresql+asyncpginbackend/app/core/config.py.Database Connection (
backend/app/core/db.py)create_engine()withcreate_async_engine()for true async database connections.SessionwithAsyncSessionandasync_sessionmaker.init_db()async to support initialization workflows.CRUD Operations (
backend/app/crud.py)create_user(),update_user(),get_user_by_email(),authenticate(),create_item()await session.execute()instead ofsession.exec().await session.commit()andawait session.refresh().Dependencies (
backend/app/api/deps.py)get_db()to async generator returningAsyncSession.get_current_user()and related dependency functions to async withawaitfor database calls.Route Handlers
All routes in
backend/app/api/routes/updated to async:login.py:login_access_token(),recover_password(),reset_password(),recover_password_html_content()users.py:read_users(),create_user(),update_user_me(),update_password_me(),delete_user_me(),register_user(),read_user_by_id(),update_user(),delete_user()items.py:read_items(),read_item(),create_item(),update_item(),delete_item()All handlers now use
async defandawaitfor session and CRUD operations.Alembic Migration Environment
backend/app/alembic/usingalembic init -t async alembic, which includes async-awareenv.pyusingasync_engine_from_config()and async connection handling.