99
1010import logging
1111import re
12+ from contextlib import asynccontextmanager
1213from typing import AsyncGenerator
1314
1415from sqlalchemy .ext .asyncio import (
1718 async_sessionmaker ,
1819 create_async_engine ,
1920)
20- from sqlalchemy .pool import QueuePool
2121
2222from backend .util .settings import Config
2323
@@ -32,21 +32,19 @@ def get_database_url() -> str:
3232 """
3333 Extract database URL from environment and convert to async format.
3434
35- Prisma URL: postgresql://user:pass@host:port/db?schema=platform
35+ Prisma URL: postgresql://user:pass@host:port/db?schema=platform&connect_timeout=60
3636 Async URL: postgresql+asyncpg://user:pass@host:port/db
3737
38- Returns the async-compatible URL without schema parameter (handled separately ).
38+ Returns the async-compatible URL without query parameters (handled via connect_args ).
3939 """
4040 prisma_url = Config ().database_url
4141
4242 # Replace postgresql:// with postgresql+asyncpg://
4343 async_url = prisma_url .replace ("postgresql://" , "postgresql+asyncpg://" )
4444
45- # Remove schema parameter (we'll handle via MetaData)
46- async_url = re .sub (r"\?schema=\w+" , "" , async_url )
47-
48- # Remove any remaining query parameters that might conflict
49- async_url = re .sub (r"&schema=\w+" , "" , async_url )
45+ # Remove ALL query parameters (schema, connect_timeout, etc.)
46+ # We'll handle these through connect_args instead
47+ async_url = re .sub (r"\?.*$" , "" , async_url )
5048
5149 return async_url
5250
@@ -88,7 +86,6 @@ def create_engine() -> AsyncEngine:
8886 engine = create_async_engine (
8987 url ,
9088 # Connection pool configuration
91- poolclass = QueuePool , # Standard connection pool
9289 pool_size = config .sqlalchemy_pool_size , # Persistent connections
9390 max_overflow = config .sqlalchemy_max_overflow , # Burst capacity
9491 pool_timeout = config .sqlalchemy_pool_timeout , # Wait time for connection
@@ -165,6 +162,7 @@ def initialize(engine: AsyncEngine) -> None:
165162 logger .info ("SQLAlchemy session factory initialized" )
166163
167164
165+ @asynccontextmanager
168166async def get_session () -> AsyncGenerator [AsyncSession , None ]:
169167 """
170168 FastAPI dependency that provides database session.
0 commit comments