Spaces:
Runtime error
Runtime error
Download core/db.py from Quratulain1/Deploy-Backend: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/Quratulain1/Deploy-Backend/resolve/main/core/db.py
- Command line
-
hf download hf://spaces/Quratulain1/Deploy-Backend/core/db.py
-
curl -L -o db.py https://huggingface.co/spaces/Quratulain1/Deploy-Backend/resolve/main/core/db.py
1.63 kB
| from sqlalchemy.ext.asyncio import create_async_engine | |
| from sqlalchemy.orm import sessionmaker | |
| from sqlalchemy.pool import NullPool | |
| from sqlmodel.ext.asyncio.session import AsyncSession | |
| from .config import settings | |
| # Handle URL conversion for asyncpg compatibility | |
| database_url = settings.DATABASE_URL | |
| if database_url.startswith("postgresql://") and not database_url.startswith("postgresql+asyncpg://"): | |
| # Convert to asyncpg format if it's the standard postgresql:// format | |
| database_url = database_url.replace("postgresql://", "postgresql+asyncpg://", 1) | |
| # asyncpg doesn't support sslmode/channel_binding in the URL query string the same way | |
| # For Neon DB, we need to strip query parameters that asyncpg can't handle | |
| # This is the same approach used in test_conn.py which works | |
| if "?" in database_url: | |
| # Extract the base URL without query parameters for asyncpg compatibility | |
| base_url = database_url.split("?")[0] | |
| database_url = base_url | |
| # For Neon DB with asyncpg, we need to handle SSL properly | |
| # Use no connection pooling for Neon's serverless architecture to avoid connection timeouts | |
| engine = create_async_engine( | |
| database_url, | |
| echo=False, | |
| future=True, | |
| poolclass=NullPool, # Use null pool for serverless | |
| # Additional asyncpg-specific parameters for Neon | |
| connect_args={ | |
| "server_settings": { | |
| "application_name": "hack-2-web-app", | |
| } | |
| } | |
| ) | |
| async_session_maker = sessionmaker( | |
| engine, class_=AsyncSession, expire_on_commit=False | |
| ) | |
| async def get_session() -> AsyncSession: | |
| async with async_session_maker() as session: | |
| yield session | |