forked from mtusi2026/mtusi2026lab2
ЛР 2: PostgreSQL, Bearer-сессии и кэш сессий в Valkey (на 4)
- Перенесён минимум lab1; переход 1 → 2: password при регистрации, Basic только на POST /auth/sessions, Bearer вместо X-User-Id, logout. - PostgreSQL — источник истины: users, chats, chat_members, messages, sessions; миграции с PK/FK/UNIQUE/CHECK и индексами, атомарное создание чата, scrypt для паролей, SHA-256 токенов в БД. - Valkey — кэш проверенных сессий с TTL, tombstone при logout, обход кэша и окно недоверия при отказе. - Ограниченные пулы и таймауты PostgreSQL/Valkey, 503 при отказе БД. - compose.yaml с Docker secrets и именованным томом, restart hook, собственные тесты, EXPLAIN на 110 тыс. сообщений, замер пользы кэша, заполненный REPORT.md и evidence/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
# 2026-10-12T14:22:38Z
|
||||
--- 1. Миграции с нуля (пустой том, первый старт API):
|
||||
api-1 | 2026-10-12 14:21:13,412 INFO migration applied: 001_init.sql
|
||||
api-1 | 2026-10-12 14:21:13,412 INFO schema version: 001_init.sql
|
||||
|
||||
--- 2. Повторный запуск миграций не дублирует данные
|
||||
до:
|
||||
users | chats | messages | migrations
|
||||
-------+-------+----------+------------
|
||||
11 | 5 | 103 | 1
|
||||
(1 row)
|
||||
|
||||
$ docker compose run --rm api python /app/src/server.py --migrate-only (запуск 1)
|
||||
2026-10-12 14:22:39,348 INFO schema version: 001_init.sql
|
||||
2026-10-12 14:22:39,348 INFO migrations are up to date, exiting (--migrate-only)
|
||||
$ docker compose run --rm api python /app/src/server.py --migrate-only (запуск 2)
|
||||
2026-10-12 14:22:40,518 INFO schema version: 001_init.sql
|
||||
2026-10-12 14:22:40,518 INFO migrations are up to date, exiting (--migrate-only)
|
||||
после:
|
||||
users | chats | messages | migrations
|
||||
-------+-------+----------+------------
|
||||
11 | 5 | 103 | 1
|
||||
(1 row)
|
||||
|
||||
version | applied_at
|
||||
--------------+-------------------------------
|
||||
001_init.sql | 2026-10-12 14:21:13.401972+00
|
||||
(1 row)
|
||||
|
||||
--- 3. Схема: ключи, ограничения, индексы
|
||||
Table "public.users"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
---------------+--------------------------+-----------+----------+---------
|
||||
id | uuid | | not null |
|
||||
username | text | | not null |
|
||||
display_name | text | | not null |
|
||||
password_hash | text | | not null |
|
||||
created_at | timestamp with time zone | | not null |
|
||||
Indexes:
|
||||
"users_pkey" PRIMARY KEY, btree (id)
|
||||
"users_username_key" UNIQUE CONSTRAINT, btree (username)
|
||||
Check constraints:
|
||||
"users_display_name_len" CHECK (char_length(display_name) >= 1 AND char_length(display_name) <= 100)
|
||||
"users_username_format" CHECK (username ~ '^[a-z0-9_]{3,32}$'::text)
|
||||
Referenced by:
|
||||
TABLE "chat_members" CONSTRAINT "chat_members_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
TABLE "messages" CONSTRAINT "messages_sender_id_fkey" FOREIGN KEY (sender_id) REFERENCES users(id)
|
||||
TABLE "sessions" CONSTRAINT "sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
|
||||
Table "public.chat_members"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
----------+---------+-----------+----------+---------
|
||||
chat_id | uuid | | not null |
|
||||
user_id | uuid | | not null |
|
||||
position | integer | | not null |
|
||||
Indexes:
|
||||
"chat_members_pkey" PRIMARY KEY, btree (chat_id, user_id)
|
||||
"chat_members_user_idx" btree (user_id, chat_id)
|
||||
Check constraints:
|
||||
"chat_members_position_check" CHECK ("position" >= 0)
|
||||
Foreign-key constraints:
|
||||
"chat_members_chat_id_fkey" FOREIGN KEY (chat_id) REFERENCES chats(id) ON DELETE CASCADE
|
||||
"chat_members_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 |
|
||||
Indexes:
|
||||
"messages_pkey" PRIMARY KEY, btree (id)
|
||||
"messages_chat_order_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)
|
||||
|
||||
Table "public.sessions"
|
||||
Column | Type | Collation | Nullable | Default
|
||||
------------+--------------------------+-----------+----------+---------
|
||||
token_hash | bytea | | not null |
|
||||
user_id | uuid | | not null |
|
||||
created_at | timestamp with time zone | | 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)
|
||||
Check constraints:
|
||||
"sessions_check" CHECK (expires_at > created_at)
|
||||
Foreign-key constraints:
|
||||
"sessions_user_id_fkey" FOREIGN KEY (user_id) REFERENCES users(id)
|
||||
|
||||
--- 4. Пароли: только scrypt-хеш (показаны параметры), токены: только SHA-256 (32 байта)
|
||||
kdf_params | count
|
||||
-----------------------+-------
|
||||
scrypt N=2^15 r=8 p=1 | 11
|
||||
(1 row)
|
||||
|
||||
hash_bytes | sessions | revoked
|
||||
------------+----------+---------
|
||||
32 | 14 | 2
|
||||
(1 row)
|
||||
|
||||
Reference in New Issue
Block a user