Skip to content

Commit

Permalink
Support target_session_attrs in URL format, add tests (#1073)
Browse files Browse the repository at this point in the history
  • Loading branch information
elprans committed Aug 24, 2023
1 parent 89d5bd0 commit 7cb4e70
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions asyncpg/connect_utils.py
Expand Up @@ -378,6 +378,13 @@ def _parse_connect_dsn_and_args(*, dsn, host, port, user,
'ssl_max_protocol_version'
)

if 'target_session_attrs' in query:
dsn_target_session_attrs = query.pop(
'target_session_attrs'
)
if target_session_attrs is None:
target_session_attrs = dsn_target_session_attrs

if query:
if server_settings is None:
server_settings = query
Expand Down
36 changes: 36 additions & 0 deletions tests/test_connect.py
Expand Up @@ -563,6 +563,42 @@ class TestConnectParams(tb.TestCase):
})
},

{
'name': 'target_session_attrs',
'dsn': 'postgresql://user@host1:1111,host2:2222/db'
'?target_session_attrs=read-only',
'result': ([('host1', 1111), ('host2', 2222)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'read-only',
})
},

{
'name': 'target_session_attrs_2',
'dsn': 'postgresql://user@host1:1111,host2:2222/db'
'?target_session_attrs=read-only',
'target_session_attrs': 'read-write',
'result': ([('host1', 1111), ('host2', 2222)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'read-write',
})
},

{
'name': 'target_session_attrs_3',
'dsn': 'postgresql://user@host1:1111,host2:2222/db',
'env': {
'PGTARGETSESSIONATTRS': 'read-only',
},
'result': ([('host1', 1111), ('host2', 2222)], {
'database': 'db',
'user': 'user',
'target_session_attrs': 'read-only',
})
},

{
'name': 'dsn_ipv6_multi_host',
'dsn': 'postgresql://user@[2001:db8::1234%25eth0],[::1]/db',
Expand Down

0 comments on commit 7cb4e70

Please sign in to comment.