Spaces:
Sleeping
Sleeping
| from sqlmodel import SQLModel | |
| from sqlmodel.ext.asyncio.session import AsyncSession | |
| from sqlalchemy.ext.asyncio import create_async_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from config import settings | |
| # Create async database engine | |
| # Note: For SQLite, we disable same_thread check as we're in an async context | |
| connect_args = {} | |
| if settings.DATABASE_URL.startswith("sqlite"): | |
| connect_args = {"check_same_thread": False} | |
| engine = create_async_engine( | |
| settings.DATABASE_URL, | |
| echo=False, | |
| future=True, | |
| connect_args=connect_args | |
| ) | |
| # Async session factory | |
| async_session_maker = sessionmaker( | |
| engine, | |
| class_=AsyncSession, | |
| expire_on_commit=False | |
| ) | |
| async def init_db(): | |
| async with engine.begin() as conn: | |
| # Import models here to make sure they register with SQLModel.metadata | |
| from models import ModerationLog | |
| await conn.run_sync(SQLModel.metadata.create_all) | |
| async def get_session(): | |
| async with async_session_maker() as session: | |
| yield session | |