fix(vault): atomic upsert for vault balance to prevent lost updates - #434
Open
portableDD wants to merge 2 commits into
Open
fix(vault): atomic upsert for vault balance to prevent lost updates#434portableDD wants to merge 2 commits into
portableDD wants to merge 2 commits into
Conversation
| vault = deposit_connector.create_vault( | ||
| user=mock_user, symbol="BTC", amount="25.00" | ||
| ) | ||
| assert Decimal(vault.amount) == Decimal("75.00") |
| symbol="ETH", | ||
| amount="50.00", | ||
| ) | ||
| assert Decimal(vault.amount) == Decimal("150.00") |
| vault = mock_connector.upsert_vault(sample_user.id, "ETH", "10.0") | ||
| assert vault is not None | ||
| assert vault.user_id == sample_user.id | ||
| assert vault.symbol == "ETH" |
| mock_connector.upsert_vault(sample_user.id, "ETH", "1.0") | ||
| initial += Decimal("1.0") | ||
| vault = mock_connector.get_vault("wallet_abc", "ETH") | ||
| assert vault is not None |
| initial += Decimal("1.0") | ||
| vault = mock_connector.get_vault("wallet_abc", "ETH") | ||
| assert vault is not None | ||
| assert Decimal(vault.amount) == initial |
| mock_connector.upsert_vault(sample_user.id, "ETH", amt) | ||
| expected += Decimal(amt) | ||
| vault = mock_connector.get_vault("wallet_abc", "ETH") | ||
| assert Decimal(vault.amount) == expected |
| def test_constraint_exists_in_model(self): | ||
| table_args = Vault.__table_args__ | ||
| constraint_names = [c.name for c in table_args if hasattr(c, "name")] | ||
| assert "uq_vault_user_symbol" in constraint_names |
|
|
||
| def test_get_vault_returns_none_for_missing(self, mock_connector): | ||
| result = mock_connector.get_vault("no_such_wallet", "ETH") | ||
| assert result is None |
|
|
||
| from decimal import Decimal | ||
| from unittest.mock import MagicMock | ||
| from unittest.mock import MagicMock, patch |
|
|
||
| import uuid | ||
| from decimal import Decimal | ||
| from unittest.mock import MagicMock, patch, PropertyMock |
portableDD
force-pushed
the
fix/vault-balance-atomic-upsert
branch
from
August 20, 2026 16:44
4fd374f to
42bfd64
Compare
- Add unique constraint on (user_id, symbol) for Vault model - Replace read-modify-write with PostgreSQL ON CONFLICT DO UPDATE - create_vault now delegates to upsert_vault (idempotent) - add_vault_balance uses atomic SQL increment - Alembic migration deduplicates existing rows and adds constraint - Tests for concurrent increment, upsert idempotency, unique constraint Closes Quantarq#421
portableDD
force-pushed
the
fix/vault-balance-atomic-upsert
branch
from
August 20, 2026 16:48
42bfd64 to
3bb1e41
Compare
|
|
||
| def test_creates_vault_when_none_exists(self, mock_connector, sample_user): | ||
| vault = mock_connector.upsert_vault(sample_user, "ETH", "10.0") | ||
| assert vault is not None |
| def test_creates_vault_when_none_exists(self, mock_connector, sample_user): | ||
| vault = mock_connector.upsert_vault(sample_user, "ETH", "10.0") | ||
| assert vault is not None | ||
| assert vault.user_id == sample_user |
The inherited async_client fixture from the outdated base causes RuntimeError: must be called from async context when running under trio. Restore the original sync TestClient approach that works correctly.
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.
Description
Replace the non-atomic read-modify-write vault balance update with a PostgreSQL atomic upsert using ON CONFLICT DO UPDATE. Add unique constraint on (user_id, symbol) to prevent duplicate vault rows.
Related Issue
Closes #421
Change Type
Testing Done
Screenshots (if UI changes)
None
Environment Variables
None
Checklist
make lintpasses (pylint on changed.pyfiles)make testpasses (pytest inquantara/web_app/tests/)Closes #421)