[+] update alembic

This commit is contained in:
Siarhei Siniak 2025-07-04 10:56:27 +03:00
parent c42e39a8d5
commit 18449382e1
2 changed files with 74 additions and 35 deletions

@ -1,5 +1,9 @@
import asyncio
from logging.config import fileConfig from logging.config import fileConfig
from sqlalchemy.ext.asyncio import async_engine_from_config
from sqlalchemy import engine_from_config from sqlalchemy import engine_from_config
from sqlalchemy import pool from sqlalchemy import pool
@ -29,62 +33,97 @@ target_metadata = Base.metadata
# ... etc. # ... etc.
def run_migrations_offline() -> None: # 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()
#
"""Run migrations in 'offline' mode. # 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()
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( # if context.is_offline_mode():
url=url, # run_migrations_offline()
target_metadata=target_metadata, # else:
literal_binds=True, # run_migrations_online()
dialect_opts={"paramstyle": "named"},
) def do_run_migrations(connection):
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction(): with context.begin_transaction():
context.run_migrations() context.run_migrations()
def run_migrations_online() -> None: async def run_async_migrations():
"""Run migrations in 'online' mode. """In this scenario we need to create an Engine
In this scenario we need to create an Engine
and associate a connection with the context. and associate a connection with the context.
""" """
url = Settings.singleton().db_url url = Settings.singleton().db_url
connectable = engine_from_config( connectable = async_engine_from_config(
config.get_section( config.get_section(config.config_ini_section),
config.config_ini_section, {}
),
prefix="sqlalchemy.", prefix="sqlalchemy.",
poolclass=pool.NullPool, poolclass=pool.NullPool,
url=url, url=url,
) )
with connectable.connect() as connection: async with connectable.connect() as connection:
context.configure( await connection.run_sync(do_run_migrations)
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction(): await connectable.dispose()
context.run_migrations()
if context.is_offline_mode(): def run_migrations_online():
run_migrations_offline() """Run migrations in 'online' mode."""
else:
run_migrations_online() asyncio.run(run_async_migrations())