28 lines
921 B
Python
28 lines
921 B
Python
from sqlalchemy import event
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker
|
|
from sqlalchemy.orm import DeclarativeBase
|
|
from app.config import settings
|
|
|
|
engine = create_async_engine(settings.database_url, echo=False)
|
|
SessionLocal = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
|
|
# Enable WAL mode so concurrent reads don't block on an open write transaction.
|
|
# Also set a generous busy timeout so transient lock contention retries instead
|
|
# of immediately raising OperationalError.
|
|
@event.listens_for(engine.sync_engine, "connect")
|
|
def _set_sqlite_pragmas(dbapi_conn, _record):
|
|
cursor = dbapi_conn.cursor()
|
|
cursor.execute("PRAGMA journal_mode=WAL")
|
|
cursor.execute("PRAGMA busy_timeout=10000") # 10 s
|
|
cursor.close()
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
async def get_db() -> AsyncSession:
|
|
async with SessionLocal() as session:
|
|
yield session
|