device: upsert on register to enforce one record per vault+party#5
Merged
device: upsert on register to enforce one record per vault+party#5
Conversation
Replace unconditional Create with FirstOrInit+Save so that re-registering the same (vault_id, party_name) updates the token/device_type instead of creating a duplicate row. Add a composite unique index on (vault_id, party_name) enforced at the DB level via AutoMigrate. Hard-delete on unregister to prevent soft-deleted rows from blocking the unique index on re-registration. Handle concurrent INSERT races via MySQL error 1062. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
Use clause.OnConflict so that registering an existing (vault_id, party_name) always overwrites the token and device_type in a single atomic statement, including under concurrent requests. Removes the isDuplicateKeyError helper which is no longer needed. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
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
db.Create()inRegisterDevicewithFirstOrInit+Saveso re-registering the same(vault_id, party_name)updates the existing row instead of creating a duplicateuniqueIndex:idx_vault_partyon(vault_id, party_name)inDeviceDBModel—AutoMigratewill create the DB-level constraint on next startupUnregisterDevicetoUnscoped().Delete()(hard delete) to prevent soft-deleted rows from blocking the unique index when a device re-registersisDuplicateKeyErrorhelper (MySQL error 1062) to gracefully handle concurrent registration racesTest plan
vault_id+party_nametwice with different tokens — confirm only one row exists and the token is updatedvault_id+party_name— confirm re-registration succeeds with no duplicate key errorAutoMigratecreatesidx_vault_partyunique index on a fresh DBidper group)🤖 Generated with Claude Code