-
Notifications
You must be signed in to change notification settings - Fork 24
fix: deduplicate Stripe webhook credits via ledger reference index #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -195,6 +195,7 @@ func (s *PostgresStore) migrate(ctx context.Context) error { | |
| created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() | ||
| )`, | ||
| `CREATE INDEX IF NOT EXISTS idx_ledger_account ON ledger_entries(account_id, created_at DESC)`, | ||
| `CREATE UNIQUE INDEX IF NOT EXISTS idx_ledger_reference ON ledger_entries(account_id, entry_type, reference) WHERE reference <> ''`, | ||
|
|
||
| // Referral system tables | ||
| `CREATE TABLE IF NOT EXISTS referrers ( | ||
|
|
@@ -837,7 +838,29 @@ func nullableCreatedAt(ts time.Time) any { | |
| return ts | ||
| } | ||
|
|
||
| // ErrAlreadyCredited is returned by creditTx when the (account_id, entry_type, | ||
| // reference) tuple already exists, indicating an idempotent duplicate. | ||
| var ErrAlreadyCredited = errors.New("store: credit already applied for this reference") | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 The Store contract now differs between implementations: |
||
| func creditTx(ctx context.Context, tx pgx.Tx, accountID string, amountMicroUSD int64, entryType LedgerEntryType, reference string, createdAt time.Time) error { | ||
| // Idempotency guard: check before touching balances so a duplicate returns | ||
| // ErrAlreadyCredited without any side-effects. The index is scoped to | ||
| // (account_id, entry_type, reference) so reusing a reference string across | ||
| // different accounts (e.g. "reservation_refund") is safe. | ||
| if reference != "" { | ||
| var exists bool | ||
| err := tx.QueryRow(ctx, | ||
| `SELECT EXISTS(SELECT 1 FROM ledger_entries WHERE account_id = $1 AND entry_type = $2 AND reference = $3)`, | ||
| accountID, string(entryType), reference, | ||
| ).Scan(&exists) | ||
| if err != nil { | ||
| return fmt.Errorf("store: check ledger reference: %w", err) | ||
| } | ||
| if exists { | ||
| return ErrAlreadyCredited | ||
| } | ||
| } | ||
|
|
||
| _, err := tx.Exec(ctx, | ||
| `INSERT INTO balances (account_id, balance_micro_usd, updated_at) | ||
| VALUES ($1, $2, NOW()) | ||
|
|
@@ -858,6 +881,10 @@ func creditTx(ctx context.Context, tx pgx.Tx, accountID string, amountMicroUSD i | |
| return fmt.Errorf("store: read balance: %w", err) | ||
| } | ||
|
|
||
| // No ON CONFLICT here: the pre-check above is the idempotency gate. | ||
| // A race that slips past the pre-check will hit the unique index and | ||
| // return a constraint error, causing the caller's transaction to roll back | ||
| // (including the balance update), so balances stay consistent. | ||
| _, err = tx.Exec(ctx, | ||
| `INSERT INTO ledger_entries (account_id, entry_type, amount_micro_usd, balance_after, reference, created_at) | ||
| VALUES ($1, $2, $3, $4, $5, COALESCE($6, NOW()))`, | ||
|
|
@@ -916,6 +943,9 @@ func (s *PostgresStore) Credit(accountID string, amountMicroUSD int64, entryType | |
| defer tx.Rollback(ctx) | ||
|
|
||
| if err := creditTx(ctx, tx, accountID, amountMicroUSD, entryType, reference, time.Time{}); err != nil { | ||
| if errors.Is(err, ErrAlreadyCredited) { | ||
| return nil | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 This makes all repeated |
||
| } | ||
| return err | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 This startup migration will fail if production already has duplicate non-empty
(account_id, entry_type, reference)rows. Given existing reused references likereservation_refund/ defaultadmin_credit, that seems plausible. Add a preflight/dedupe migration or narrow the unique index predicate to the Stripe deposit reference shape so deploys do not fail closed on existing ledger history.