Spaces:
Sleeping
Sleeping
| import os | |
| from collections.abc import AsyncGenerator | |
| from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine, async_sessionmaker | |
| from sqlalchemy.pool import NullPool | |
| DATABASE_URL = os.getenv("DATABASE_URL") | |
| if not DATABASE_URL: | |
| DATABASE_URL = "sqlite+aiosqlite:///./codebookly_testing.db" | |
| else: | |
| if DATABASE_URL.startswith("postgres://"): | |
| DATABASE_URL = DATABASE_URL.replace("postgres://", "postgresql+asyncpg://", 1) | |
| elif DATABASE_URL.startswith("postgresql://") and "+asyncpg" not in DATABASE_URL: | |
| DATABASE_URL = DATABASE_URL.replace("postgresql://", "postgresql+asyncpg://", 1) | |
| engine = create_async_engine(DATABASE_URL) | |
| async_session_maker = async_sessionmaker(engine, class_=AsyncSession) | |
| async def get_async_session() -> AsyncGenerator[AsyncSession, None]: | |
| async with async_session_maker() as session: | |
| yield session |