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) }