refactor: Sprint 3 - 引入 UnitOfWork + Repository 架构

新增:
- src/db/unit_of_work.py: UnitOfWork 事务封装
- src/db/repositories.py: Match/Team/League/Prediction Repository

重构:
- 删除 src/data/match_lookup.py(由 Repository 替代)
- 数据源(bzzoiro/understat/injuries)不再自行 commit
- API 路由(ingest)改用 UnitOfWork
- LLM 服务(predict/orchestrator/eval/backtest)改用 UnitOfWork

事务边界统一由调用方控制,数据层不再自行决定 commit。
This commit is contained in:
shangfangjian
2026-09-15 00:42:05 +08:00
parent cb36dc3ef9
commit 483cb956ba
11 changed files with 281 additions and 117 deletions
+5 -3
View File
@@ -11,6 +11,7 @@ from dataclasses import dataclass
from src.core.config import settings
from src.db.base import AsyncSessionLocal
from src.db.models import Match, Prediction
from src.db.unit_of_work import get_uow
from src.llm.agents.base import AgentReport, AgentSpec, load_agent_prompt
from src.llm.context_builder import (
MatchHeader,
@@ -184,8 +185,9 @@ async def predict_match_multi(
_reports_to_json(reports).encode("utf-8")
).hexdigest()
# 4. 存库
async with AsyncSessionLocal() as db:
# 4. 存库(使用 UnitOfWork)
async with get_uow() as uow:
db = uow.session
m = await db.get(Match, match_id)
if m is None:
raise ValueError(f"match {match_id} not found")
@@ -218,7 +220,7 @@ async def predict_match_multi(
input_hash=input_hash,
)
db.add(pred)
await db.commit()
await uow.commit()
await db.refresh(pred)
return MultiPredictResult(