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
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:
# 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
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,
)
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)
------------------------------------------------------
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)
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.",
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)
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::