From 191888225f6cb63f49316e5198de9f2ff51ecaf8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Kr=C3=BCger?= Date: Sun, 14 Jun 2026 17:38:35 +0200 Subject: [PATCH] fix: delete goals once per match instead of once per team in syncGoals() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit syncGoals() was calling DELETE FROM goals WHERE match_id=X at the top, so processing goals2 (away team) wiped out goals1 (home team) that were just inserted. Every match with goals from both sides lost all home-team goals — Ronaldo's hat-trick vs Spain, Kane's vs Panama, and many others. Fix: move DELETE above the goals1/goals2 loop, executed once per match. Result: 2018 goal count corrected from 107 → 169; hat tricks from 8 → 18. Co-Authored-By: Claude Sonnet 4.6 --- scripts/sync.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/scripts/sync.ts b/scripts/sync.ts index 3c35674..e84139e 100644 --- a/scripts/sync.ts +++ b/scripts/sync.ts @@ -226,7 +226,6 @@ async function run() { } async function syncGoals(matchId: number, teamId: number, rawGoals: RawGoal[], isOwnGoalTeamId: number) { - await db.execute(sql`DELETE FROM goals WHERE match_id = ${matchId}`) for (const g of rawGoals) { if (!g.name) continue const minute = g.minute != null ? parseInt(String(g.minute)) : null @@ -294,8 +293,11 @@ async function run() { const score = parseScore(m.score) const group = m.group ?? null const matchId = await upsertMatch(year, m.round ?? 'Unknown', group, m.date ?? null, m.time ?? null, t1Id, t2Id, score, false) - if (m.goals1?.length) await syncGoals(matchId, t1Id, m.goals1, t2Id) - if (m.goals2?.length) await syncGoals(matchId, t2Id, m.goals2, t1Id) + if (m.goals1?.length || m.goals2?.length) { + await db.execute(sql`DELETE FROM goals WHERE match_id = ${matchId}`) + if (m.goals1?.length) await syncGoals(matchId, t1Id, m.goals1, t2Id) + if (m.goals2?.length) await syncGoals(matchId, t2Id, m.goals2, t1Id) + } matchCount++ goalCount += (m.goals1?.length ?? 0) + (m.goals2?.length ?? 0) }