Files
mtusi2026lab5/migrations/003_attachments.sql
T

28 lines
1.1 KiB
SQL

-- Метаданные вложения. Байты лежат в приватном S3-bucket, не в этой таблице.
-- object_key назначает сервер. filename только для отображения.
-- Связь сообщения и вложения — message_attachments, порядок задаёт position.
CREATE TABLE 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 integer NOT NULL,
sha256 text NOT NULL,
object_key text NOT NULL,
width integer NOT NULL,
height integer NOT NULL,
created_at timestamptz NOT NULL,
CONSTRAINT attachments_object_key_unique UNIQUE (object_key)
);
CREATE INDEX attachments_chat_idx ON attachments (chat_id, created_at, id);
CREATE TABLE message_attachments (
message_id uuid NOT NULL REFERENCES messages (id),
attachment_id uuid NOT NULL REFERENCES attachments (id),
position integer NOT NULL,
PRIMARY KEY (message_id, attachment_id)
);