Skip to content

Make create_for_owner! idempotent and race-safe#2

Merged
rameerez merged 1 commit into
mainfrom
idempotent-wallet-creation
Mar 18, 2026
Merged

Make create_for_owner! idempotent and race-safe#2
rameerez merged 1 commit into
mainfrom
idempotent-wallet-creation

Conversation

@rameerez
Copy link
Copy Markdown
Owner

Summary

Makes Wallet.create_for_owner! idempotent and race-safe, and cleans up the gemspec for release.

Changes

Idempotent wallet creation

  • Returns existing wallet if one already exists for owner + asset_code
  • Handles race conditions with rescue for RecordNotUnique/RecordInvalid
  • Normalizes asset_code before lookup/create
  • Extracts initial_balance_credit_attributes for subclass overriding (used by usage_credits)
# Now safe to call multiple times
wallet = Wallets::Wallet.create_for_owner!(owner: user, asset_code: :coins, initial_balance: 100)
same_wallet = Wallets::Wallet.create_for_owner!(owner: user, asset_code: :coins, initial_balance: 200)

wallet.id == same_wallet.id  # => true
wallet.balance               # => 100 (initial balance only applied once)

Gemspec cleanup

  • Use git ls-files for cleaner file list
  • Exclude dev directories (.claude, .cursor, .github, test, etc.)
  • Add Rails version ceiling (< 9.0)

Test plan

  • 66 tests, 252 assertions, 0 failures
  • New test for idempotent creation behavior
  • Gem builds cleanly

🤖 Generated with Claude Code

- Return existing wallet if one already exists for owner + asset_code
- Handle race conditions with rescue for RecordNotUnique/RecordInvalid
- Normalize asset_code before lookup/create
- Extract initial_balance_credit_attributes for subclass overriding
- Add test for idempotent behavior

Gemspec cleanup:
- Use git ls-files for cleaner file list
- Exclude dev directories (.claude, .cursor, .github, test, etc.)
- Add Rails version ceiling (< 9.0)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@rameerez rameerez merged commit ac5d350 into main Mar 18, 2026
11 of 12 checks passed
@rameerez rameerez deleted the idempotent-wallet-creation branch March 18, 2026 02:44
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.

1 participant