fix(realestate-lab): 최종 리뷰 이슈 수정 — FK CASCADE, 단일 연결, 동시성 가드

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-06 08:49:05 +09:00
parent bdfcdee5fd
commit afc159c84d
3 changed files with 41 additions and 35 deletions

View File

@@ -116,7 +116,7 @@ def init_db():
conn.execute("""
CREATE TABLE IF NOT EXISTS match_results (
id INTEGER PRIMARY KEY AUTOINCREMENT,
announcement_id INTEGER NOT NULL,
announcement_id INTEGER NOT NULL REFERENCES announcements(id) ON DELETE CASCADE,
model_id INTEGER,
match_score INTEGER NOT NULL DEFAULT 0,
match_reasons TEXT NOT NULL DEFAULT '[]',
@@ -341,8 +341,7 @@ def update_announcement(ann_id: int, data: Dict[str, Any]) -> Optional[Dict[str,
def delete_announcement(ann_id: int) -> bool:
with _conn() as conn:
# 관련 매칭 결과도 삭제
conn.execute("DELETE FROM match_results WHERE announcement_id = ?", (ann_id,))
# match_results는 FK CASCADE로 자동 삭제
cur = conn.execute("DELETE FROM announcements WHERE id = ?", (ann_id,))
return cur.rowcount > 0
@@ -351,14 +350,12 @@ def update_all_statuses():
"""모든 진행중 공고의 status를 날짜 기반으로 재계산."""
with _conn() as conn:
rows = conn.execute(
"SELECT id, receipt_start, receipt_end, winner_date FROM announcements "
"SELECT id, status, receipt_start, receipt_end, winner_date FROM announcements "
"WHERE status != '완료' AND (receipt_start IS NOT NULL OR receipt_end IS NOT NULL OR winner_date IS NOT NULL)"
).fetchall()
for r in rows:
new_status = compute_status(r["receipt_start"], r["receipt_end"], r["winner_date"])
if new_status != "완료":
conn.execute("UPDATE announcements SET status = ? WHERE id = ?", (new_status, r["id"]))
else:
if new_status != r["status"]: # only update if status actually changed
conn.execute(
"UPDATE announcements SET status = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ','now') WHERE id = ?",
(new_status, r["id"]),
@@ -509,11 +506,6 @@ def mark_match_read(match_id: int) -> bool:
return cur.rowcount > 0
def clear_match_results():
with _conn() as conn:
conn.execute("DELETE FROM match_results")
# ── collect_log CRUD ─────────────────────────────────────────────────────────
def save_collect_log(new_count: int, total_count: int, error: str = None):