Реализовать лабораторную 3 на оценку 5 (трек 1)

This commit is contained in:
Чиняева
2026-11-07 00:57:10 +03:00
parent 0ab3a341aa
commit 2c4f0a5a61
96 changed files with 7784 additions and 45 deletions
+51
View File
@@ -0,0 +1,51 @@
-- Laboratory 2 initial schema.
-- PostgreSQL is the source of truth for users, chats, membership and messages.
CREATE TABLE IF NOT EXISTS users (
id uuid PRIMARY KEY,
username text NOT NULL,
display_name text NOT NULL,
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT users_username_key UNIQUE (username),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100)
);
CREATE TABLE IF NOT EXISTS chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100)
);
CREATE TABLE IF NOT EXISTS chat_members (
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
joined_at timestamptz NOT NULL DEFAULT clock_timestamp(),
PRIMARY KEY (chat_id, user_id)
);
CREATE TABLE IF NOT EXISTS messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
sender_id uuid NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
text text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000)
);
-- Reverse membership lookup: "my chats, newest last".
CREATE INDEX IF NOT EXISTS chat_members_user_chat_idx
ON chat_members (user_id, chat_id);
-- Chat page ordering and keyset pagination.
CREATE INDEX IF NOT EXISTS chats_created_at_id_idx
ON chats (created_at, id);
-- The only history access path:
-- WHERE chat_id = ? AND (created_at, id) > (?, ?)
-- ORDER BY created_at, id LIMIT ?
-- It supports the range scan without reading the whole chat history.
CREATE INDEX IF NOT EXISTS messages_chat_created_at_id_idx
ON messages (chat_id, created_at, id);
+29
View File
@@ -0,0 +1,29 @@
-- Laboratory 3: shared idempotency for POST messages.
--
-- Scope is exactly (user_id, method, path, key): the same key may be reused by
-- another user or another chat independently. The message row and the stored
-- response are written in one transaction, so a transient failure never leaves
-- a "completed" key without its side effect (and vice versa).
CREATE TABLE IF NOT EXISTS idempotency_keys (
id bigserial PRIMARY KEY,
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,
status_code integer,
response_body jsonb,
message_id uuid REFERENCES messages(id) ON DELETE CASCADE,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
expires_at timestamptz NOT NULL,
CONSTRAINT idempotency_keys_scope_key UNIQUE (user_id, method, path, key),
CONSTRAINT idempotency_keys_key_format
CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
CONSTRAINT idempotency_keys_method CHECK (method IN ('POST'))
);
-- Expired rows are removed opportunistically by the API. This index keeps the
-- cleanup and expiry checks cheap without scanning the whole table.
CREATE INDEX IF NOT EXISTS idempotency_keys_expires_at_idx
ON idempotency_keys (expires_at);