готово лаб2
Skeleton contracts / contracts (pull_request) Canceled after 0s

This commit is contained in:
2026-10-23 17:51:58 +03:00
parent 65003b6119
commit 6dbc2da1ca
47 changed files with 201807 additions and 25 deletions
+1
View File
@@ -0,0 +1 @@
Generic single-database configuration.
+88
View File
@@ -0,0 +1,88 @@
from app.models.base import Base
from app.models.user import User
from app.models.chat import Chat, ChatMember
from app.models.message import Message
from app.config import DATABASE_URL
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
config.set_main_option("sqlalchemy.url", DATABASE_URL.replace("+asyncpg", "+psycopg2"))
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline() -> None:
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section, {}),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
+26
View File
@@ -0,0 +1,26 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision: str = ${repr(up_revision)}
down_revision: Union[str, None] = ${repr(down_revision)}
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,49 @@
"""seed explain test 10k
Revision ID: 509b7b85e81f
Revises: 9f0905ee61f9
Create Date: 2026-10-23 10:19:10.693752
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '509b7b85e81f'
down_revision: Union[str, None] = '9f0905ee61f9'
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,70 @@
"""seed load test data
Revision ID: 9f0905ee61f9
Revises: d3ba2cff7625
Create Date: 2026-10-23 10:08:32.985236
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '9f0905ee61f9'
down_revision: Union[str, None] = 'd3ba2cff7625'
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';")
@@ -0,0 +1,30 @@
"""create tables
Revision ID: d3ba2cff7625
Revises:
Create Date: 2026-10-23 10:07:55.443651
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = 'd3ba2cff7625'
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! ###
pass
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
pass
# ### end Alembic commands ###