fix: remove stale knockout matches when bracket opponents change on Wikipedia

The upsert key is (year, team1_id, team2_id, date), so when Wikipedia updates
a placeholder bracket entry to real opponents, the old pairing is never
overwritten — it just accumulates alongside the new one. Before upserting a
knockout match, delete any existing match in the same year+round that involves
either team but isn't the exact same pairing.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-01 10:14:54 +02:00
co-authored by Claude Sonnet 4.6
parent c96c683580
commit a808d0726a
+29
View File
@@ -38,6 +38,35 @@ async function run() {
ft: [number, number] | undefined, et: [number, number] | undefined, p: [number, number] | undefined,
isQuali: boolean,
) {
// For knockout matches, a team plays exactly once per round. If Wikipedia updated
// the bracket (e.g. placeholder → real opponent), the old pairing won't be overwritten
// by the upsert (different key). Delete any stale match involving either team in this
// round before inserting.
if (!group) {
await db.execute(sql`
DELETE FROM goals WHERE match_id IN (
SELECT id FROM matches
WHERE tournament_year = ${year}
AND round = ${round}
AND group_name IS NULL
AND is_quali_playoff = ${isQuali}
AND (team1_id = ${team1Id} OR team2_id = ${team1Id} OR team1_id = ${team2Id} OR team2_id = ${team2Id})
AND NOT (team1_id = ${team1Id} AND team2_id = ${team2Id})
AND NOT (team1_id = ${team2Id} AND team2_id = ${team1Id})
)
`)
await db.execute(sql`
DELETE FROM matches
WHERE tournament_year = ${year}
AND round = ${round}
AND group_name IS NULL
AND is_quali_playoff = ${isQuali}
AND (team1_id = ${team1Id} OR team2_id = ${team1Id} OR team1_id = ${team2Id} OR team2_id = ${team2Id})
AND NOT (team1_id = ${team1Id} AND team2_id = ${team2Id})
AND NOT (team1_id = ${team2Id} AND team2_id = ${team1Id})
`)
}
const rows = await db.execute(sql`
INSERT INTO matches (tournament_year, round, group_name, date, time_local, team1_id, team2_id,
score_ft_home, score_ft_away, score_et_home, score_et_away,