Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multi port connection string issue #1222

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion asyncpg/connect_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,15 @@ def _read_password_from_pgpass(


def _validate_port_spec(hosts, port):
if isinstance(port, list):
if isinstance(port, list) and len(port) > 1:
# If there is a list of ports, its length must
# match that of the host list.
if len(port) != len(hosts):
raise exceptions.ClientConfigurationError(
'could not match {} port numbers to {} hosts'.format(
len(port), len(hosts)))
elif isinstance(port, list) and len(port) == 1:
port = [port[0] for _ in range(len(hosts))]
else:
port = [port for _ in range(len(hosts))]

Expand Down
14 changes: 14 additions & 0 deletions tests/test_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,20 @@ class TestConnectParams(tb.TestCase):
}
)
},
{
'name': 'multi_host_single_port',
'dsn': 'postgres:///postgres?host=127.0.0.1,127.0.0.2&port=5432&user=postgres',
'result': (
[
('127.0.0.1', 5432),
('127.0.0.2', 5432)
], {
'user': 'postgres',
'database': 'postgres',
'target_session_attrs': 'any',
}
)
},
]

@contextlib.contextmanager
Expand Down