Skip to main content

max / makenotwork

1.3 KB · 25 lines History Blame Raw
1 -- Payment finalize idempotency (ultra-fuzz Payments A+, finding M-Pay1).
2 --
3 -- Webhook secondary effects (license-key mint, revenue splits, etc.) run after
4 -- the transaction is committed. On crash-recovery redelivery the finalizer
5 -- re-runs, so the side-effecting writes must be structurally safe to repeat.
6 -- These two indexes are the backstop that makes a double-mint / double-split
7 -- impossible even if application-level pre-checks are bypassed.
8 --
9 -- NOTE: both indexes are created NON-CONCURRENTLY (sqlx runs each migration
10 -- inside its own transaction, and CREATE INDEX CONCURRENTLY cannot run there).
11 -- Acceptable at current table sizes; revisit if license_keys / revenue_splits
12 -- grow large enough that the brief write lock matters.
13
14 -- One auto-minted license key per purchase. The partial predicate excludes the
15 -- manually-created keys (transaction_id IS NULL) that creators issue by hand,
16 -- so it constrains only purchase-minted keys.
17 CREATE UNIQUE INDEX IF NOT EXISTS license_keys_transaction_id_key
18 ON license_keys (transaction_id)
19 WHERE transaction_id IS NOT NULL;
20
21 -- One revenue split per recipient per transaction. Lets create_transaction_splits
22 -- use ON CONFLICT DO NOTHING so a finalize re-run is a no-op.
23 CREATE UNIQUE INDEX IF NOT EXISTS revenue_splits_tx_recipient_key
24 ON revenue_splits (transaction_id, recipient_id);
25