- agent_runtime: orchestrator、core/memory/schemas 调整 - agent_monitoring API、service、agent_llm_log 模型与 database 注册 - 前端 AgentDashboard、AgentConfig、Agents/MainLayout/路由与 AgentChat - 文档:(红头)项目核心文档汇总、自主AI Agent改造完成情况、AI agent改造计划 Made-with: Cursor
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
"""
|
||
数据库配置
|
||
"""
|
||
from sqlalchemy import create_engine
|
||
from sqlalchemy.ext.declarative import declarative_base
|
||
from sqlalchemy.orm import sessionmaker
|
||
from app.core.config import settings
|
||
|
||
# 创建数据库引擎(MySQL)
|
||
engine = create_engine(
|
||
settings.DATABASE_URL,
|
||
pool_pre_ping=True,
|
||
pool_size=10,
|
||
max_overflow=20,
|
||
echo=settings.DEBUG # 开发环境显示SQL
|
||
)
|
||
|
||
# 创建会话工厂
|
||
# expire_on_commit=False:commit 后仍可读取已加载标量,避免 FastAPI 在序列化 ExecutionResponse 时
|
||
# 因会话已关闭而再次触发懒加载,从而出现「仅 HTTP 报 DATABASE_ERROR、TestClient 正常」的现象。
|
||
SessionLocal = sessionmaker(
|
||
autocommit=False, autoflush=False, bind=engine, expire_on_commit=False
|
||
)
|
||
|
||
# 创建基础模型类
|
||
Base = declarative_base()
|
||
|
||
|
||
def get_db():
|
||
"""获取数据库会话"""
|
||
db = SessionLocal()
|
||
try:
|
||
yield db
|
||
finally:
|
||
db.close()
|
||
|
||
|
||
def init_db():
|
||
"""初始化数据库,创建所有表"""
|
||
# 导入所有模型,确保它们被注册
|
||
import app.models.user
|
||
import app.models.workflow
|
||
import app.models.agent
|
||
import app.models.execution
|
||
import app.models.model_config
|
||
import app.models.workflow_template
|
||
import app.models.permission
|
||
import app.models.alert_rule
|
||
import app.models.agent_llm_log
|
||
Base.metadata.create_all(bind=engine)
|