From 244c29768254d12ff18bb342b154a009080345d6 Mon Sep 17 00:00:00 2001 From: Pavel Sirotkin Date: Thu, 20 Apr 2023 13:40:04 -0400 Subject: Add name_func optional attribute for asyncpg adapter I faced an issue related to pg bouncer and prepared statement cache flow in asyncpg dialect. Regarding this discussion https://github.com/sqlalchemy/sqlalchemy/issues/6467 I prepared PR to support an optional parameter `name` in prepared statement which is allowed, since 0.25.0 version in `asyncpg` https://github.com/MagicStack/asyncpg/pull/846 **UPD:** the issue with proposal: https://github.com/sqlalchemy/sqlalchemy/issues/9608 ### Description Added optional parameter `name_func` to `AsyncAdapt_asyncpg_connection` class which will call on the `self._connection.prepare()` function and populate a unique name. so in general instead this ```python from uuid import uuid4 from asyncpg import Connection class CConnection(Connection): def _get_unique_id(self, prefix: str) -> str: return f'__asyncpg_{prefix}_{uuid4()}__' engine = create_async_engine(..., connect_args={ 'connection_class': CConnection, }, ) ``` would be enough ```python from uuid import uuid4 engine = create_async_engine(..., connect_args={ 'name_func': lambda: f'__asyncpg_{uuid4()}__', }, ) ``` ### Checklist This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [ ] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [x] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. **Have a nice day!** Fixes: #9608 Closes: #9607 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/9607 Pull-request-sha: b4bc8d3e57ab095a26112830ad4bea36083454e3 Change-Id: Icd753366cba166b8a60d1c8566377ec8335cd828 --- test/dialect/postgresql/test_async_pg_py3k.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'test/dialect/postgresql/test_async_pg_py3k.py') diff --git a/test/dialect/postgresql/test_async_pg_py3k.py b/test/dialect/postgresql/test_async_pg_py3k.py index d9116a7ce..49014fcaf 100644 --- a/test/dialect/postgresql/test_async_pg_py3k.py +++ b/test/dialect/postgresql/test_async_pg_py3k.py @@ -1,4 +1,5 @@ import random +import uuid from sqlalchemy import Column from sqlalchemy import exc @@ -272,3 +273,19 @@ class AsyncPgTest(fixtures.TestBase): await conn.close() eq_(codec_meth.mock_calls, [mock.call(adapted_conn)]) + + @async_test + async def test_name_connection_func(self, metadata, async_testing_engine): + cache = [] + + def name_f(): + name = str(uuid.uuid4()) + cache.append(name) + return name + + engine = async_testing_engine( + options={"connect_args": {"prepared_statement_name_func": name_f}}, + ) + async with engine.begin() as conn: + await conn.execute(select(1)) + assert len(cache) > 0 -- cgit v1.2.1