Files
aiagent/backend/app/core/database.py
renjianbo 02d7cf8f62 feat: add Goal/Task data models, service layer, and API routes (Phase 1)
Main Agent 数字员工工厂基础设施:
- 新增 Goal 和 Task SQLAlchemy 数据模型
- Agent 模型新增 agent_type / input_schema / output_schema
- Execution 模型新增 goal_id 关联
- 新增 Goal/Task CRUD 服务层(含依赖检查、任务树、进度计算)
- 新增 /api/v1/goals (9端点) + /api/v1/tasks (8端点)
- 数据库迁移 013_add_goals_tasks

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-08 19:50:16 +08:00

60 lines
1.7 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
数据库配置
"""
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=Falsecommit 后仍可读取已加载标量,避免 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
import app.models.agent_vector_memory
import app.models.agent_learning_pattern
import app.models.agent_schedule
import app.models.knowledge_base
import app.models.notification
import app.models.orchestration_template
import app.models.plugin
import app.models.goal
import app.models.task
Base.metadata.create_all(bind=engine)