31 lines
1.4 KiB
SQL
31 lines
1.4 KiB
SQL
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 CHECK (char_length(filename) BETWEEN 1 AND 255),
|
|
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
|
|
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
|
sha256 bytea NOT NULL CHECK (octet_length(sha256) = 32),
|
|
object_key text NOT NULL UNIQUE,
|
|
status text NOT NULL CHECK (status IN ('uploading', 'ready')),
|
|
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
updated_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
|
);
|
|
|
|
CREATE INDEX attachments_chat_idx ON attachments (chat_id);
|
|
CREATE INDEX attachments_status_created_idx ON attachments (status, created_at);
|
|
|
|
CREATE TABLE message_attachments (
|
|
message_id uuid NOT NULL REFERENCES messages (id),
|
|
attachment_id uuid NOT NULL REFERENCES attachments (id),
|
|
position smallint NOT NULL CHECK (position >= 1),
|
|
PRIMARY KEY (message_id, attachment_id),
|
|
UNIQUE (message_id, position)
|
|
);
|
|
|
|
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id);
|
|
|
|
ALTER TABLE messages
|
|
DROP CONSTRAINT messages_text_check,
|
|
ADD CONSTRAINT messages_text_check CHECK (octet_length(text) <= 16000);
|