Files
mtusi2026lab2/migrations/001_init.sql
T
vika f9b2fd11e0 Перенести мессенджер в PostgreSQL и сессии в Redis.
Подтверждённые сообщения и действующие сессии переживают перезапуск, история читается по индексу, копия восстанавливается в новый том.
2026-10-23 12:50:45 +03:00

63 lines
2.9 KiB
SQL

-- Tables, constraints and indexes. Queries live in src/db.py and follow this layout.
--
-- users one row per account; username is unique; password_hash is Argon2id, never returned
-- chats one row per chat
-- chat_members membership; primary key (chat_id, user_id); member_ord keeps the request order
-- a chat and its members are inserted in one transaction
-- messages history of one chat; page query is
-- WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
-- ORDER BY created_at, id
-- LIMIT $4
--
-- Transaction boundaries (see src/db.py):
-- register: single INSERT; unique username aborts that insert
-- create chat: INSERT chats + INSERT chat_members, one transaction; any failure rolls both back
-- send message: membership check + INSERT, one transaction, under an advisory lock with the cap
-- reads: one transaction, no write
-- Sessions are not stored here. Redis is the only session store (see compose.yaml).
-- Re-running these statements does not insert rows. The runner also records schema_migrations.
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,
CONSTRAINT users_username_unique 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),
CONSTRAINT users_password_hash_present CHECK (char_length(password_hash) > 0)
);
CREATE TABLE IF NOT EXISTS chats (
id uuid PRIMARY KEY,
title text NOT NULL,
created_at timestamptz NOT NULL,
CONSTRAINT chats_title_len 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),
member_ord integer NOT NULL,
PRIMARY KEY (chat_id, user_id),
CONSTRAINT chat_members_ord_unique UNIQUE (chat_id, member_ord),
CONSTRAINT chat_members_ord_nonneg CHECK (member_ord >= 0)
);
-- List "chats of this user" filters by user_id. The primary key leads with chat_id, so it does not serve that lookup.
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (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),
body text NOT NULL,
created_at timestamptz NOT NULL,
CONSTRAINT messages_body_len CHECK (char_length(body) BETWEEN 1 AND 4000)
);
-- Matches the history query above: the scan stays inside one chat and already has the page order,
-- so LIMIT stops after one page instead of sorting the whole chat.
CREATE INDEX IF NOT EXISTS messages_chat_history_idx ON messages (chat_id, created_at, id);