forked from mtusi2026/mtusi2026lab4
Перенесён стенд lab3 (API, Redis-сессии, идемпотентность, HAProxy, реплики API). Добавлены приватный bucket SeaweedFS с отдельным ключом API, загрузка PNG/JPEG с лимитами и полной проверкой, компенсация и reconciler, плавная остановка API, тесты и evidence.
56 lines
2.4 KiB
SQL
56 lines
2.4 KiB
SQL
-- 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);
|