16 lines
702 B
MySQL
16 lines
702 B
MySQL
|
|
-- Remove duplicate participant_expected_values rows before adding unique constraint.
|
||
|
|
-- For each (participant_id, sports_season_id) group with duplicates, keep the row
|
||
|
|
-- that has a non-null source_elo (preferred) or the most recently updated row.
|
||
|
|
DELETE FROM participant_expected_values
|
||
|
|
WHERE id NOT IN (
|
||
|
|
SELECT DISTINCT ON (participant_id, sports_season_id) id
|
||
|
|
FROM participant_expected_values
|
||
|
|
ORDER BY participant_id, sports_season_id,
|
||
|
|
(source_elo IS NOT NULL) DESC,
|
||
|
|
updated_at DESC
|
||
|
|
);
|
||
|
|
|
||
|
|
-- Now add unique constraint to prevent future duplicates.
|
||
|
|
CREATE UNIQUE INDEX "participant_ev_participant_season_unique"
|
||
|
|
ON "participant_expected_values" ("participant_id", "sports_season_id");
|