lab4: S3 attachments (Silo), negative tests, cleanup-job, limited S3 credentials (grade 4)

This commit is contained in:
2026-10-21 19:39:26 +03:00
parent ec540a1a72
commit 652dfda832
35 changed files with 3557 additions and 18 deletions
@@ -0,0 +1,33 @@
from __future__ import annotations
from alembic import op
revision = "2803f3a0e579"
down_revision = "447472b8acec"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("""
CREATE TABLE idempotency_keys (
user_id UUID NOT NULL,
method TEXT NOT NULL,
path TEXT NOT NULL,
key TEXT NOT NULL,
request_hash TEXT NOT NULL,
response_status INTEGER NOT NULL,
response_body JSONB NOT NULL,
created_at TIMESTAMPTZ NOT NULL,
PRIMARY KEY (user_id, method, path, key)
)
""")
op.execute("""
CREATE INDEX idempotency_keys_created_at_idx
ON idempotency_keys (created_at)
""")
def downgrade() -> None:
op.execute("DROP INDEX IF EXISTS idempotency_keys_created_at_idx")
op.execute("DROP TABLE IF EXISTS idempotency_keys")
@@ -0,0 +1,83 @@
"""create initial schema
Revision ID: <оставь тот, что сгенерировал alembic>
Revises:
Create Date: <оставь>
"""
from __future__ import annotations
from alembic import op
# revision identifiers, used by Alembic.
revision = "447472b8acec"
down_revision = None
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("""
CREATE TABLE users (
id UUID PRIMARY KEY,
username TEXT NOT NULL UNIQUE,
display_name TEXT NOT NULL,
password_hash TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
)
""")
op.execute("""
CREATE TABLE chats (
id UUID PRIMARY KEY,
title TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
)
""")
op.execute("""
CREATE TABLE chat_members (
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
PRIMARY KEY (chat_id, user_id)
)
""")
op.execute("""
CREATE INDEX chat_members_user_idx ON chat_members (user_id)
""")
op.execute("""
CREATE TABLE messages (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
sender_id UUID NOT NULL REFERENCES users(id),
text TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL
)
""")
op.execute("""
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id)
""")
op.execute("""
CREATE TABLE sessions (
token_hash TEXT PRIMARY KEY,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
expires_at TIMESTAMPTZ NOT NULL,
created_at TIMESTAMPTZ NOT NULL
)
""")
op.execute("""
CREATE INDEX sessions_user_idx ON sessions (user_id)
""")
def downgrade() -> None:
op.execute("DROP TABLE IF EXISTS sessions")
op.execute("DROP TABLE IF EXISTS messages")
op.execute("DROP INDEX IF EXISTS chat_members_user_idx")
op.execute("DROP TABLE IF EXISTS chat_members")
op.execute("DROP TABLE IF EXISTS chats")
op.execute("DROP TABLE IF EXISTS users")
@@ -0,0 +1,57 @@
"""add attachments table
Revision ID: c3f18af4ec56
Revises: 2803f3a0e579
Create Date: <оставь>
"""
from __future__ import annotations
from alembic import op
revision = "c3f18af4ec56"
down_revision = "2803f3a0e579"
branch_labels = None
depends_on = None
def upgrade() -> None:
op.execute("""
CREATE TABLE attachments (
id UUID PRIMARY KEY,
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
owner_id UUID NOT NULL REFERENCES users(id),
filename TEXT NOT NULL,
content_type TEXT NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
size_bytes INTEGER NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
s3_key TEXT NOT NULL UNIQUE,
status TEXT NOT NULL DEFAULT 'ready',
error_code TEXT,
created_at TIMESTAMPTZ NOT NULL,
updated_at TIMESTAMPTZ NOT NULL
)
""")
op.execute("""
CREATE INDEX attachments_chat_idx ON attachments (chat_id)
""")
op.execute("""
CREATE INDEX attachments_owner_idx ON attachments (owner_id)
""")
op.execute("""
CREATE TABLE message_attachments (
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
position INTEGER NOT NULL,
PRIMARY KEY (message_id, attachment_id)
)
""")
op.execute("""
CREATE INDEX message_attachments_attachment_idx ON message_attachments (attachment_id)
""")
def downgrade() -> None:
op.execute("DROP TABLE IF EXISTS message_attachments")
op.execute("DROP INDEX IF EXISTS attachments_owner_idx")
op.execute("DROP INDEX IF EXISTS attachments_chat_idx")
op.execute("DROP TABLE IF EXISTS attachments")