Complete Lab 5 grade 4 async attachment pipeline

This commit is contained in:
2026-11-16 16:46:44 +03:00
parent 276dbdad8a
commit f9246b8844
52 changed files with 4023 additions and 50 deletions
+48
View File
@@ -0,0 +1,48 @@
-- Lab 4: S3 stores file bytes, while PostgreSQL owns metadata and chat relations.
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_check;
ALTER TABLE messages
ADD CONSTRAINT messages_text_length_ck CHECK (char_length(text) BETWEEN 0 AND 4000);
ALTER TABLE messages
ADD CONSTRAINT messages_id_chat_id_uq UNIQUE (id, chat_id);
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL,
owner_id UUID NOT NULL,
filename VARCHAR(255) NOT NULL,
content_type VARCHAR(32) NOT NULL,
size_bytes INTEGER NOT NULL,
sha256 CHAR(64) NOT NULL,
status VARCHAR(16) NOT NULL DEFAULT 'ready',
variants JSONB NOT NULL DEFAULT '[]'::jsonb,
error_code VARCHAR(64),
object_key TEXT NOT NULL UNIQUE,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT attachments_owner_is_chat_member_fk
FOREIGN KEY (chat_id, owner_id)
REFERENCES chat_members(chat_id, user_id) ON DELETE CASCADE,
CONSTRAINT attachments_id_chat_id_uq UNIQUE (id, chat_id),
CONSTRAINT attachments_filename_length_ck CHECK (char_length(filename) BETWEEN 1 AND 255),
CONSTRAINT attachments_content_type_ck CHECK (content_type IN ('image/png', 'image/jpeg')),
CONSTRAINT attachments_size_bytes_ck CHECK (size_bytes BETWEEN 1 AND 10485760),
CONSTRAINT attachments_sha256_ck CHECK (sha256 ~ '^[a-f0-9]{64}$'),
CONSTRAINT attachments_status_ck CHECK (status = 'ready')
);
CREATE INDEX attachments_chat_created_idx ON attachments(chat_id, created_at, id);
CREATE TABLE message_attachments (
message_id UUID NOT NULL,
attachment_id UUID NOT NULL,
chat_id UUID NOT NULL,
position SMALLINT NOT NULL CHECK (position BETWEEN 0 AND 9),
PRIMARY KEY (message_id, attachment_id),
CONSTRAINT message_attachments_message_chat_fk
FOREIGN KEY (message_id, chat_id)
REFERENCES messages(id, chat_id) ON DELETE CASCADE,
CONSTRAINT message_attachments_attachment_chat_fk
FOREIGN KEY (attachment_id, chat_id)
REFERENCES attachments(id, chat_id) ON DELETE CASCADE,
CONSTRAINT message_attachments_position_uq UNIQUE (message_id, position)
);
CREATE INDEX message_attachments_attachment_idx ON message_attachments(attachment_id);