| 1 | -- Freeze an invoice's customer and lines before contacting Stripe. Concurrent |
| 2 | -- callers and retries use the same attempt even when more usage arrives. |
| 3 | CREATE TABLE invoice_attempts ( |
| 4 | id TEXT PRIMARY KEY, |
| 5 | workspace TEXT NOT NULL UNIQUE, |
| 6 | payload TEXT NOT NULL, |
| 7 | created_ms INTEGER NOT NULL, |
| 8 | invoice_id TEXT |
| 9 | ); |
| 10 | |
| 11 | -- Continue bounded replay across ticks instead of repeatedly reading the |
| 12 | -- newest 1,000 events and starving an older backlog. |
| 13 | ALTER TABLE stripe_sync ADD COLUMN replay_after TEXT; |
| 14 | ALTER TABLE stripe_sync ADD COLUMN replay_since INTEGER; |
| 15 | ALTER TABLE stripe_sync ADD COLUMN replay_until INTEGER; |
| 16 | ALTER TABLE stripe_sync ADD COLUMN replay_failed INTEGER; |
| 17 | |
| 18 | -- Keep the card that funded a trial even if the workspace changes cards. |
| 19 | ALTER TABLE trial_grants ADD COLUMN fingerprint TEXT; |
| 20 | UPDATE trial_grants SET fingerprint = ( |
| 21 | SELECT c.fingerprint FROM card_checks c WHERE c.workspace = trial_grants.workspace |
| 22 | AND c.fingerprint IS NOT NULL AND c.workspace = ( |
| 23 | SELECT MIN(other.workspace) FROM card_checks other JOIN trial_grants g ON g.workspace = other.workspace |
| 24 | WHERE other.fingerprint = c.fingerprint |
| 25 | ) |
| 26 | ); |
| 27 | CREATE UNIQUE INDEX trial_grants_by_fingerprint ON trial_grants(fingerprint) WHERE fingerprint IS NOT NULL; |