Skip to content

Commit 0085e5e

Browse files
committed
integrate into database manager and rest api.
1 parent 5c8dac4 commit 0085e5e

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

autogpt_platform/backend/backend/executor/database.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,42 @@ async def lifespan(self, app: "FastAPI"):
8888
logger.info(f"[{self.service_name}] ⏳ Connecting to Database...")
8989
await db.connect()
9090

91+
# Initialize SQLAlchemy if enabled (for gradual migration from Prisma)
92+
if config.enable_sqlalchemy:
93+
try:
94+
from backend.data import sqlalchemy as sa
95+
96+
engine = sa.create_engine()
97+
sa.initialize(engine)
98+
app.state.db_engine = engine
99+
logger.info(
100+
f"[{self.service_name}] ✓ SQLAlchemy initialized "
101+
f"(pool_size={config.sqlalchemy_pool_size}, "
102+
f"max_overflow={config.sqlalchemy_max_overflow})"
103+
)
104+
except Exception as e:
105+
logger.error(
106+
f"[{self.service_name}] Failed to initialize SQLAlchemy: {e}"
107+
)
108+
raise
109+
91110
logger.info(f"[{self.service_name}] ✅ Ready")
92111
yield
93112

94113
logger.info(f"[{self.service_name}] ⏳ Disconnecting Database...")
114+
115+
# Dispose SQLAlchemy if it was enabled
116+
if config.enable_sqlalchemy:
117+
try:
118+
from backend.data import sqlalchemy as sa
119+
120+
await sa.dispose()
121+
logger.info(f"[{self.service_name}] ✓ SQLAlchemy disposed")
122+
except Exception as e:
123+
logger.warning(
124+
f"[{self.service_name}] Error disposing SQLAlchemy: {e}"
125+
)
126+
95127
await db.disconnect()
96128

97129
async def health_check(self) -> str:

autogpt_platform/backend/backend/server/rest_api.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ async def lifespan_context(app: fastapi.FastAPI):
7878

7979
await backend.data.db.connect()
8080

81+
# Initialize SQLAlchemy if enabled (for gradual migration from Prisma)
82+
config = backend.util.settings.Config()
83+
if config.enable_sqlalchemy:
84+
try:
85+
from backend.data import sqlalchemy as sa
86+
87+
engine = sa.create_engine()
88+
sa.initialize(engine)
89+
app.state.db_engine = engine
90+
logger.info(
91+
f"✓ AgentServer: SQLAlchemy initialized "
92+
f"(pool_size={config.sqlalchemy_pool_size}, "
93+
f"max_overflow={config.sqlalchemy_max_overflow})"
94+
)
95+
except Exception as e:
96+
logger.error(f"Failed to initialize SQLAlchemy: {e}")
97+
raise
98+
8199
# Configure thread pool for FastAPI sync operation performance
82100
# CRITICAL: FastAPI automatically runs ALL sync functions in this thread pool:
83101
# - Any endpoint defined with 'def' (not async def)
@@ -118,6 +136,16 @@ async def lifespan_context(app: fastapi.FastAPI):
118136
except Exception as e:
119137
logger.warning(f"Error shutting down cloud storage handler: {e}")
120138

139+
# Dispose SQLAlchemy if it was enabled
140+
if config.enable_sqlalchemy:
141+
try:
142+
from backend.data import sqlalchemy as sa
143+
144+
await sa.dispose()
145+
logger.info("✓ AgentServer: SQLAlchemy disposed")
146+
except Exception as e:
147+
logger.warning(f"Error disposing SQLAlchemy: {e}")
148+
121149
await backend.data.db.disconnect()
122150

123151

autogpt_platform/backend/backend/util/settings.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ class Config(UpdateTrackingModel["Config"], BaseSettings):
274274
)
275275

276276
# SQLAlchemy Configuration
277+
enable_sqlalchemy: bool = Field(
278+
default=False,
279+
description="Enable SQLAlchemy database connections. Set to true to enable gradual migration from Prisma to SQLAlchemy. "
280+
"When disabled, only Prisma is used. When enabled, both ORMs coexist during transition.",
281+
)
282+
277283
sqlalchemy_pool_size: int = Field(
278284
default=10,
279285
ge=1,

0 commit comments

Comments
 (0)