ALTER TABLE messages DROP CONSTRAINT messages_text_check; ALTER TABLE messages ADD CONSTRAINT messages_text_limit CHECK (length(text)<=4000); CREATE TABLE attachments ( id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id), owner_id uuid NOT NULL REFERENCES users(id), object_key text NOT NULL UNIQUE, filename text NOT NULL, content_type text NOT NULL, size_bytes integer NOT NULL, sha256 char(64) NOT NULL, state text NOT NULL CHECK(state IN ('ready','missing')), created_at timestamptz NOT NULL DEFAULT clock_timestamp(), updated_at timestamptz NOT NULL DEFAULT clock_timestamp(), FOREIGN KEY(chat_id,owner_id) REFERENCES chat_members(chat_id,user_id) ); CREATE TABLE message_attachments ( message_id uuid NOT NULL REFERENCES messages(id), attachment_id uuid NOT NULL REFERENCES attachments(id), position integer NOT NULL, PRIMARY KEY(message_id,attachment_id), UNIQUE(message_id,position) ); -- Старые сохранённые ключи теперь сравнивают полное нормализованное тело. UPDATE message_requests SET body=json_build_object('text',body,'attachment_ids',json_build_array())::text;