forked from mtusi2026/mtusi2026lab4
27 lines
1.4 KiB
SQL
27 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),
|
|
object_key text NOT NULL UNIQUE,
|
|
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
|
|
size_bytes integer NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
|
sha256 text NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
|
|
width integer NOT NULL CHECK (width > 0),
|
|
height integer NOT NULL CHECK (height > 0),
|
|
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(),
|
|
CHECK (width::bigint * height <= 40000000)
|
|
);
|
|
CREATE INDEX attachments_chat_idx ON attachments(chat_id, id);
|
|
CREATE INDEX attachments_pending_idx ON attachments(created_at) WHERE status = 'uploading';
|
|
|
|
ALTER TABLE messages ADD COLUMN attachment_ids uuid[] NOT NULL DEFAULT '{}';
|
|
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
|
ALTER TABLE messages ADD CONSTRAINT messages_content_check CHECK (
|
|
char_length(text) <= 4000 AND cardinality(attachment_ids) <= 10
|
|
AND (char_length(btrim(text, E' \t\r\n')) > 0 OR cardinality(attachment_ids) > 0)
|
|
);
|
|
ALTER TABLE message_requests ADD COLUMN request_attachment_ids uuid[] NOT NULL DEFAULT '{}';
|