Skip to content

fix: prevent duplicate waiting list entries#2482

Open
mroderick wants to merge 8 commits intocodebar:masterfrom
mroderick:fix-duplicate-waiting-list-entries
Open

fix: prevent duplicate waiting list entries#2482
mroderick wants to merge 8 commits intocodebar:masterfrom
mroderick:fix-duplicate-waiting-list-entries

Conversation

@mroderick
Copy link
Collaborator

Summary

Fixes #2479 - Prevents duplicate waiting list entries by making WaitingList.add() idempotent and adding a database-level unique constraint.

Changes:

  • Made WaitingList.add() idempotent using find_or_create_by (first call wins for auto_rsvp)
  • Added unique database constraint on waiting_lists.invitation_id
  • Migration cleans up existing duplicates (keeping oldest entry) before adding constraint
  • Added comprehensive tests at model and controller levels

Root cause: The controller action is not idempotent - double-clicks, browser back button, or network retries could create duplicate waiting list entries for the same invitation.

Solution approach:

  1. Application-level: find_or_create_by makes the model method idempotent
  2. Database-level: Unique constraint provides defense-in-depth against race conditions
  3. Data cleanup: Migration safely removes existing duplicates before adding constraint

Test Plan

  • Model tests verify idempotency (returns same record on multiple calls)
  • Model tests verify auto_rsvp preservation (first call wins)
  • Controller tests verify double-submission protection
  • Full test suite passes: 847 examples, 0 failures
  • RuboCop clean (no offenses)
  • Manual verification in console confirms behavior
  • Migration tested (cleans duplicates, adds constraint)

Coverage

  • 95.43% code coverage (3235/3390 LOC)
  • 12 new/modified tests all passing
  • No regressions detected in full test suite

Design addresses issue codebar#2479 by:
- Making WaitingList.add() idempotent with find_or_create_by
- Adding unique database constraint on invitation_id
- Cleaning up existing duplicates in migration
Prevents accidental commit of worktree directories
- Remove hard-coded line numbers
- Make controller test more comprehensive (check responses and flash messages)
- Improve migration safety (logging, delete_all, verification step)
- Combine test and linting verification into single task
- Make manual verification required instead of optional
- Add production deployment and rollback notes
Use find_or_create_by instead of create to prevent duplicate
waiting list entries for the same invitation. First call wins
for auto_rsvp setting.

Refs codebar#2479
Add comprehensive test to verify double-submission protection at the
controller level. Test confirms that multiple identical POST requests
result in only a single waiting list entry.
Migration cleans up existing duplicates (keeping oldest entry)
before adding unique index to prevent future duplicates.

Refs codebar#2479
- Use described_class instead of WaitingList class name
- Use describe instead of context for method testing
- Replace .times.map with Array.new pattern
- Use Array#new with block instead of times.map
- Remove useless variable assignment
- Use `be` matcher instead of `eq` for boolean
- Fix trailing whitespace
@mroderick mroderick marked this pull request as ready for review February 11, 2026 16:00
Copy link
Contributor

@KimberleyCook KimberleyCook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Duplicate entries in waiting lists

2 participants