Реализовать lab4 на оценку 4: SeaweedFS, вложения и согласованность БД/S3.

Перенесён стенд lab3 (API, Redis-сессии, идемпотентность, HAProxy, реплики API). Добавлены приватный bucket SeaweedFS с отдельным ключом API, загрузка PNG/JPEG с лимитами и полной проверкой, компенсация и reconciler, плавная остановка API, тесты и evidence.
This commit is contained in:
Mikhail Verkovykh
2026-11-06 19:48:37 +03:00
parent ec540a1a72
commit 728e53cc55
94 changed files with 8821 additions and 43 deletions
+48
View File
@@ -0,0 +1,48 @@
-- Lab2 schema: users, chats, membership, messages.
-- Applied once via schema_migrations; safe to re-run application bootstrap.
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
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_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
CONSTRAINT users_username_unique UNIQUE (username)
);
CREATE TABLE IF NOT EXISTS chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
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),
PRIMARY KEY (chat_id, user_id)
);
CREATE INDEX IF NOT EXISTS idx_chat_members_user
ON chat_members (user_id, chat_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),
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
-- Keyset pagination by (created_at, id) within a chat; avoids full history scan.
CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id
ON messages (chat_id, created_at ASC, id ASC);
-- List chats for a user ordered by chat created_at/id.
CREATE INDEX IF NOT EXISTS idx_chats_created_id
ON chats (created_at ASC, id ASC);
+16
View File
@@ -0,0 +1,16 @@
-- Lab3: shared idempotency records for POST /messages (across API replicas).
CREATE TABLE IF NOT EXISTS idempotency_records (
user_id UUID NOT NULL,
method TEXT NOT NULL,
path TEXT NOT NULL,
idem_key TEXT NOT NULL,
request_hash TEXT NOT NULL,
response_status INT NOT NULL,
response_body JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (user_id, method, path, idem_key)
);
CREATE INDEX IF NOT EXISTS idx_idempotency_created
ON idempotency_records (created_at);
+55
View File
@@ -0,0 +1,55 @@
-- Lab4: attachment metadata (bytes live only in the private S3 bucket).
-- Additive migration: lab3 replicas keep working while it is applied.
CREATE TABLE IF NOT EXISTS attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id),
owner_id UUID NOT NULL REFERENCES users(id),
filename TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
sha256 TEXT NOT NULL,
width INT NOT NULL,
height INT NOT NULL,
-- Server-generated S3 key; never derived from filename, never exposed by the API.
object_key TEXT NOT NULL,
status TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL,
CONSTRAINT attachments_object_key_unique UNIQUE (object_key),
CONSTRAINT attachments_content_type_check CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size_check CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_check CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_pixels_check CHECK (width > 0 AND height > 0 AND width::bigint * height <= 40000000),
CONSTRAINT attachments_status_check CHECK (status IN ('ready'))
);
CREATE INDEX IF NOT EXISTS idx_attachments_chat
ON attachments (chat_id, created_at, id);
-- Ordered links message -> attachment (an attachment may be referenced by several messages).
CREATE TABLE IF NOT EXISTS message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id),
position SMALLINT NOT NULL,
PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_position_check CHECK (position BETWEEN 0 AND 9),
CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position)
);
CREATE INDEX IF NOT EXISTS idx_message_attachments_attachment
ON message_attachments (attachment_id);
-- Write-ahead journal of S3 objects whose metadata is not committed yet.
-- A row exists from just before the S3 PUT until the metadata commit (same
-- transaction deletes it) or until compensation/reconciliation removes the object.
CREATE TABLE IF NOT EXISTS upload_intents (
object_key TEXT PRIMARY KEY,
attachment_id UUID NOT NULL,
chat_id UUID NOT NULL,
instance_id TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_upload_intents_created
ON upload_intents (created_at);
+8
View File
@@ -0,0 +1,8 @@
-- Lab4: message text may contain U+0000, which PostgreSQL TEXT/JSONB cannot store.
-- Text is kept as exact UTF-8 bytes; the idempotent response snapshot as JSON
-- (textual JSON keeps the "\u0000" escape, JSONB rejects it).
ALTER TABLE messages
ALTER COLUMN text TYPE BYTEA USING convert_to(text, 'UTF8');
ALTER TABLE idempotency_records
ALTER COLUMN response_body TYPE JSON USING response_body::json;