diff options
Diffstat (limited to 'alembic/env.py')
| -rw-r--r-- | alembic/env.py | 50 | 
1 files changed, 29 insertions, 21 deletions
| diff --git a/alembic/env.py b/alembic/env.py index 7978da1..903d5ef 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,15 +1,17 @@  from logging.config import fileConfig -from sqlalchemy import engine_from_config +from sqlalchemy.ext.asyncio import async_engine_from_config  from sqlalchemy import pool +import asyncio +  from alembic import context  import sys  sys.path.append(".")  from metricity.database import build_db_uri -from metricity.models import db +from metricity.models import Base  # this is the Alembic Config object, which provides  # access to the values within the .ini file in use. @@ -23,7 +25,7 @@ fileConfig(config.config_file_name)  # for 'autogenerate' support  # from myapp import mymodel  # target_metadata = mymodel.Base.metadata -target_metadata = db +target_metadata = Base.metadata  # other values from the config, defined by the needs of env.py,  # can be acquired: @@ -32,6 +34,28 @@ target_metadata = db  config.set_main_option('sqlalchemy.url', build_db_uri()) +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. + +    """ + +    connectable = async_engine_from_config( +        config.get_section(config.config_ini_section), +        prefix="sqlalchemy.", +        poolclass=pool.NullPool, +    ) + +    async with connectable.connect() as connection: +        await connection.run_sync(do_run_migrations) + +    await connectable.dispose()  def run_migrations_offline():      """Run migrations in 'offline' mode. @@ -58,25 +82,9 @@ def run_migrations_offline():  def run_migrations_online(): -    """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 -        ) +    """Run migrations in 'online' mode.""" -        with context.begin_transaction(): -            context.run_migrations() +    asyncio.run(run_async_migrations())  if context.is_offline_mode(): | 
