summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCaselIT <cfederico87@gmail.com>2023-02-14 21:52:33 +0100
committerCaselIT <cfederico87@gmail.com>2023-02-14 21:52:33 +0100
commit9a477ef054e3da35513a368b7b82c52cd6b723ca (patch)
tree3c4be2c63ecb7d3619c7a0b7b7eaafb0172ba25d
parenta79ba7d74d12e0be63e1afb05e037d0a052b5086 (diff)
downloadalembic-9a477ef054e3da35513a368b7b82c52cd6b723ca.tar.gz
Follow up Ia98c8f9a93953f049378f5029e355a3f249ed638
that was merged prematurely Change-Id: I131dfb8dabf10457954f5216b1abadc976aebd38
-rw-r--r--alembic/templates/async/env.py15
-rw-r--r--docs/build/cookbook.rst46
2 files changed, 18 insertions, 43 deletions
diff --git a/alembic/templates/async/env.py b/alembic/templates/async/env.py
index 847ac98..9f2d519 100644
--- a/alembic/templates/async/env.py
+++ b/alembic/templates/async/env.py
@@ -59,13 +59,12 @@ def do_run_migrations(connection: Connection) -> None:
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.",
@@ -78,7 +77,13 @@ async def run_migrations_online() -> None:
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()
diff --git a/docs/build/cookbook.rst b/docs/build/cookbook.rst
index d50becc..58a65ea 100644
--- a/docs/build/cookbook.rst
+++ b/docs/build/cookbook.rst
@@ -1483,13 +1483,12 @@ file that's used by Alembic to start its operations. In particular only
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.",
@@ -1502,10 +1501,10 @@ file that's used by Alembic to start its operations. In particular only
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
@@ -1518,30 +1517,7 @@ Programmatic API use (connection sharing) With Asyncio
------------------------------------------------------
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():
@@ -1556,12 +1532,6 @@ together, the ``env.py`` can be updated as follows works::
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::