summaryrefslogtreecommitdiff
path: root/test/dialect/postgresql/test_async_pg_py3k.py
diff options
context:
space:
mode:
authorPavel Sirotkin <pav.pnz@gmail.com>2023-04-20 13:40:04 -0400
committerFederico Caselli <cfederico87@gmail.com>2023-04-21 19:47:26 +0200
commit244c29768254d12ff18bb342b154a009080345d6 (patch)
tree3bc86916e3a12b3dac7d122cd9c4bd1210d5753f /test/dialect/postgresql/test_async_pg_py3k.py
parentfc2bcead435a9bf0a2de8e9b15a1bd835f9d7fe4 (diff)
downloadsqlalchemy-244c29768254d12ff18bb342b154a009080345d6.tar.gz
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 <!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once) --> 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: #<issue number>` 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: #<issue number>` 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
Diffstat (limited to 'test/dialect/postgresql/test_async_pg_py3k.py')
-rw-r--r--test/dialect/postgresql/test_async_pg_py3k.py17
1 files changed, 17 insertions, 0 deletions
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