🐛 Fix account creation misdiagnosed as duplicate phone #16

Merged
longtao.wu merged 1 commits from fix/phone-duplicate-error-misclassification into dev 2026-09-23 11:18:46 +08:00
Owner

Root-caused live on Fanyu: creating an account failed with "phone must be unique" for a never-used phone number. The users collection still had a stale, non-sparse email_unique index left from before the email→phone identifier migration; the first account created without an email field occupied its one null slot, and every subsequent account collided with it on insert — but upsertUser/updateAdministrator treated any duplicate-key error on the users collection as a phone conflict without checking which index actually fired.

Fixes: isPhoneUniqueDuplicateKeyError checks the error message for the real index name (mirrors the existing pattern in annotation_labels.go); EnsureIndexes now drops email_unique if present so other pre-migration databases don't carry the same landmine; directly dropped the stale index on live Fanyu to unblock immediately.

Tests: go build/vet clean, go test ./internal/api/... ./internal/store/... unchanged 75-failure baseline. Added TestPhoneUniqueDuplicateKeyErrorIgnoresUnrelatedIndex (live-Mongo gated, same convention as the existing annotation-review index test) reproducing the exact failure and locking in the fix.

🤖 Generated with Claude Code

Root-caused live on Fanyu: creating an account failed with "phone must be unique" for a never-used phone number. The users collection still had a stale, non-sparse email_unique index left from before the email→phone identifier migration; the first account created without an email field occupied its one null slot, and every subsequent account collided with it on insert — but upsertUser/updateAdministrator treated *any* duplicate-key error on the users collection as a phone conflict without checking which index actually fired. Fixes: isPhoneUniqueDuplicateKeyError checks the error message for the real index name (mirrors the existing pattern in annotation_labels.go); EnsureIndexes now drops email_unique if present so other pre-migration databases don't carry the same landmine; directly dropped the stale index on live Fanyu to unblock immediately. Tests: go build/vet clean, go test ./internal/api/... ./internal/store/... unchanged 75-failure baseline. Added TestPhoneUniqueDuplicateKeyErrorIgnoresUnrelatedIndex (live-Mongo gated, same convention as the existing annotation-review index test) reproducing the exact failure and locking in the fix. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
longtao.wu added 1 commit 2026-09-23 11:18:40 +08:00
Root-caused live on Fanyu: creating a new account failed with "phone
must be unique" for a phone number that had never been used. The users
collection still carried a stale, non-sparse email_unique index left
over from before accounts moved to phone as the sole identifier — this
codebase stopped declaring that index, but nothing ever dropped it from
an already-existing database. The first account created without an
email field (a normal, expected shape post-migration) occupied that
index's one allowed null slot; every account created after that
collided with it on insert, and upsertUser/updateAdministrator treated
*any* MongoDB duplicate-key error on the users collection as a phone
conflict without checking which index actually fired, misreporting the
real (email_unique) conflict as a phone conflict that had nothing to do
with the phone number entered.

Fixes:
- isPhoneUniqueDuplicateKeyError checks the error message for the real
  index name (phone_unique) instead of assuming any duplicate-key error
  on the collection is a phone conflict — mirrors the existing pattern
  already used for annotation_labels' unique-index checks.
- EnsureIndexes now drops email_unique if present (dropObsoleteIndexes),
  so any other pre-migration database doesn't carry the same landmine.
- Directly dropped the stale email_unique index on the live Fanyu
  database to unblock account creation immediately.

Added TestPhoneUniqueDuplicateKeyErrorIgnoresUnrelatedIndex (live-Mongo
gated, same convention as the existing annotation-review index test) to
reproduce the exact failure and lock in the fix.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
longtao.wu merged commit 654b64d9ab into dev 2026-09-23 11:18:46 +08:00
Sign in to join this conversation.
No Reviewers
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: longtao.wu/droboticflow#16
No description provided.