diff --git a/backend/src/core/identity-migration/migrations/001_migrate_user_id_to_uuid.sql b/backend/src/core/identity-migration/migrations/001_migrate_user_id_to_uuid.sql index 230a3b2..95d9d74 100644 --- a/backend/src/core/identity-migration/migrations/001_migrate_user_id_to_uuid.sql +++ b/backend/src/core/identity-migration/migrations/001_migrate_user_id_to_uuid.sql @@ -90,6 +90,14 @@ DELETE FROM user_preferences AND user_id ~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' AND NOT EXISTS (SELECT 1 FROM user_profiles WHERE id = user_preferences.user_id::uuid); +-- Deduplicate user_preferences: same user may have both an auth0_sub row and +-- a UUID row, both now mapping to the same user_profile_id. Keep the newest. +DELETE FROM user_preferences a + USING user_preferences b + WHERE a.user_profile_id = b.user_profile_id + AND a.user_profile_id IS NOT NULL + AND (a.updated_at < b.updated_at OR (a.updated_at = b.updated_at AND a.id < b.id)); + UPDATE saved_stations SET user_profile_id = up.id FROM user_profiles up WHERE saved_stations.user_id = up.auth0_sub AND saved_stations.user_profile_id IS NULL;