1f2aa54e43
Co-authored-by: christianlouis <361235+christianlouis@users.noreply.github.com> Agent-Logs-Url: https://github.com/christianlouis/pop_puller_to_gmail/sessions/e0178422-af66-4a2c-9cfe-13d750965c1a
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
"""
|
|
Database configuration and session management.
|
|
"""
|
|
|
|
from collections.abc import AsyncGenerator
|
|
|
|
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
|
|
from sqlalchemy.orm import declarative_base
|
|
from app.core.config import settings
|
|
|
|
# Create async engine
|
|
# prepared_statement_cache_size=0 disables SQLAlchemy's asyncpg prepared-statement
|
|
# LRU cache. This prevents ProgrammingError("cached statement plan is invalid") when
|
|
# CREATE TYPE / CREATE TABLE DDL (run via create_all at startup) invalidates previously
|
|
# cached statement plans on the same connection.
|
|
engine = create_async_engine(
|
|
settings.DATABASE_URL,
|
|
echo=settings.DEBUG,
|
|
pool_size=settings.DATABASE_POOL_SIZE,
|
|
max_overflow=settings.DATABASE_MAX_OVERFLOW,
|
|
pool_pre_ping=True,
|
|
connect_args={"prepared_statement_cache_size": 0},
|
|
)
|
|
|
|
# Create async session factory
|
|
async_session_maker = async_sessionmaker(
|
|
engine,
|
|
class_=AsyncSession,
|
|
expire_on_commit=False,
|
|
autocommit=False,
|
|
autoflush=False,
|
|
)
|
|
|
|
# Base class for models
|
|
Base = declarative_base()
|
|
|
|
|
|
async def get_db() -> AsyncGenerator[AsyncSession, None]:
|
|
"""Dependency for getting async database session"""
|
|
async with async_session_maker() as session:
|
|
try:
|
|
yield session
|
|
await session.commit()
|
|
except Exception:
|
|
await session.rollback()
|
|
raise
|
|
finally:
|
|
await session.close()
|