NeahStable/scripts/cleanup-duplicate-events.sql
2026-01-15 13:37:05 +01:00

58 lines
1.7 KiB
SQL

-- Script SQL to clean up duplicate events
-- Keeps events with externalEventId, removes duplicates without externalEventId
-- For events with same title, calendarId, and date (for all-day events) or same start/end (for timed events)
-- First, let's see the duplicates
SELECT
"calendarId",
title,
DATE("start") as start_date,
DATE("end") as end_date,
COUNT(*) as duplicate_count,
COUNT(CASE WHEN "externalEventId" IS NOT NULL THEN 1 END) as with_external_id,
COUNT(CASE WHEN "externalEventId" IS NULL THEN 1 END) as without_external_id
FROM "Event"
WHERE title IN ('Vendredi saint', 'Dimanche de Pâques')
GROUP BY "calendarId", title, DATE("start"), DATE("end")
HAVING COUNT(*) > 1
ORDER BY title, start_date;
-- Delete duplicates: keep the one with externalEventId, or the oldest one if none have externalEventId
WITH duplicates AS (
SELECT
id,
"calendarId",
title,
"start",
"end",
"externalEventId",
ROW_NUMBER() OVER (
PARTITION BY
"calendarId",
title,
DATE("start"),
DATE("end")
ORDER BY
CASE WHEN "externalEventId" IS NOT NULL THEN 0 ELSE 1 END, -- Prefer events with externalEventId
"createdAt" ASC -- If no externalEventId, keep the oldest
) as rn
FROM "Event"
WHERE title IN ('Vendredi saint', 'Dimanche de Pâques')
)
DELETE FROM "Event"
WHERE id IN (
SELECT id FROM duplicates WHERE rn > 1
);
-- Verify cleanup
SELECT
id,
title,
"calendarId",
"externalEventId",
"start",
"end"
FROM "Event"
WHERE title IN ('Vendredi saint', 'Dimanche de Pâques')
ORDER BY title, "start";