-- Optional presigned upload. The signed URL dies quickly. The row stays pending -- until complete checks the object that actually landed in the bucket. -- Incomplete rows are aborted by the same grace sweeper as other uncommitted keys. CREATE TABLE direct_uploads ( 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 INTEGER NOT NULL, object_key TEXT NOT NULL UNIQUE, status TEXT NOT NULL, expires_at TIMESTAMPTZ NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), CONSTRAINT direct_uploads_filename_len CHECK (char_length(filename) BETWEEN 1 AND 255), CONSTRAINT direct_uploads_size CHECK (size_bytes BETWEEN 1 AND 10485760), CONSTRAINT direct_uploads_type CHECK (content_type IN ('image/png', 'image/jpeg')), CONSTRAINT direct_uploads_status CHECK (status IN ('pending', 'completed', 'aborted')) ); CREATE INDEX direct_uploads_pending_idx ON direct_uploads (expires_at) WHERE status = 'pending';