lab3: nginx, replicas, idempotency, tests

This commit is contained in:
2026-11-06 17:23:38 +03:00
commit 46c829b730
96 changed files with 206384 additions and 0 deletions
@@ -0,0 +1,30 @@
"""create tables
Revision ID: 08fe661df060
Revises: bc35c1b6b748
Create Date: 2026-10-31 11:52:26.342048
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '08fe661df060'
down_revision: Union[str, None] = 'bc35c1b6b748'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###
@@ -0,0 +1,67 @@
"""create tables
Revision ID: b24373e3b620
Revises:
Create Date: 2026-10-24 06:54:22.471827
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'b24373e3b620'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('chats',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('title', sa.String(length=100), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('users',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('username', sa.String(length=32), nullable=False),
sa.Column('display_name', sa.String(length=100), nullable=False),
sa.Column('password_hash', sa.String(length=255), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
op.create_table('chat_members',
sa.Column('chat_id', sa.UUID(), nullable=False),
sa.Column('user_id', sa.UUID(), nullable=False),
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('chat_id', 'user_id')
)
op.create_table('messages',
sa.Column('id', sa.UUID(), nullable=False),
sa.Column('chat_id', sa.UUID(), nullable=False),
sa.Column('sender_id', sa.UUID(), nullable=False),
sa.Column('text', sa.Text(), nullable=False),
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
sa.ForeignKeyConstraint(['chat_id'], ['chats.id'], ondelete='CASCADE'),
sa.ForeignKeyConstraint(['sender_id'], ['users.id'], ondelete='CASCADE'),
sa.PrimaryKeyConstraint('id')
)
op.create_index('ix_messages_chat_created_id', 'messages', ['chat_id', 'created_at', 'id'], unique=False)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('ix_messages_chat_created_id', table_name='messages')
op.drop_table('messages')
op.drop_table('chat_members')
op.drop_index(op.f('ix_users_username'), table_name='users')
op.drop_table('users')
op.drop_table('chats')
# ### end Alembic commands ###
@@ -0,0 +1,43 @@
"""seed explain test 10k
Revision ID: bc35c1b6b748
Revises: f9b85be2b710
Create Date: 2026-10-24 07:04:09.294404
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'bc35c1b6b748'
down_revision: Union[str, None] = 'f9b85be2b710'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute("""
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES ('11111111-1111-1111-1111-111111111111', 'explain_test', 'Test', 'hash', NOW())
ON CONFLICT DO NOTHING;
""")
op.execute("""
INSERT INTO chats (id, title, created_at)
VALUES ('22222222-2222-2222-2222-222222222222', 'Explain Chat', NOW())
ON CONFLICT DO NOTHING;
""")
op.execute("""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), '22222222-2222-2222-2222-222222222222', '11111111-1111-1111-1111-111111111111', 'Message ' || i, NOW() - (i || ' minutes')::interval
FROM generate_series(1, 10000) AS i;
""")
def downgrade() -> None:
op.execute("DELETE FROM messages WHERE chat_id = '22222222-2222-2222-2222-222222222222';")
op.execute("DELETE FROM chats WHERE id = '22222222-2222-2222-2222-222222222222';")
op.execute("DELETE FROM users WHERE id = '11111111-1111-1111-1111-111111111111';")
@@ -0,0 +1,69 @@
"""seed load test data
Revision ID: f9b85be2b710
Revises: b24373e3b620
Create Date: ...
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'f9b85be2b710'
down_revision: Union[str, None] = 'b24373e3b620'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.execute("""
INSERT INTO users (id, username, display_name, password_hash, created_at)
VALUES ('55555555-5555-4555-8555-555555555555', 'load_test', 'Load', 'hash', NOW())
ON CONFLICT DO NOTHING;
""")
op.execute("""
INSERT INTO chats (id, title, created_at) VALUES
('66666666-6666-4666-8666-666666666666', 'Big Chat', NOW()),
('77777777-7777-4777-8777-777777777777', 'Small Chat 1', NOW()),
('88888888-8888-4888-8888-888888888888', 'Small Chat 2', NOW())
ON CONFLICT DO NOTHING;
""")
op.execute("""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), '66666666-6666-4666-8666-666666666666', '55555555-5555-4555-8555-555555555555', 'Big ' || i, NOW() - (i || ' minutes')::interval
FROM generate_series(1, 80000) AS i;
""")
op.execute("""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), '77777777-7777-4777-8777-777777777777', '55555555-5555-4555-8555-555555555555', 'Small1 ' || i, NOW() - (i || ' minutes')::interval
FROM generate_series(1, 10000) AS i;
""")
op.execute("""
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
SELECT gen_random_uuid(), '88888888-8888-4888-8888-888888888888', '55555555-5555-4555-8555-555555555555', 'Small2 ' || i, NOW() - (i || ' minutes')::interval
FROM generate_series(1, 10000) AS i;
""")
def downgrade() -> None:
op.execute("""
DELETE FROM messages WHERE chat_id IN (
'66666666-6666-4666-8666-666666666666',
'77777777-7777-4777-8777-777777777777',
'88888888-8888-4888-8888-888888888888'
);
""")
op.execute("""
DELETE FROM chats WHERE id IN (
'66666666-6666-4666-8666-666666666666',
'77777777-7777-4777-8777-777777777777',
'88888888-8888-4888-8888-888888888888'
);
""")
op.execute("DELETE FROM users WHERE id = '55555555-5555-4555-8555-555555555555';")