Files

26 lines
1.0 KiB
SQL

CREATE TABLE attachments (
id uuid PRIMARY KEY,
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id uuid NOT NULL REFERENCES users(id),
filename varchar(255) NOT NULL,
content_type varchar(32) NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
size_bytes bigint NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 char(64) NOT NULL,
status varchar(16) NOT NULL CHECK (status = 'ready'),
object_key text NOT NULL UNIQUE,
error_code varchar(64),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX attachments_chat_idx ON attachments(chat_id, created_at, id);
CREATE TABLE message_attachments (
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
PRIMARY KEY (message_id, attachment_id)
);
CREATE INDEX message_attachments_attachment_idx
ON message_attachments(attachment_id, message_id);