fix(migration): make unique-index migration resilient + dedupe data - #2779
Merged
Conversation
Production data has duplicate rows (concurrent double-creates slipped past the non-atomic Rails uniqueness validations) that block the unique indexes. Dedupe invitations/workshop_invitations/meeting_invitations first (keep newest row, tie-break lowest id), and add if_not_exists so the migration re-runs after its partial first attempt on prod. Switch RSVP find_or_create_by/Invitation.create to create_or_find_by: once the unique index backs the validation, the racing double-create raises RecordNotUnique instead of silently duping.
mroderick
marked this pull request as ready for review
August 4, 2026 11:33
mroderick
force-pushed
the
fix/unique-index-deploy
branch
from
August 4, 2026 11:46
edeebae to
5b76e63
Compare
olleolleolle
approved these changes
Aug 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PR #2771 added DB unique indexes to back up model validation. But production data already contains duplicate
invitations/workshop_invitations/meeting_invitationsrows (a Rails uniqueness validation is not atomic, so concurrent double-creates slipped past it). The release-phase migration aborts in prod on aPG::UniqueViolation, and — because it runs withdisable_ddl_transaction!— it left 5 of 9 indexes applied with no rollback and the migration un-recorded, blocking every deploy.What this changes
Migration (
20260731104506) — now resilient and safe to re-run against prod's mid-flight state:attending=true/verified row is never dropped). No-op on clean data. Verified against the production dump: deletes 1105 / 1937 / 55 rows, leaves zero duplicate groups, never drops a soleattending=truerow.if_not_exists: trueto all 9add_indexcalls, so prod's 5 already-created indexes are skipped and the remaining 4 are created. Wrapped the dedupeexecuteinsafety_assuredfor strong_migrations.Because prod never recorded the migration, the next deploy re-runs it and heals itself — no manual prod DB surgery.
RSVP call sites (
events/workshopscontrollers) — switchfind_or_create_by/ bareInvitation.createtocreate_or_find_by(atomicINSERT ... ON CONFLICT). Once the unique index backs the validation, the racing double-create would raise unhandledRecordNotUnique→ 500;create_or_find_byreturns the existing row instead.Auth callback (
auth_services_controller#create) — a double-fired OAuth callback can pass the initial service lookup concurrently, then collide on the unique(uid, provider)index (or its uniqueness validation) duringmember.save!, erroring the request. RescueRecordInvalid/RecordNotUniqueand reuse the already-created auth service, guarded byfind_by!so a non-race failure still surfaces.can_log_intoggles only on actual creation so the losing callback doesn't flip the winner's flag back.Verification