generated from MinBZK/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
91 make tad database connection async (#306)
- Loading branch information
Showing
38 changed files
with
723 additions
and
430 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,40 @@ | ||
import logging | ||
|
||
from sqlalchemy import create_engine, select | ||
from sqlalchemy.engine import Engine | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy import select | ||
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, create_async_engine | ||
|
||
from amt.core.config import get_settings | ||
from amt.models.base import Base | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def get_engine() -> Engine: | ||
def get_engine() -> AsyncEngine: | ||
settings = get_settings() | ||
connect_args = {"check_same_thread": False} if settings.APP_DATABASE_SCHEME == "sqlite" else {} | ||
|
||
return create_engine( | ||
return create_async_engine( | ||
settings.SQLALCHEMY_DATABASE_URI, # pyright: ignore [reportArgumentType] | ||
connect_args=connect_args, | ||
echo=settings.SQLALCHEMY_ECHO, | ||
) | ||
|
||
|
||
def check_db() -> None: | ||
async def check_db() -> None: | ||
logger.info("Checking database connection") | ||
with Session(get_engine()) as session: | ||
session.execute(select(1)) | ||
async with AsyncSession(get_engine()) as session: | ||
await session.execute(select(1)) | ||
|
||
logger.info("Finish Checking database connection") | ||
|
||
|
||
def init_db() -> None: | ||
async def init_db() -> None: | ||
logger.info("Initializing database") | ||
|
||
if get_settings().AUTO_CREATE_SCHEMA: # pragma: no cover | ||
logger.info("Creating database schema") | ||
Base.metadata.create_all(get_engine()) | ||
engine = get_engine() | ||
async with engine.begin() as connection: | ||
await connection.run_sync(Base.metadata.create_all) | ||
|
||
logger.info("Finished initializing database") |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
from collections.abc import Generator | ||
from collections.abc import AsyncGenerator | ||
|
||
from sqlalchemy.orm import Session | ||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker | ||
|
||
from amt.core.db import get_engine | ||
|
||
|
||
def get_session() -> Generator[Session, None, None]: | ||
with Session(get_engine()) as session: | ||
yield session | ||
async def get_session() -> AsyncGenerator[AsyncSession, None]: | ||
async_session_factory = async_sessionmaker( | ||
get_engine(), | ||
expire_on_commit=False, | ||
class_=AsyncSession, | ||
) | ||
async with async_session_factory() as async_session: | ||
yield async_session |
Oops, something went wrong.