- carry the lab 2 solution over and plan lab 3 (PLAN.md, OpenSpec change instance-id-idempotency) - rename the compose project to mtusi-lab3 and the image to :lab3 - add X-Instance-Id to every response and instance_id to every log record ([app] instance_id, hostname by default) - require Idempotency-Key on POST messages: keys table in Postgres, key and message in one transaction, replay returns the same 201, another body gets 409, TTL from [idempotency] ttl_seconds - send the key from student-tests, k6 scenarios and the skill driver; add idempotency, X-Instance-Id, key race and fault tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""added idempotency keys
|
|
|
|
Revision ID: 7652184b7016
|
|
Revises: 42bc3b9be38f
|
|
Create Date: 2026-10-08 16:54:06.091180
|
|
|
|
"""
|
|
|
|
from collections.abc import Sequence
|
|
|
|
import sqlalchemy as sa
|
|
from alembic import op
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = "7652184b7016"
|
|
down_revision: str | Sequence[str] | None = "42bc3b9be38f"
|
|
branch_labels: str | Sequence[str] | None = None
|
|
depends_on: str | Sequence[str] | None = None
|
|
|
|
|
|
def upgrade() -> None:
|
|
# только новая таблица: работающая версия API без ключей её не
|
|
# замечает (expand). CHECK записаны литералами, а не константами src
|
|
op.create_table(
|
|
"idempotency_keys",
|
|
sa.Column("user_id", sa.Uuid(), nullable=False),
|
|
sa.Column("chat_id", sa.Uuid(), nullable=False),
|
|
sa.Column("key", sa.Text(), nullable=False),
|
|
sa.Column("request_hash", sa.LargeBinary(), nullable=False),
|
|
sa.Column("message_id", sa.Uuid(), nullable=False),
|
|
sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
|
|
sa.CheckConstraint(
|
|
"key ~ '^[A-Za-z0-9._:-]+$'",
|
|
name=op.f("ck_idempotency_keys_key_format"),
|
|
),
|
|
sa.CheckConstraint(
|
|
"char_length(key) BETWEEN 1 AND 128",
|
|
name=op.f("ck_idempotency_keys_key_length"),
|
|
),
|
|
sa.CheckConstraint(
|
|
"octet_length(request_hash) = 32",
|
|
name=op.f("ck_idempotency_keys_request_hash_length"),
|
|
),
|
|
sa.ForeignKeyConstraint(
|
|
["chat_id", "user_id"],
|
|
["chat_members.chat_id", "chat_members.user_id"],
|
|
name=op.f("fk_idempotency_keys_chat_id_user_id_chat_members"),
|
|
),
|
|
# ключ вставляется раньше сообщения в той же транзакции: проверка
|
|
# ссылки — на commit
|
|
sa.ForeignKeyConstraint(
|
|
["message_id"],
|
|
["messages.id"],
|
|
name=op.f("fk_idempotency_keys_message_id_messages"),
|
|
initially="DEFERRED",
|
|
deferrable=True,
|
|
),
|
|
sa.PrimaryKeyConstraint(
|
|
"user_id",
|
|
"chat_id",
|
|
"key",
|
|
name=op.f("pk_idempotency_keys"),
|
|
),
|
|
)
|
|
op.create_index(
|
|
op.f("ix_idempotency_keys_created_at"),
|
|
"idempotency_keys",
|
|
["created_at"],
|
|
unique=False,
|
|
)
|
|
|
|
|
|
def downgrade() -> None:
|
|
op.drop_index(
|
|
op.f("ix_idempotency_keys_created_at"),
|
|
table_name="idempotency_keys",
|
|
)
|
|
op.drop_table("idempotency_keys")
|