-- Lab4: attachment metadata lives in PostgreSQL; the original bytes live in -- the private S3 bucket. The only public state in the mandatory lab4 contract -- is ready. pending exists solely as an internal, server-side upload intent: -- it is written before the S3 PUT and promoted in the same request after the -- PUT. A crash or a database failure between the two steps therefore leaves an -- accounted row, never an anonymous object. The reconciler removes stale -- pending rows and their objects after a documented grace period. 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 text NOT NULL, content_type text NOT NULL, size_bytes bigint NOT NULL, sha256 text NOT NULL, status text NOT NULL, error_code text, object_key text NOT NULL, created_at timestamptz NOT NULL, updated_at timestamptz NOT NULL, CONSTRAINT attachments_filename_length CHECK (char_length(filename) BETWEEN 1 AND 255), CONSTRAINT attachments_content_type CHECK (content_type IN ('image/png', 'image/jpeg')), CONSTRAINT attachments_size CHECK (size_bytes BETWEEN 1 AND 10485760), CONSTRAINT attachments_sha256 CHECK (sha256 ~ '^[a-f0-9]{64}$'), CONSTRAINT attachments_status CHECK (status IN ('pending', 'ready')), CONSTRAINT attachments_ready_error CHECK ((status = 'ready' AND error_code IS NULL) OR status = 'pending'), CONSTRAINT attachments_object_key CHECK (object_key LIKE 'attachments/%'), CONSTRAINT attachments_object_key_unique UNIQUE (object_key), -- Referenced by the composite foreign key of message_attachments so the -- database itself rejects a link to an attachment of another chat. CONSTRAINT attachments_id_chat_unique UNIQUE (id, chat_id) ); -- The only attachment query on the hot path is "attachment of this chat". CREATE INDEX attachments_chat_created_idx ON attachments (chat_id, created_at, id); -- The reconciler scans all rows by state and age. It never runs on the HTTP -- hot path. CREATE INDEX attachments_status_updated_idx ON attachments (status, updated_at); -- messages.id is already the primary key; this unique constraint is the -- target required by the composite foreign keys below, not a second identity. -- It must exist before the foreign key that references (id, chat_id). ALTER TABLE messages ADD CONSTRAINT messages_id_chat_unique UNIQUE (id, chat_id); -- A message and its attachment links share one transaction. The composite -- foreign keys make "file of another chat" impossible at the database level -- even if a future handler forgets the application check. CREATE TABLE message_attachments ( message_id uuid NOT NULL, attachment_id uuid NOT NULL, chat_id uuid NOT NULL, position integer NOT NULL, PRIMARY KEY (message_id, attachment_id), CONSTRAINT message_attachments_position_unique UNIQUE (message_id, position), CONSTRAINT message_attachments_position_nonnegative CHECK (position >= 0), 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) ); -- Reconciler lookup: "is this object key still referenced by any row?". CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id);