# Что доказывает: миграции Attachment (README, «Что должно быть в PR»):
#   ревизия 0003 создаёт attachments и message_attachments, миграции
#   выполняются однократно сервисом migrate до реплик, повтор — no-op,
#   описание схемы (src/storage/tables.py) совпадает с базой. С нуля все
#   ревизии применяет каждый make test-isolated и make failure-checks
#   (пустая база отдельного стенда) — tests/test-isolated.txt.
# Дата: 2026-10-23T16:46Z (UTC)
# Окружение: macOS 26.6.2, Apple M3 Pro; Docker 29.9.2, Compose 5.6.1;
#   uv Python 3.13.15, alembic 1.20.0.
# Стенд: рабочий (`make up`, проект vladimirceva-lab4), PostgreSQL 18.6 на
#   127.0.0.1:5533 (порт из .env).
# Код: src_sha256 bc10d566e2f63173541445a2f6273b967447e13e5dd7f1fb76900934e5692cbe.
# Как повторить: make up; docker compose run --rm migrate; переменные DB_*
#   из .env и uv run alembic check / alembic history (migrations/AGENTS.md)

$ docker compose run --rm migrate
2026-10-23T16:46:30.131Z INFO    src.migrations [rid=-] migrations started db=postgres:5432/messenger
2026-10-23T16:46:30.149Z INFO    alembic.runtime.migration [rid=-] Context impl PostgresqlImpl.
2026-10-23T16:46:30.149Z INFO    alembic.runtime.migration [rid=-] Will assume transactional DDL.
2026-10-23T16:46:30.156Z INFO    src.migrations [rid=-] migrations finished db=messenger
exit=0

$ uv run alembic check
2026-10-23T16:46:32.220Z INFO    src.migrations [rid=-] migrations finished db=messenger
No new upgrade operations detected.

$ uv run alembic history
0002 -> 0003 (head), attachments and message attachments
0001 -> 0002, idempotency keys for sending messages
<base> -> 0001, messenger schema: users, sessions, chats, chat members, messages

$ psql -c "SELECT version_num FROM alembic_version"
 version_num
-------------
 0003
(1 row)

$ psql -c "\d attachments"
                        Table "public.attachments"
    Column    |           Type           | Collation | Nullable | Default
--------------+--------------------------+-----------+----------+---------
 id           | uuid                     |           | not null |
 chat_id      | uuid                     |           | not null |
 owner_id     | uuid                     |           | not null |
 filename     | text                     |           | not null |
 content_type | text                     |           | not null |
 size_bytes   | integer                  |           | not null |
 sha256       | bytea                    |           | not null |
 object_key   | text                     |           | not null |
 status       | text                     |           | not null |
 created_at   | timestamp with time zone |           | not null |
 updated_at   | timestamp with time zone |           | not null |
 lost_at      | timestamp with time zone |           |          |
Indexes:
    "pk_attachments" PRIMARY KEY, btree (id)
    "ix_attachments_pending_created_at" btree (created_at, id) WHERE status = 'pending'::text
    "uq_attachments_object_key" UNIQUE CONSTRAINT, btree (object_key)
Check constraints:
    "ck_attachments_content_type" CHECK (content_type = ANY (ARRAY['image/png'::text, 'image/jpeg'::text]))
    "ck_attachments_filename_chars" CHECK (filename !~ '[/\\\u0001-\u001f\u007f-\u009f]'::text)
    "ck_attachments_filename_length" CHECK (char_length(filename) >= 1 AND char_length(filename) <= 255)
    "ck_attachments_object_key_length" CHECK (octet_length(object_key) >= 1 AND octet_length(object_key) <= 1024)
    "ck_attachments_sha256_length" CHECK (octet_length(sha256) = 32)
    "ck_attachments_size_bytes" CHECK (size_bytes >= 1 AND size_bytes <= 10485760)
    "ck_attachments_status" CHECK (status = ANY (ARRAY['pending'::text, 'ready'::text]))
    "ck_attachments_updated_after_created" CHECK (updated_at >= created_at)
Foreign-key constraints:
    "fk_attachments_chat_id_owner_id_chat_members" FOREIGN KEY (chat_id, owner_id) REFERENCES chat_members(chat_id, user_id)
Referenced by:
    TABLE "message_attachments" CONSTRAINT "fk_message_attachments_attachment_id_attachments" FOREIGN KEY (attachment_id) REFERENCES attachments(id)

$ psql -c "\d message_attachments"
            Table "public.message_attachments"
    Column     |   Type   | Collation | Nullable | Default
---------------+----------+-----------+----------+---------
 message_id    | uuid     |           | not null |
 position      | smallint |           | not null |
 attachment_id | uuid     |           | not null |
Indexes:
    "pk_message_attachments" PRIMARY KEY, btree (message_id, "position")
    "ix_message_attachments_attachment_id" btree (attachment_id)
    "uq_message_attachments_message_id_attachment_id" UNIQUE CONSTRAINT, btree (message_id, attachment_id)
Check constraints:
    "ck_message_attachments_position_range" CHECK ("position" >= 0 AND "position" <= 9)
Foreign-key constraints:
    "fk_message_attachments_attachment_id_attachments" FOREIGN KEY (attachment_id) REFERENCES attachments(id)
    "fk_message_attachments_message_id_messages" FOREIGN KEY (message_id) REFERENCES messages(id)

# Итог: база на ревизии 0003 (head); повторный запуск migrate — no-op
# (новых ревизий не применено), alembic check расхождений схемы и
# tables.py не видит. Байтов в схеме нет: оригинал — объект S3 по
# object_key (уникален), в строке — размер, SHA-256 и статус. Членство
# владельца страхует составной внешний ключ на chat_members; связь с
# сообщением — без каскада, позиции 0–9 (не больше 10 вложений). Частичный
# индекс pending — для выборки кандидатов уборщика.
