Files
sdb2-backend/app/core/database.py

64 lines
1.8 KiB
Python

import asyncio
from collections.abc import AsyncGenerator, Callable
from alembic.config import Config
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
from sqlmodel.ext.asyncio.session import AsyncSession
# Import all models to ensure SQLModel metadata discovery
import app.models # noqa: F401
from alembic import command
from app.core.config import settings
from app.core.logging import get_logger
engine: AsyncEngine = create_async_engine(
settings.DATABASE_URL,
echo=settings.DATABASE_ECHO,
)
async def get_db() -> AsyncGenerator[AsyncSession]:
"""Get a database session for dependency injection."""
logger = get_logger(__name__)
async with AsyncSession(engine) as session:
try:
yield session
except Exception:
logger.exception("Database session error")
await session.rollback()
raise
finally:
await session.close()
def get_session_factory() -> Callable[[], AsyncSession]:
"""Get a session factory function for services."""
def session_factory() -> AsyncSession:
return AsyncSession(engine)
return session_factory
async def init_db() -> None:
"""Initialize the database using Alembic migrations."""
logger = get_logger(__name__)
try:
logger.info("Running database migrations")
# Run Alembic migrations programmatically
# Get the alembic config
alembic_cfg = Config("alembic.ini")
# Run migrations to the latest revision in a thread pool to avoid blocking
await asyncio.get_event_loop().run_in_executor(
None, command.upgrade, alembic_cfg, "head",
)
logger.info("Database migrations completed successfully")
except Exception:
logger.exception("Failed to initialize database")
raise