Skip to main content

max / makenotwork

666 B · 18 lines History Blame Raw
1 -- Idempotency key storage for safe POST retries.
2 -- Clients send an Idempotency-Key header; the server caches the response
3 -- body and status code for 24 hours so duplicate requests return the same result.
4
5 CREATE TABLE idempotency_keys (
6 key TEXT NOT NULL,
7 user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
8 method TEXT NOT NULL,
9 path TEXT NOT NULL,
10 status_code SMALLINT NOT NULL,
11 response_body TEXT NOT NULL,
12 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
13 PRIMARY KEY (key, user_id)
14 );
15
16 -- Index for cleanup job (expire keys older than 24h)
17 CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys (created_at);
18