From 9a477ef054e3da35513a368b7b82c52cd6b723ca Mon Sep 17 00:00:00 2001 From: CaselIT Date: Tue, 14 Feb 2023 21:52:33 +0100 Subject: Follow up Ia98c8f9a93953f049378f5029e355a3f249ed638 that was merged prematurely Change-Id: I131dfb8dabf10457954f5216b1abadc976aebd38 --- docs/build/cookbook.rst | 46 ++++++++-------------------------------------- 1 file changed, 8 insertions(+), 38 deletions(-) (limited to 'docs/build/cookbook.rst') 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:: -- cgit v1.2.1