fix: handle duplicate room creation race condition (#165)

Merged
SI
9mo ago
+193-50
  • fix: handle duplicate room creation race condition

Catch 23505 unique constraint error when frontend creates room before backend's selectRoom sees it. Skip notification if room already exists.

  • fix: keep Promise.all, just wrap in try/catch

Simpler fix - keep parallel execution, only catch the 23505 error.

  • fix: handle duplicate room in createNewRoom.ts

This is the actual fix - createNewRoom is called during setupConversation at request START, which is where the 500 error occurs.

  • fix: revert try/catch bandaid - no longer needed

Frontend no longer creates rooms, so no race condition. Backend is single source of truth for room creation.

  • fix: use upsert instead of insert for room creation
  • Changed insertRoom to use upsert with ignoreDuplicates: true
  • Reverted try/catch in createNewRoom.ts and handleChatCompletion.ts
  • Cleaner solution: duplicates are silently ignored at the DB level
  • fix: use upsert instead of insert for rooms

One-line fix: change insert to upsert in insertRoom.ts. Removes try/catch workarounds since upsert handles duplicates.

  • refactor: rename insertRoom to upsertRoom to match method

Renames the function and file from insertRoom to upsertRoom to align with the actual Supabase method being called (.upsert()).

  • test: add unit tests for upsertRoom function

Adds comprehensive unit tests covering:

  • Basic upsert operation
  • Null artist_id handling
  • Null topic handling
  • Error handling on upsert failure
  • Upsert behavior (update on conflict)

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability of chat room creation and updates to handle edge cases more gracefully.
  • Tests

    • Added comprehensive test coverage for room creation and update operations.
    • Updated existing tests to verify consistent room handling across chat workflows.

✏️ Tip: You can customize this high-level summary in your review settings.