From 28165e4f4a8a9837536588b8dbd67c802b4ad0fa Mon Sep 17 00:00:00 2001 From: Eric Gullickson <16152721+ericgullickson@users.noreply.github.com> Date: Mon, 16 Feb 2026 11:03:35 -0600 Subject: [PATCH] fix: deduplicate user_preferences before unique constraint (refs #206) Users with both auth0_sub and UUID rows in user_preferences get the same user_profile_id after backfill, causing unique constraint violation on rename. Keep the newest row per user_profile_id. Co-Authored-By: Claude Opus 4.6 --- .../migrations/001_migrate_user_id_to_uuid.sql | 8 ++++++++ 1 file changed, 8 insertions(+) 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;