From: Sam Bull Date: Mon, 13 Feb 2023 20:36:06 +0000 (-0500) Subject: Include missing part of env.py X-Git-Tag: rel_1_9_4~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e96972108d3ecd0b723019b30fa80e0e5cdf0719;p=thirdparty%2Fsqlalchemy%2Falembic.git Include missing part of env.py This part is missing from the example, and if a user started with the async template, then they would have an `asyncio.run()` call in here, which breaks the script. Fixes: #1174 Closes: #1172 Pull-request: https://github.com/sqlalchemy/alembic/pull/1172 Pull-request-sha: fa2c55319d09fab4e1435433fa2d7e5fca5dc820 Change-Id: Ia98c8f9a93953f049378f5029e355a3f249ed638 --- diff --git a/alembic/templates/async/env.py b/alembic/templates/async/env.py index c0071b34..847ac983 100644 --- a/alembic/templates/async/env.py +++ b/alembic/templates/async/env.py @@ -1,10 +1,9 @@ import asyncio from logging.config import fileConfig -from sqlalchemy import engine_from_config from sqlalchemy import pool from sqlalchemy.engine import Connection -from sqlalchemy.ext.asyncio import AsyncEngine +from sqlalchemy.ext.asyncio import async_engine_from_config from alembic import context @@ -67,13 +66,10 @@ async def run_migrations_online() -> None: and associate a connection with the context. """ - connectable = AsyncEngine( - engine_from_config( - config.get_section(config.config_ini_section), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - future=True, - ) + connectable = async_engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, ) async with connectable.connect() as connection: diff --git a/alembic/templates/generic/env.py b/alembic/templates/generic/env.py index 6626bfd0..36112a3c 100644 --- a/alembic/templates/generic/env.py +++ b/alembic/templates/generic/env.py @@ -58,7 +58,7 @@ def run_migrations_online() -> None: """ connectable = engine_from_config( - config.get_section(config.config_ini_section), + config.get_section(config.config_ini_section, {}), prefix="sqlalchemy.", poolclass=pool.NullPool, ) diff --git a/alembic/templates/multidb/env.py b/alembic/templates/multidb/env.py index f787824c..e937b64e 100644 --- a/alembic/templates/multidb/env.py +++ b/alembic/templates/multidb/env.py @@ -22,7 +22,7 @@ logger = logging.getLogger("alembic.env") # gather section names referring to different # databases. These are named "engine1", "engine2" # in the sample .ini file. -db_names = config.get_main_option("databases") +db_names = config.get_main_option("databases", "") # add your model's MetaData objects here # for 'autogenerate' support. These must be set @@ -94,7 +94,7 @@ def run_migrations_online() -> None: for name in re.split(r",\s*", db_names): engines[name] = rec = {} rec["engine"] = engine_from_config( - context.config.get_section(name), + context.config.get_section(name, {}), prefix="sqlalchemy.", poolclass=pool.NullPool, ) diff --git a/docs/build/cookbook.rst b/docs/build/cookbook.rst index 8ac5bfc0..d50becc7 100644 --- a/docs/build/cookbook.rst +++ b/docs/build/cookbook.rst @@ -222,7 +222,7 @@ transaction declared in a Python file:: cfg.attributes['connection'] = connection command.upgrade(cfg, "head") -Then in ``env.py``:: +Then in ``env.py`` we can update ``run_migrations_online``:: def run_migrations_online(): connectable = config.attributes.get('connection', None) @@ -1518,14 +1518,7 @@ Programmatic API use (connection sharing) With Asyncio ------------------------------------------------------ Combining the examples of :ref:`connection_sharing` with :ref:`asyncio_recipe` -together, and ``env.py`` as follows works:: - - import asyncio - - from sqlalchemy.ext.asyncio import async_engine_from_config - - # ... no change required to the rest of the code - +together, the ``env.py`` can be updated as follows works:: def do_run_migrations(connection): context.configure(connection=connection, target_metadata=target_metadata) @@ -1535,6 +1528,10 @@ together, and ``env.py`` as follows works:: 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.", @@ -1550,9 +1547,6 @@ together, and ``env.py`` as follows works:: 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 = config.attributes.get("connection", None) @@ -1562,6 +1556,12 @@ together, and ``env.py`` 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::