context.run_migrations()
-async def run_migrations_online() -> None:
- """Run migrations in 'online' mode.
-
- In this scenario we need to create an Engine
+async def run_async_migrations() -> None:
+ """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.",
await connectable.dispose()
+def run_migrations_online() -> None:
+ """Run migrations in 'online' mode."""
+
+ asyncio.run(run_async_migrations())
+
+
if context.is_offline_mode():
run_migrations_offline()
else:
- asyncio.run(run_migrations_online())
+ run_migrations_online()
context.run_migrations()
- async def run_migrations_online():
- """Run migrations in 'online' mode.
-
- In this scenario we need to create an Engine
+ 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.",
await connectable.dispose()
- if context.is_offline_mode():
- run_migrations_offline()
- else:
- asyncio.run(run_migrations_online())
+ def run_migrations_online():
+ """Run migrations in 'online' mode."""
+
+ asyncio.run(run_async_migrations())
An async application can also interact with the Alembic api directly by using
the SQLAlchemy ``run_sync`` method to adapt the non-async api of Alembic to
------------------------------------------------------
Combining the examples of :ref:`connection_sharing` with :ref:`asyncio_recipe`
-together, the ``env.py`` can be updated as follows works::
-
- 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()
+together, the ``env.py`` listed above can be updated as follows works::
def run_migrations_online():
else:
do_run_migrations(connectable)
-
- if context.is_offline_mode():
- run_migrations_offline()
- else:
- run_migrations_online()
-
Above, using an asyncio database URL in ``alembic.ini`` one can run
commands such as ``alembic upgrade`` from the command line. Programmatically,
the same ``env.py`` file can be invoked using asyncio as::