This is largely lifted from engine_from_config().
Fixes: #7301
Signed-off-by: Nils Philippsen <nils@tiptoe.de>
# This module is part of SQLAlchemy and is released under
# the MIT License: https://www.opensource.org/licenses/mit-license.php
+from .engine import async_engine_from_config
from .engine import AsyncConnection
from .engine import AsyncEngine
from .engine import AsyncTransaction
return AsyncEngine(sync_engine)
+def async_engine_from_config(configuration, prefix="sqlalchemy.", **kwargs):
+ """Create a new AsyncEngine instance using a configuration dictionary.
+
+ This works analogous to :func:`_sa.engine_from_config`, but the
+ configured dialect must be an asyncio-compatible dialect such as
+ :ref:`dialect-postgresql-asyncpg`. Arguments
+ passed to :func:`_asyncio.async_engine_from_config` are mostly
+ identical to :func:`_sa.engine_from_config`.
+
+ .. versionadded:: 1.4.28
+
+ """
+ options = {
+ key[len(prefix) :]: value
+ for key, value in configuration.items()
+ if key.startswith(prefix)
+ }
+ options["_coerce_config"] = True
+ options.update(kwargs)
+ url = options.pop("url")
+ return create_async_engine(url, **options)
+
+
class AsyncConnectable:
__slots__ = "_slots_dispatch", "__weakref__"
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy import union_all
+from sqlalchemy.ext.asyncio import async_engine_from_config
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.ext.asyncio import engine as _async_engine
from sqlalchemy.ext.asyncio import exc as asyncio_exc
server_side_cursors=True,
)
+ def test_async_engine_from_config(self):
+ config = {
+ "sqlalchemy.url": str(testing.db.url),
+ "sqlalchemy.echo": "true",
+ }
+ engine = async_engine_from_config(config)
+ assert engine.url == testing.db.url
+ assert engine.echo is True
+ assert engine.dialect.is_async is True
+
class AsyncEventTest(EngineFixture):
"""The engine events all run in their normal synchronous context.