# 07 схема БД и миграции (2026-11-14T11:02:32Z)
       version       |          applied_at           
---------------------+-------------------------------
 001_init.sql        | 2026-11-14 10:59:15.430991+00
 002_idempotency.sql | 2026-11-14 10:59:15.491662+00
 003_attachments.sql | 2026-11-14 10:59:15.505907+00
(3 rows)

              List of relations
 Schema |        Name         | Type  | Owner 
--------+---------------------+-------+-------
 public | attachments         | table | msg
 public | chat_members        | table | msg
 public | chats               | table | msg
 public | idempotency_keys    | table | msg
 public | message_attachments | table | msg
 public | messages            | table | msg
 public | schema_migrations   | table | msg
 public | sessions            | table | msg
 public | users               | table | msg
(9 rows)

                             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   | bigint                   |           | not null | 
 sha256       | text                     |           | not null | 
 width        | integer                  |           | not null | 
 height       | integer                  |           | not null | 
 object_key   | text                     |           | not null | 
 status       | text                     |           | not null | 'uploading'::text
 created_at   | timestamp with time zone |           | not null | now()
 updated_at   | timestamp with time zone |           | not null | now()
Indexes:
    "attachments_pkey" PRIMARY KEY, btree (id)
    "attachments_chat_idx" btree (chat_id, created_at)
    "attachments_object_key_key" UNIQUE CONSTRAINT, btree (object_key)
    "attachments_pending_idx" btree (created_at) WHERE status = 'uploading'::text
Check constraints:
    "attachments_content_type_check" CHECK (content_type = ANY (ARRAY['image/png'::text, 'image/jpeg'::text]))
    "attachments_filename_check" CHECK (char_length(filename) >= 1 AND char_length(filename) <= 255)
    "attachments_height_check" CHECK (height > 0)
    "attachments_sha256_check" CHECK (sha256 ~ '^[a-f0-9]{64}$'::text)
    "attachments_size_bytes_check" CHECK (size_bytes >= 1 AND size_bytes <= 10485760)
    "attachments_status_check" CHECK (status = ANY (ARRAY['uploading'::text, 'ready'::text]))
    "attachments_width_check" CHECK (width > 0)
Foreign-key constraints:
    "attachments_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
    "attachments_owner_id_fkey" FOREIGN KEY (owner_id) REFERENCES users(id)
Referenced by:
    TABLE "message_attachments" CONSTRAINT "message_attachments_attachment_id_fkey" FOREIGN KEY (attachment_id) REFERENCES attachments(id)

            Table "public.message_attachments"
    Column     |  Type   | Collation | Nullable | Default 
---------------+---------+-----------+----------+---------
 message_id    | uuid    |           | not null | 
 attachment_id | uuid    |           | not null | 
 position      | integer |           | not null | 
Indexes:
    "message_attachments_pkey" PRIMARY KEY, btree (message_id, attachment_id)
    "message_attachments_att_idx" btree (attachment_id)
Foreign-key constraints:
    "message_attachments_attachment_id_fkey" FOREIGN KEY (attachment_id) REFERENCES attachments(id)
    "message_attachments_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE

                     Table "public.idempotency_keys"
    Column    |           Type           | Collation | Nullable | Default 
--------------+--------------------------+-----------+----------+---------
 user_id      | uuid                     |           | not null | 
 method       | text                     |           | not null | 
 path         | text                     |           | not null | 
 key          | text                     |           | not null | 
 request_hash | bytea                    |           | not null | 
 status_code  | integer                  |           | not null | 
 response     | jsonb                    |           | not null | 
 created_at   | timestamp with time zone |           | not null | now()
 expires_at   | timestamp with time zone |           | not null | 
Indexes:
    "idempotency_keys_pkey" PRIMARY KEY, btree (user_id, method, path, key)
    "idempotency_expires_idx" btree (expires_at)
Check constraints:
    "idempotency_keys_key_check" CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'::text)
Foreign-key constraints:
    "idempotency_keys_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)

                             Table "public.messages"
   Column   |           Type           | Collation | Nullable |      Default      
------------+--------------------------+-----------+----------+-------------------
 id         | uuid                     |           | not null | 
 chat_id    | uuid                     |           | not null | 
 sender_id  | uuid                     |           | not null | 
 text       | text                     |           | not null | 
 created_at | timestamp with time zone |           | not null | clock_timestamp()
Indexes:
    "messages_pkey" PRIMARY KEY, btree (id)
    "messages_history_idx" btree (chat_id, created_at, id)
Check constraints:
    "messages_text_check" CHECK (char_length(text) <= 4000)
Foreign-key constraints:
    "messages_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
    "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id)
Referenced by:
    TABLE "message_attachments" CONSTRAINT "message_attachments_message_id_fkey" FOREIGN KEY (message_id) REFERENCES messages(id) ON DELETE CASCADE

--- статусы вложений в БД (после тестов не должно быть uploading)
 status | count 
--------+-------
 ready  |     6
(1 row)

--- пароли хранятся как Argon2id, в sessions только sha256 токена
 hash_prefix | count 
-------------+-------
 $argon2id$  |    26
(1 row)

 token_hash_bytes | count 
------------------+-------
               32 |    26
(1 row)

--- миграции выполняет отдельный one-shot сервис migrate (однократно, не репликами)
NAME                   IMAGE              COMMAND                  SERVICE   CREATED         STATUS                      PORTS
mtusi-lab4-migrate-1   mtusi-lab4-api:1   "python -m app.migra…"   migrate   3 minutes ago   Exited (0) 10 seconds ago   
migrations: ok, latest = 003_attachments.sql
migrations: ok, latest = 003_attachments.sql
migrations: ok, latest = 003_attachments.sql
--- API-реплики стартуют с RUN_MIGRATIONS=0
RUN_MIGRATIONS=0
IDEMPOTENCY_TTL_SECONDS=86400
SESSION_TTL_SECONDS=3600
--- повторный явный запуск миграции безопасен
migrations: ok, latest = 003_attachments.sql
       version       
---------------------
 001_init.sql
 002_idempotency.sql
 003_attachments.sql
(3 rows)

