fix(db): create_all checkfirst=True 修復 CrashLoopBackOff
Some checks failed
CD Pipeline / build-and-deploy (push) Failing after 12m19s

rolling update 時 create_all 嘗試重建既有 index 導致
"ix_incident_evidence_incident_id already exists" 啟動失敗。
checkfirst=True 讓 SQLAlchemy 跳過已存在的 table/index,
init_db() 從此冪等,不再造成 CrashLoopBackOff。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
OG T
2026-04-15 15:00:49 +08:00
parent 777e40d618
commit a142e6e937

View File

@@ -144,7 +144,9 @@ async def init_db() -> None:
"""
engine = get_engine()
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)
# checkfirst=True: 存在的 table/index 直接跳過,避免 rolling update 重啟時
# "relation already exists" CrashLoopBackOff2026-04-15 Claude Sonnet 4.6 Phase 3 修復)
await conn.run_sync(lambda c: Base.metadata.create_all(c, checkfirst=True))
# 2026-04-02 Claude Code: 確保 risklevel enum 包含 'high' 值
# Phase 23 新增,避免舊 DB 缺少此值導致 InvalidTextRepresentation