]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commit
Add name_func optional attribute for asyncpg adapter
authorPavel Sirotkin <pav.pnz@gmail.com>
Thu, 20 Apr 2023 17:40:04 +0000 (13:40 -0400)
committerFederico Caselli <cfederico87@gmail.com>
Fri, 21 Apr 2023 17:47:26 +0000 (19:47 +0200)
commit244c29768254d12ff18bb342b154a009080345d6
tree3bc86916e3a12b3dac7d122cd9c4bd1210d5753f
parentfc2bcead435a9bf0a2de8e9b15a1bd835f9d7fe4
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
doc/build/changelog/unreleased_20/9608.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/asyncpg.py
test/dialect/postgresql/test_async_pg_py3k.py