diff options
Diffstat (limited to 'alembic/env.py')
| -rw-r--r-- | alembic/env.py | 65 | 
1 files changed, 31 insertions, 34 deletions
| diff --git a/alembic/env.py b/alembic/env.py index 7978da1..91aca63 100644 --- a/alembic/env.py +++ b/alembic/env.py @@ -1,15 +1,12 @@ +import asyncio  from logging.config import fileConfig -from sqlalchemy import engine_from_config -from sqlalchemy import pool +from sqlalchemy import Connection, pool +from sqlalchemy.ext.asyncio import async_engine_from_config  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. @@ -21,20 +18,37 @@ 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 = db +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. -config.set_main_option('sqlalchemy.url', build_db_uri()) +config.set_main_option("sqlalchemy.url", build_db_uri()) + +def do_run_migrations(connection: Connection) -> None: +    """Run migrations.""" +    context.configure(connection=connection, target_metadata=target_metadata) + +    with context.begin_transaction(): +        context.run_migrations() + +async def run_async_migrations() -> None: +    """Run migrations asynchronously using the asyncpg driver.""" +    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. +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 @@ -57,26 +71,9 @@ def run_migrations_offline():          context.run_migrations() -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 -        ) - -        with context.begin_transaction(): -            context.run_migrations() +def run_migrations_online() -> None: +    """Run migrations in 'online' mode.""" +    asyncio.run(run_async_migrations())  if context.is_offline_mode(): | 
