Skip to content

Commit

Permalink
Improve db setup
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSuperiorStanislav committed Jul 31, 2024
1 parent f85b476 commit f767b75
Show file tree
Hide file tree
Showing 3 changed files with 295 additions and 289 deletions.
36 changes: 16 additions & 20 deletions example_app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

import saritasa_sqlalchemy_tools

db_session_manager = saritasa_sqlalchemy_tools.get_async_db_session(
drivername="postgresql+asyncpg",
username="fastapi-rest-framework-user",
password="manager", # noqa: S106
host="postgres",
port=5432,
database="fastapi-rest-framework-dev",
autocommit=False,
autoflush=False,
expire_on_commit=False,
on_connect=[],
query={},
)


async def get_db_session() -> (
collections.abc.AsyncIterator[saritasa_sqlalchemy_tools.Session]
):
"""Set up and get db session."""
async with saritasa_sqlalchemy_tools.get_async_session_factory(
drivername="postgresql+asyncpg",
username="fastapi-rest-framework-user",
password="manager", # noqa: S106
host="postgres",
port=5432,
database="fastapi-rest-framework-user-dev",
autocommit=False,
autoflush=False,
expire_on_commit=False,
on_connect=[],
query={},
)() as session:
try:
yield session
except Exception:
await session.rollback()
raise
else:
await session.commit()
async with db_session_manager as session:
yield session
Loading

0 comments on commit f767b75

Please sign in to comment.