All checks were successful
Build and Deploy Log Center / build-and-deploy (push) Successful in 2m16s
- 新增 Project 模型(repo_url, local_path, name, description) - 项目 CRUD API(GET/PUT /api/v1/projects) - 日志上报自动 upsert Project 记录 - ErrorLog 增加 failure_reason 字段 - update_task_status / create_repair_report 写入失败原因 - Repair Agent 优先从 API 获取项目配置,回退 .env - 新增 Web 端「项目管理」页面(表格 + 行内编辑) - BugList/BugDetail/RepairList 展示失败原因 - 更新接入指南文档 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
50 lines
1.9 KiB
Python
50 lines
1.9 KiB
Python
from sqlmodel import SQLModel, create_engine
|
|
from sqlmodel.ext.asyncio.session import AsyncSession
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from sqlalchemy import text
|
|
import os
|
|
from dotenv import load_dotenv
|
|
|
|
load_dotenv()
|
|
|
|
DB_USER = os.getenv("DB_USER")
|
|
DB_PASSWORD = os.getenv("DB_PASSWORD")
|
|
DB_HOST = os.getenv("DB_HOST")
|
|
DB_PORT = os.getenv("DB_PORT", "5432")
|
|
DB_NAME = os.getenv("DB_NAME")
|
|
|
|
DATABASE_URL = f"postgresql+asyncpg://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
|
|
|
|
engine = create_async_engine(DATABASE_URL, echo=True, future=True)
|
|
|
|
async def init_db():
|
|
async with engine.begin() as conn:
|
|
# await conn.run_sync(SQLModel.metadata.drop_all)
|
|
await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
# Migrate: add new columns to existing repairtask table
|
|
migrations = [
|
|
"ALTER TABLE repairtask ADD COLUMN IF NOT EXISTS repair_round INTEGER DEFAULT 1",
|
|
"ALTER TABLE repairtask ADD COLUMN IF NOT EXISTS failure_reason TEXT",
|
|
# Log source support
|
|
"ALTER TABLE errorlog ADD COLUMN IF NOT EXISTS source VARCHAR(20) DEFAULT 'runtime'",
|
|
"ALTER TABLE errorlog ALTER COLUMN file_path DROP NOT NULL",
|
|
"ALTER TABLE errorlog ALTER COLUMN line_number DROP NOT NULL",
|
|
"CREATE INDEX IF NOT EXISTS ix_errorlog_source ON errorlog (source)",
|
|
# ErrorLog failure_reason
|
|
"ALTER TABLE errorlog ADD COLUMN IF NOT EXISTS failure_reason TEXT",
|
|
]
|
|
for sql in migrations:
|
|
try:
|
|
await conn.execute(text(sql))
|
|
except Exception:
|
|
pass # Already applied
|
|
|
|
async def get_session() -> AsyncSession:
|
|
async_session = sessionmaker(
|
|
engine, class_=AsyncSession, expire_on_commit=False
|
|
)
|
|
async with async_session() as session:
|
|
yield session
|