🗃️ Use of dedicated db sessions
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
from logging import getLogger
|
||||
from typing import Annotated, AsyncIterator
|
||||
|
||||
from fastapi import Depends
|
||||
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
async_sessionmaker,
|
||||
AsyncEngine,
|
||||
@@ -10,42 +15,47 @@ from sqlalchemy.ext.asyncio import (
|
||||
from .base_class import Base
|
||||
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self) -> None:
|
||||
self._engine: AsyncEngine | None = None
|
||||
self._session_maker: async_sessionmaker[AsyncSession] | None = None
|
||||
self._session: AsyncSession | None = None
|
||||
self._async_engine: AsyncEngine | None = None
|
||||
self._async_session_local: async_sessionmaker[AsyncSession] | None = None
|
||||
|
||||
@property
|
||||
def session(self) -> AsyncSession | None:
|
||||
if self._session is None and (session_maker := self._session_maker) is not None:
|
||||
self._session = session_maker()
|
||||
return self._session
|
||||
async def get_session(self) -> AsyncSession | None:
|
||||
try:
|
||||
return self._async_session_local() # type: ignore
|
||||
|
||||
except (SQLAlchemyError, AttributeError) as e:
|
||||
logger.exception(e)
|
||||
|
||||
return None
|
||||
|
||||
# TODO: Preserve UserLastStopSearchResults table from drop.
|
||||
async def connect(self, db_path: str, clear_static_data: bool = False) -> bool:
|
||||
self._async_engine = create_async_engine(
|
||||
db_path, pool_pre_ping=True, pool_size=10, max_overflow=20
|
||||
)
|
||||
|
||||
# TODO: Preserve UserLastStopSearchResults table from drop.
|
||||
self._engine = create_async_engine(db_path)
|
||||
if self._engine is not None:
|
||||
SQLAlchemyInstrumentor().instrument(engine=self._engine.sync_engine)
|
||||
if self._async_engine is not None:
|
||||
SQLAlchemyInstrumentor().instrument(engine=self._async_engine.sync_engine)
|
||||
|
||||
self._session_maker = async_sessionmaker(
|
||||
self._engine, expire_on_commit=False, class_=AsyncSession
|
||||
self._async_session_local = async_sessionmaker(
|
||||
bind=self._async_engine,
|
||||
# autoflush=False,
|
||||
expire_on_commit=False,
|
||||
class_=AsyncSession,
|
||||
)
|
||||
if (session := self.session) is not None:
|
||||
await session.execute(text("CREATE EXTENSION IF NOT EXISTS pg_trgm;"))
|
||||
|
||||
async with self._engine.begin() as conn:
|
||||
async with self._async_engine.begin() as session:
|
||||
await session.execute(text("CREATE EXTENSION IF NOT EXISTS pg_trgm;"))
|
||||
if clear_static_data:
|
||||
await conn.run_sync(Base.metadata.drop_all)
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await session.run_sync(Base.metadata.drop_all)
|
||||
await session.run_sync(Base.metadata.create_all)
|
||||
|
||||
return True
|
||||
|
||||
async def disconnect(self) -> None:
|
||||
if self._session is not None:
|
||||
await self._session.close()
|
||||
self._session = None
|
||||
|
||||
if self._engine is not None:
|
||||
await self._engine.dispose()
|
||||
if self._async_engine is not None:
|
||||
await self._async_engine.dispose()
|
||||
|
Reference in New Issue
Block a user