Skip to content

Commit 682f01f

Browse files
committed
added tests and toggle for sqlalchemy integration
1 parent 8338fb8 commit 682f01f

File tree

3 files changed

+644
-277
lines changed

3 files changed

+644
-277
lines changed

autogpt_platform/backend/.env.default

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ DIRECT_URL="postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}:${DB_PORT}/${DB_NAME}?
1818
PRISMA_SCHEMA="postgres/schema.prisma"
1919

2020
# SQLAlchemy Configuration (for gradual migration from Prisma)
21+
# Set to true to enable SQLAlchemy alongside Prisma (both ORMs coexist during migration)
22+
ENABLE_SQLALCHEMY=false
2123
SQLALCHEMY_POOL_SIZE=10
2224
SQLALCHEMY_MAX_OVERFLOW=5
2325
SQLALCHEMY_POOL_TIMEOUT=30

autogpt_platform/backend/backend/data/sqlalchemy.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import logging
1111
import re
12+
from contextlib import asynccontextmanager
1213
from typing import AsyncGenerator
1314

1415
from sqlalchemy.ext.asyncio import (
@@ -17,7 +18,6 @@
1718
async_sessionmaker,
1819
create_async_engine,
1920
)
20-
from sqlalchemy.pool import QueuePool
2121

2222
from 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
168166
async def get_session() -> AsyncGenerator[AsyncSession, None]:
169167
"""
170168
FastAPI dependency that provides database session.

0 commit comments

Comments
 (0)