forked from mtusi2026/mtusi2026lab3
41 lines
1.4 KiB
SQL
41 lines
1.4 KiB
SQL
-- Lab2: начальная схема. Источник истины — PostgreSQL.
|
|
CREATE TABLE users (
|
|
id uuid PRIMARY KEY,
|
|
username text NOT NULL UNIQUE,
|
|
display_name text NOT NULL,
|
|
password_hash text NOT NULL,
|
|
created_at timestamptz NOT NULL
|
|
);
|
|
|
|
CREATE TABLE chats (
|
|
id uuid PRIMARY KEY,
|
|
title text NOT NULL,
|
|
created_at timestamptz NOT NULL
|
|
);
|
|
|
|
CREATE TABLE chat_members (
|
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
|
user_id uuid NOT NULL REFERENCES users(id),
|
|
position integer NOT NULL, -- порядок участников как в запросе
|
|
PRIMARY KEY (chat_id, user_id)
|
|
);
|
|
CREATE INDEX chat_members_user_idx ON chat_members (user_id);
|
|
|
|
CREATE TABLE messages (
|
|
id uuid PRIMARY KEY,
|
|
chat_id uuid NOT NULL REFERENCES chats(id),
|
|
sender_id uuid NOT NULL REFERENCES users(id),
|
|
text text NOT NULL,
|
|
created_at timestamptz NOT NULL
|
|
);
|
|
-- Индекс под keyset-пагинацию истории: (created_at, id) внутри чата.
|
|
CREATE INDEX messages_chat_order_idx ON messages (chat_id, created_at, id);
|
|
|
|
CREATE TABLE sessions (
|
|
token_hash bytea PRIMARY KEY, -- SHA-256 от токена; сам токен не хранится
|
|
user_id uuid NOT NULL REFERENCES users(id),
|
|
created_at timestamptz NOT NULL,
|
|
expires_at timestamptz NOT NULL,
|
|
revoked_at timestamptz
|
|
);
|