Files
mtusi2026lab5/migrations/002_idempotency.sql
T

12 lines
566 B
SQL

-- Lab3: one atomic idempotency result per user, chat and key.
CREATE TABLE IF NOT EXISTS message_idempotency (
user_id UUID NOT NULL REFERENCES users(id),
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
idem_key VARCHAR(128) NOT NULL,
request_hash CHAR(64) NOT NULL,
message_id UUID NOT NULL UNIQUE REFERENCES messages(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (user_id, chat_id, idem_key)
);
CREATE INDEX IF NOT EXISTS message_idempotency_expiry ON message_idempotency(created_at);