Files
mtusi2026lab4/src/internal/store/migrations/0002_idempotency.sql
T
2026-10-26 16:10:22 +03:00

14 lines
782 B
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- Lab 3: идемпотентность отправки сообщений.
-- Все операции идемпотентны (IF NOT EXISTS).
-- Область ключа: (chat_id, sender_id, idempotency_key).
-- Это пользователь + путь + ключ для фиксированного метода POST.
ALTER TABLE messages
ADD COLUMN IF NOT EXISTS idempotency_key text;
-- У старых сообщений ключа нет (NULL).
-- В Postgres NULL'ы в уникальном индексе считаются различающимися,
-- поэтому старый контент не нарушит уникальность.
CREATE UNIQUE INDEX IF NOT EXISTS messages_idempotency_uq
ON messages (chat_id, sender_id, idempotency_key);