Files
mtusi2026lab4/migrations/002_idempotency.sql
T
Чиняева f39ae06ef3 Реализовать лабораторную 4 на оценку 5
Добавить приватное S3-хранилище, вложения, presigned upload, reconciler и доказательства.
2026-11-07 01:37:21 +03:00

27 lines
1.2 KiB
SQL

-- Laboratory 3: shared idempotency for message creation.
-- Scope is (user_id, method, path, key); the stored result is the message id.
-- The row and the message are written in one transaction by the API.
CREATE TABLE IF NOT EXISTS idempotency_keys (
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
method text NOT NULL,
path text NOT NULL,
key text NOT NULL,
request_hash text NOT NULL,
message_id uuid REFERENCES messages(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
expires_at timestamptz NOT NULL,
PRIMARY KEY (user_id, method, path, key),
CONSTRAINT idempotency_key_format CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
CONSTRAINT idempotency_method_post CHECK (method = 'POST')
);
-- Expired rows are removed lazily; this index keeps that cleanup bounded.
CREATE INDEX IF NOT EXISTS idempotency_expires_at_idx
ON idempotency_keys (expires_at);
-- The message id is unique per stored key, but the same message is never
-- shared across keys; a plain lookup by primary key is enough for replay.
CREATE INDEX IF NOT EXISTS idempotency_message_id_idx
ON idempotency_keys (message_id);