forked from mtusi2026/mtusi2026lab2
выполнение лр2 на 4
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
-- Initial schema for lab2. Applied once by app/migrate.py inside one transaction.
|
||||
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL,
|
||||
@@ -18,7 +16,6 @@ CREATE TABLE chats (
|
||||
CONSTRAINT chats_title_len CHECK (char_length(title) BETWEEN 1 AND 100)
|
||||
);
|
||||
|
||||
-- position keeps member_ids in the order the creator sent them.
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id),
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
@@ -26,7 +23,6 @@ CREATE TABLE chat_members (
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
-- "Chats of a user": GET /chats starts from the caller's memberships.
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
@@ -35,18 +31,13 @@ CREATE TABLE messages (
|
||||
sender_id uuid NOT NULL,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
-- The sender must be a member of the chat, enforced by the database itself.
|
||||
CONSTRAINT messages_sender_member_fk FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id),
|
||||
CONSTRAINT messages_text_len CHECK (char_length(text) BETWEEN 1 AND 4000)
|
||||
);
|
||||
|
||||
-- Keyset pagination of history: WHERE chat_id = $1 AND (created_at, id) > ($2, $3)
|
||||
-- ORDER BY created_at, id LIMIT n. Justified by EXPLAIN in evidence/explain_history.txt.
|
||||
CREATE INDEX messages_chat_history_idx ON messages (chat_id, created_at, id);
|
||||
|
||||
-- Sessions: PostgreSQL is the source of truth; Valkey only caches lookups.
|
||||
-- token_hash = sha256(token); the raw token is never stored.
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
|
||||
Reference in New Issue
Block a user