fix: race-proof user creation + unique Identity index + collection extension point#67
Merged
Merged
Conversation
…tension point Resolves #65 — closes the check-then-act race in UserServiceRepositoryBase.GetUserAsync that produces duplicate UserEntity rows when two near-simultaneous first-time logins for the same identity hit a fresh per-environment User collection. - Added unique index on User.Identity declared in UserRepositoryCollection.Indices. - Added MongoWriteException/DuplicateKey catch in UserServiceRepositoryBase.GetUserAsync: on conflict, re-read by Identity and return the winning row. - Promoted UserRepositoryCollection<TUserEntity> to public so consumers can subclass it to declare per-deployment indices. - Added ThargaTeamOptions.RegisterUserRepository<TUserEntity, TCollection>() overload for registering the consumer subclass. New Tharga.Team.MongoDB.Tests project with 7 tests. 296 / 296 passing. Migration note for the PR: existing deployments with duplicate User rows must dedupe before upgrading; the unique index will fail to create against conflicting data and Tharga.MongoDB's AssureIndex surfaces this in startup logs.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Summary
Resolves #65. Closes the check-then-act race in
UserServiceRepositoryBase.GetUserAsyncthat produces duplicateUserEntityrows when two near-simultaneous first-time logins for the same identity hit a fresh per-environment User collection.Identityindex declared onUserRepositoryCollection.Indices.Tharga.MongoDB'sAssureIndexcreates and maintains it.GetUserAsync— catchesMongoWriteExceptionwithWriteError.Category == ServerErrorCategory.DuplicateKeyfromAddAsync, re-reads by Identity, and returns the winning row.UserRepositoryCollection<TUserEntity>is nowpublic; subclass it to declare per-deployment indices. Register the subclass via the newThargaTeamOptions.RegisterUserRepository<TUserEntity, TCollection>()overload. See the new "Adding per-deployment User indices" section inTharga.Team.MongoDB/README.md.Existing deployments with duplicate User rows must dedupe before upgrading. The unique
Identityindex will fail to create against conflicting data;Tharga.MongoDB'sAssureIndexsurfaces this in startup logs and refuses to apply the index until the data is clean.Recommended cleanup (mongosh, per affected database):
```js
db.User.aggregate([
{ $group: { _id: "$Identity", ids: { $push: "$_id" }, count: { $sum: 1 } } },
{ $match: { count: { $gt: 1 } } }
])
```
Then delete all but the earliest `_id` per group (or whichever row is canonical).
Behaviour change for consumers
UserServiceRepositoryBaseconstructor unchanged. Existing subclasses (UserServicein consumer projects) compile without change.ThargaTeamOptions.RegisterUserRepository<TUserEntity>()continues to register the built-in collection. The new<TUserEntity, TCollection>()overload is opt-in.UserRepositoryCollection<TUserEntity>is nowpublic(wasinternal). Source-compatible promotion.Consumer cleanup unlocked (separate downstream bumps)
UserIdentityIndexHostedService(it created the index out-of-band) and itsMongoWriteException/DuplicateKeycatch inUserService.GetUserAsync(defensive code that becomes redundant). Tracked downstream.Out of scope
FindOneAndUpdate \$setOnInsertupsert path — both approaches close the race; catch-DuplicateKey is the chosen minimal change.Test plan