Files
mtusi2026lab3/migrations/003_idempotency_keys.sql
T
2026-10-13 19:11:11 +03:00

19 lines
886 B
SQL

-- Lab3: shared idempotency of POST /chats/{chat_id}/messages.
-- Scope: user + method + chat path + key. The key row and the message are
-- written in one transaction, so either both exist or neither does.
-- Expand-only change: the running lab2/lab3 API keeps working with it.
CREATE TABLE IF NOT EXISTS idempotency_keys (
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
method VARCHAR(8) NOT NULL,
key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, chat_id, method, key)
);
-- For a periodic cleanup of keys older than IDEMPOTENCY_TTL_SECONDS.
CREATE INDEX IF NOT EXISTS idempotency_keys_created_idx
ON idempotency_keys (created_at);