| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
CREATE TABLE completion_effects ( |
| 16 |
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), |
| 17 |
|
| 18 |
|
| 19 |
transaction_id UUID REFERENCES transactions(id) ON DELETE CASCADE, |
| 20 |
tip_id UUID REFERENCES tips(id) ON DELETE CASCADE, |
| 21 |
subscription_id UUID REFERENCES subscriptions(id) ON DELETE CASCADE, |
| 22 |
|
| 23 |
|
| 24 |
kind VARCHAR(64) NOT NULL, |
| 25 |
|
| 26 |
|
| 27 |
payload JSONB NOT NULL DEFAULT '{}'::jsonb, |
| 28 |
|
| 29 |
|
| 30 |
status VARCHAR(16) NOT NULL DEFAULT 'pending', |
| 31 |
attempt INT NOT NULL DEFAULT 0, |
| 32 |
last_error TEXT, |
| 33 |
|
| 34 |
scheduled_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 35 |
completed_at TIMESTAMPTZ, |
| 36 |
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), |
| 37 |
|
| 38 |
CHECK ( |
| 39 |
(transaction_id IS NOT NULL)::int + |
| 40 |
(tip_id IS NOT NULL)::int + |
| 41 |
(subscription_id IS NOT NULL)::int = 1 |
| 42 |
) |
| 43 |
); |
| 44 |
|
| 45 |
CREATE INDEX idx_completion_effects_pending |
| 46 |
ON completion_effects (scheduled_at) |
| 47 |
WHERE status = 'pending'; |
| 48 |
|
| 49 |
CREATE INDEX idx_completion_effects_transaction |
| 50 |
ON completion_effects (transaction_id) |
| 51 |
WHERE transaction_id IS NOT NULL; |
| 52 |
|
| 53 |
CREATE INDEX idx_completion_effects_tip |
| 54 |
ON completion_effects (tip_id) |
| 55 |
WHERE tip_id IS NOT NULL; |
| 56 |
|
| 57 |
CREATE INDEX idx_completion_effects_subscription |
| 58 |
ON completion_effects (subscription_id) |
| 59 |
WHERE subscription_id IS NOT NULL; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
CREATE UNIQUE INDEX uq_completion_effects_transaction_kind |
| 65 |
ON completion_effects (transaction_id, kind) |
| 66 |
WHERE transaction_id IS NOT NULL; |
| 67 |
CREATE UNIQUE INDEX uq_completion_effects_tip_kind |
| 68 |
ON completion_effects (tip_id, kind) |
| 69 |
WHERE tip_id IS NOT NULL; |
| 70 |
CREATE UNIQUE INDEX uq_completion_effects_subscription_kind |
| 71 |
ON completion_effects (subscription_id, kind) |
| 72 |
WHERE subscription_id IS NOT NULL; |
| 73 |
|