ЛР 2: PostgreSQL, сессии в Valkey, кэш истории (трек 1)

This commit is contained in:
Нурлыгаянова Екатерина
2026-10-29 11:29:38 +03:00
parent 65003b6119
commit 1503b5db81
132 changed files with 15114 additions and 20 deletions
+22
View File
@@ -0,0 +1,22 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username text NOT NULL,
display_name text NOT NULL,
-- Строка password KDF (Argon2id/scrypt) вместе с солью и параметрами; никогда не отдаётся в API.
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT users_username_key UNIQUE (username),
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_display_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100
AND btrim(display_name) <> ''),
CONSTRAINT users_password_hash_not_empty CHECK (password_hash <> '')
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS users;
-- +goose StatementEnd
+16
View File
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE chats (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
title text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100
AND btrim(title) <> '')
);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS chats;
-- +goose StatementEnd
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
CONSTRAINT chat_members_pkey PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user_id_chat_id_idx ON chat_members (user_id, chat_id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS chat_members;
-- +goose StatementEnd
@@ -0,0 +1,25 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE messages (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
chat_id uuid NOT NULL,
sender_id uuid NOT NULL,
text text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
-- Отправитель обязан быть участником этого чата: FK на пару из chat_members.
CONSTRAINT messages_chat_member_fkey FOREIGN KEY (chat_id, sender_id)
REFERENCES chat_members (chat_id, user_id) ON DELETE RESTRICT,
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
);
-- История чата с keyset-пагинацией:
-- WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4
-- Индекс отдаёт страницу без полного scan и без сортировки.
CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS messages;
-- +goose StatementEnd