]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Include missing part of env.py
authorSam Bull <aa6bs0@sambull.org>
Mon, 13 Feb 2023 20:36:06 +0000 (15:36 -0500)
committerCaselIT <cfederico87@gmail.com>
Mon, 13 Feb 2023 21:10:00 +0000 (22:10 +0100)
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

alembic/templates/async/env.py
alembic/templates/generic/env.py
alembic/templates/multidb/env.py
docs/build/cookbook.rst

index c0071b345b652d0ad19937b17475b56191dfb8a0..847ac983cd2eb17c8b3632ebee6e8ed775d6d22d 100644 (file)
@@ -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:
index 6626bfd0c4d6bc29d144218599d446fbbfb238e7..36112a3c68590d6a8e07fea0ce70a5afb38c951a 100644 (file)
@@ -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,
     )
index f787824c6a4111d321b61f51fe37a819e2d1451c..e937b64eeed2fd980c214ab87505d75041b581ad 100644 (file)
@@ -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,
         )
index 8ac5bfc00dab9861a3b505dde3b12896a03ce741..d50becc7dbde834444bba3b3058de5ad78228ffd 100644 (file)
@@ -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::