Реализовать лабораторную 7

This commit is contained in:
Krep4334
2026-12-01 17:47:59 +03:00
parent 20afc2c790
commit 9ffa60daef
89 changed files with 5054 additions and 69 deletions
+43
View File
@@ -0,0 +1,43 @@
CREATE TABLE users (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
username text NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
password_hash bytea NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE members (
chat_id uuid REFERENCES chats(id),
user_id uuid REFERENCES users(id),
position integer NOT NULL,
PRIMARY KEY (chat_id, user_id),
UNIQUE (chat_id, position)
);
CREATE INDEX members_user_idx ON members (user_id, chat_id);
CREATE INDEX chats_order_idx ON chats (created_at, id);
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 CHECK (char_length(text) BETWEEN 1 AND 4000),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES members (chat_id, user_id)
);
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY,
user_id uuid NOT NULL REFERENCES users(id),
expires_at timestamptz NOT NULL
);
CREATE INDEX sessions_expiry_idx ON sessions (expires_at);
+11
View File
@@ -0,0 +1,11 @@
CREATE TABLE message_requests (
actor_id uuid NOT NULL REFERENCES users(id),
chat_id uuid NOT NULL REFERENCES chats(id),
key text NOT NULL,
fingerprint bytea NOT NULL,
message_id uuid NOT NULL REFERENCES messages(id),
expires_at timestamptz NOT NULL,
PRIMARY KEY (actor_id, chat_id, key)
);
CREATE INDEX message_requests_expiry_idx ON message_requests (expires_at);
+18
View File
@@ -0,0 +1,18 @@
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL,
owner_id uuid NOT NULL,
filename text NOT NULL,
content_type text NOT NULL,
size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 text NOT NULL,
ready boolean NOT NULL DEFAULT false,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, owner_id) REFERENCES members (chat_id, user_id)
);
CREATE INDEX attachments_pending_idx ON attachments (created_at) WHERE NOT ready;
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
ALTER TABLE messages ADD CHECK (char_length(text) <= 4000 AND
cardinality(attachment_ids) <= 10 AND (char_length(text) > 0 OR cardinality(attachment_ids) > 0));
+19
View File
@@ -0,0 +1,19 @@
ALTER TABLE attachments ADD COLUMN status text NOT NULL DEFAULT 'receiving'
CHECK (status IN ('receiving','queued','processing','ready','failed'));
UPDATE attachments SET status='ready' WHERE ready;
ALTER TABLE attachments ADD COLUMN variants jsonb NOT NULL DEFAULT '[]';
ALTER TABLE attachments ADD COLUMN error_code text;
CREATE TABLE jobs (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
attempt integer NOT NULL DEFAULT 0,
next_attempt timestamptz NOT NULL DEFAULT now(),
published_at timestamptz,
lease_until timestamptz,
finished boolean NOT NULL DEFAULT false
);
CREATE TABLE job_replays (
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
job_id uuid NOT NULL REFERENCES jobs(id),
replayed_at timestamptz NOT NULL DEFAULT now()
);
+1
View File
@@ -0,0 +1 @@
ALTER TABLE jobs ADD COLUMN trace_context jsonb NOT NULL DEFAULT '{}';
+5
View File
@@ -0,0 +1,5 @@
CREATE TABLE login_limits (
key bytea PRIMARY KEY,
count integer NOT NULL,
window_start timestamptz NOT NULL
);