# 07 схема БД и миграции (2026-11-13T10:48:49Z)
       version       |          applied_at           
---------------------+-------------------------------
 001_init.sql        | 2026-11-13 10:47:16.695785+00
 002_idempotency.sql | 2026-11-13 10:47:16.75375+00
(2 rows)

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

                     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.sessions"
   Column   |           Type           | Collation | Nullable | Default 
------------+--------------------------+-----------+----------+---------
 token_hash | bytea                    |           | not null | 
 user_id    | uuid                     |           | not null | 
 expires_at | timestamp with time zone |           | not null | 
 revoked_at | timestamp with time zone |           |          | 
Indexes:
    "sessions_pkey" PRIMARY KEY, btree (token_hash)
    "sessions_expires_idx" btree (expires_at)
Foreign-key constraints:
    "sessions_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) >= 1 AND 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)

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

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

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

