ЛР 5: RabbitMQ, асинхронные миниатюры и отказы — на 3

This commit is contained in:
2026-11-20 20:09:59 +03:00
parent 276dbdad8a
commit 3f8d7fb56b
50 changed files with 3388 additions and 39 deletions
+36
View File
@@ -0,0 +1,36 @@
CREATE TABLE users (
id uuid PRIMARY KEY,
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 AND btrim(display_name) <> ''),
password_hash text NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chats (
id uuid PRIMARY KEY,
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100 AND btrim(title) <> ''),
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
);
CREATE TABLE chat_members (
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
user_id uuid NOT NULL REFERENCES users(id),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX chat_members_user ON chat_members(user_id, chat_id);
CREATE INDEX chats_order ON chats(created_at, id);
CREATE TABLE messages (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
sender_id uuid NOT NULL,
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000 AND btrim(text) <> ''),
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
);
CREATE INDEX messages_history ON messages(chat_id, created_at, id);
CREATE TABLE sessions (
token_hash bytea PRIMARY KEY CHECK (octet_length(token_hash) = 32),
user_id uuid NOT NULL REFERENCES users(id),
expires_at timestamptz NOT NULL,
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
CHECK (expires_at > created_at)
);
CREATE INDEX sessions_expiry ON sessions(expires_at);
+9
View File
@@ -0,0 +1,9 @@
CREATE TABLE message_idempotency (
user_id uuid NOT NULL REFERENCES users(id),
method text NOT NULL,
chat_id uuid NOT NULL REFERENCES chats(id),
key text NOT NULL CHECK (key COLLATE "C" ~ '^[A-Za-z0-9._:-]{1,128}$'),
message_id uuid NOT NULL REFERENCES messages(id) DEFERRABLE INITIALLY DEFERRED,
expires_at timestamptz NOT NULL,
PRIMARY KEY (user_id, method, chat_id, key)
);
+35
View File
@@ -0,0 +1,35 @@
-- Preserve existing message rows and stored idempotency keys.
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
ALTER TABLE messages ADD CHECK (char_length(text) <= 4000);
ALTER TABLE messages ADD UNIQUE (chat_id, id);
CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id),
owner_id uuid NOT NULL,
filename text NOT NULL CHECK (char_length(filename) BETWEEN 1 AND 255
AND strpos(filename, '/') = 0 AND strpos(filename, chr(92)) = 0
AND filename !~ '[[:cntrl:]]'),
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
sha256 text NOT NULL CHECK (sha256 COLLATE "C" ~ '^[a-f0-9]{64}$'),
object_key text NOT NULL UNIQUE,
status text NOT NULL DEFAULT 'ready' CHECK (status IN ('ready', 'failed')),
error_code text,
created_at timestamptz NOT NULL DEFAULT transaction_timestamp(),
updated_at timestamptz NOT NULL DEFAULT transaction_timestamp(),
CHECK ((status = 'ready') = (error_code IS NULL)),
FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id),
UNIQUE (chat_id, id)
);
CREATE TABLE message_attachments (
chat_id uuid NOT NULL,
message_id uuid NOT NULL,
attachment_id uuid NOT NULL,
position integer NOT NULL CHECK (position BETWEEN 1 AND 10),
PRIMARY KEY (message_id, position),
UNIQUE (message_id, attachment_id),
FOREIGN KEY (chat_id, message_id) REFERENCES messages(chat_id, id) ON DELETE CASCADE,
FOREIGN KEY (chat_id, attachment_id) REFERENCES attachments(chat_id, id)
);
+32
View File
@@ -0,0 +1,32 @@
-- Existing Lab 4 rows keep their original keys, metadata and message links.
ALTER TABLE attachments DROP CONSTRAINT attachments_status_check;
ALTER TABLE attachments DROP CONSTRAINT attachments_check;
ALTER TABLE attachments ALTER COLUMN status SET DEFAULT 'queued';
ALTER TABLE attachments ADD CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
ALTER TABLE attachments ADD CHECK ((status = 'failed') = (error_code IS NOT NULL));
CREATE TABLE attachment_jobs (
job_id uuid PRIMARY KEY,
attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id),
pipeline_version integer NOT NULL CHECK (pipeline_version = 1),
staging_key text NOT NULL UNIQUE,
created_at timestamptz NOT NULL,
attempt integer NOT NULL DEFAULT 1,
max_attempts integer NOT NULL CHECK (max_attempts BETWEEN 1 AND 20),
retry_at timestamptz NOT NULL DEFAULT now(),
last_error text,
CHECK (attempt BETWEEN 1 AND max_attempts)
);
CREATE TABLE attachment_variants (
attachment_id uuid NOT NULL REFERENCES attachments(id),
pipeline_version integer NOT NULL CHECK (pipeline_version = 1),
name text NOT NULL CHECK (name = 'thumbnail'),
content_type text NOT NULL CHECK (content_type = 'image/jpeg'),
size_bytes integer NOT NULL CHECK (size_bytes > 0),
sha256 text NOT NULL CHECK (sha256 COLLATE "C" ~ '^[a-f0-9]{64}$'),
width integer NOT NULL CHECK (width BETWEEN 1 AND 256),
height integer NOT NULL CHECK (height BETWEEN 1 AND 256),
object_key text NOT NULL UNIQUE,
PRIMARY KEY (attachment_id, pipeline_version, name)
);