From 3f1e8c57ac416890b3722c254162b534227f2b8f Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Fri, 4 Jul 2025 11:33:33 +0300 Subject: [PATCH] [+] update alembic 1. fix async connection for alembic; 2. generate Market table migration; 3. migrate database; --- deps/test-task-2025-06-30-v1/pyproject.toml | 2 + .../tickers/alembic/env.py | 121 ++++++++---------- .../versions/335b4c4f052c_add_market_table.py | 36 ++++++ 3 files changed, 90 insertions(+), 69 deletions(-) create mode 100644 deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/versions/335b4c4f052c_add_market_table.py diff --git a/deps/test-task-2025-06-30-v1/pyproject.toml b/deps/test-task-2025-06-30-v1/pyproject.toml index b3e9f22..5ee4e67 100644 --- a/deps/test-task-2025-06-30-v1/pyproject.toml +++ b/deps/test-task-2025-06-30-v1/pyproject.toml @@ -36,6 +36,8 @@ build-backend = 'setuptools.build_meta' script_location = 'python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic' prepend_sys_path = ['python'] +# sqlalchemy.url = 'asdfasdf:/asdfasdfa' + [tool.ruff] line-length = 160 target-version = 'py310' diff --git a/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/env.py b/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/env.py index 29dc690..4c71459 100644 --- a/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/env.py +++ b/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/env.py @@ -1,4 +1,5 @@ import asyncio +import logging from logging.config import fileConfig @@ -9,22 +10,35 @@ from sqlalchemy import pool from alembic import context +from online.fxreader.pr34.test_task_2025_06_30_v1.tickers.settings import Settings +from online.fxreader.pr34.test_task_2025_06_30_v1.tickers.models import ( + Base, + Market, +) + # 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', + Settings.singleton().db_url +) + # 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) +else: + logging.basicConfig(level=logging.DEBUG) + +logger = logging.getLogger(__name__) # add your model's MetaData object here # for 'autogenerate' support # from myapp import mymodel # target_metadata = mymodel.Base.metadata # target_metadata = None -from online.fxreader.pr34.test_task_2025_06_30_v1.tickers.settings import Settings -from online.fxreader.pr34.test_task_2025_06_30_v1.tickers.models import Base target_metadata = Base.metadata # other values from the config, defined by the needs of env.py, @@ -33,97 +47,66 @@ target_metadata = Base.metadata # ... 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") -# url = Settings.singleton().db_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. -# -# """ -# -# url = Settings.singleton().db_url -# -# connectable = engine_from_config( -# config.get_section( -# config.config_ini_section, {} -# ), -# prefix="sqlalchemy.", -# poolclass=pool.NullPool, -# url=url, -# ) -# -# 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() - def do_run_migrations(connection): context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() - async def run_async_migrations(): """In this scenario we need to create an Engine and associate a connection with the context. """ - url = Settings.singleton().db_url - + logger.info(dict(msg='started')) connectable = async_engine_from_config( - config.get_section(config.config_ini_section), + config.get_section(config.config_ini_section, {}), prefix="sqlalchemy.", poolclass=pool.NullPool, - url=url, ) + async with connectable.connect() as connection: await connection.run_sync(do_run_migrations) await connectable.dispose() + logger.info(dict(msg='done')) + + +def run_migrations_offline(): + """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(): """Run migrations in 'online' mode.""" asyncio.run(run_async_migrations()) + + +if context.is_offline_mode(): + raise NotImplementedError + # run_migrations_offline() +else: + run_migrations_online() diff --git a/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/versions/335b4c4f052c_add_market_table.py b/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/versions/335b4c4f052c_add_market_table.py new file mode 100644 index 0000000..60c4f3d --- /dev/null +++ b/deps/test-task-2025-06-30-v1/python/online/fxreader/pr34/test_task_2025_06_30_v1/tickers/alembic/versions/335b4c4f052c_add_market_table.py @@ -0,0 +1,36 @@ +"""add Market table + +Revision ID: 335b4c4f052c +Revises: +Create Date: 2025-07-04 11:31:10.983947 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '335b4c4f052c' +down_revision: Union[str, Sequence[str], None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('tickers_market', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=32), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('tickers_market') + # ### end Alembic commands ###