Spaces:
Running
Running
File size: 2,049 Bytes
c84fdae 6286ee1 c84fdae | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | """Database connection and session management."""
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession, async_sessionmaker
from sqlalchemy.pool import NullPool
from src.config import get_settings
from loguru import logger
settings = get_settings()
# Build async URL — handle both postgresql:// and postgresql+asyncpg://
_db_url = settings.database_url
if _db_url.startswith("postgresql://"):
_db_url = _db_url.replace("postgresql://", "postgresql+asyncpg://", 1)
# Neon (and most managed Postgres) requires SSL.
# server_settings sets statement_timeout at connection level (Supabase free tier defaults to ~8s,
# which causes ALTER TABLE / CREATE TABLE to randomly fail on startup migrations).
_connect_args = {
"server_settings": {"statement_timeout": "60000"}, # 60s per statement
}
if "neon.tech" in _db_url or "sslmode=require" in _db_url or "supabase.com" in _db_url:
_connect_args["ssl"] = "require"
_db_url = _db_url.split("?")[0] # asyncpg doesn't accept ?sslmode= in the URL
# Create async engine
engine = create_async_engine(
_db_url,
echo=settings.database_echo,
poolclass=NullPool,
connect_args=_connect_args,
)
# Create session factory
AsyncSessionLocal = async_sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
async def get_db():
"""Dependency for FastAPI to get DB session."""
async with AsyncSessionLocal() as session:
try:
yield session
except Exception as e:
logger.error(f"Database session error: {e}")
await session.rollback()
raise
finally:
await session.close()
async def init_db():
"""Initialize database (create tables)."""
from src.models.database import Base
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
logger.info("Database tables initialized")
async def close_db():
"""Close database connections."""
await engine.dispose()
logger.info("Database connections closed")
|