From: RamonWill Date: Wed, 2 Sep 2020 22:43:53 +0000 (-0400) Subject: Support for multiple hosts in PostgreSQL connection string X-Git-Tag: rel_1_4_0b1~64^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f9ef7fd30b3f0613bc9dd912621856b3262d23bc;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Support for multiple hosts in PostgreSQL connection string Provide support for multiple hosts in the PostgreSQL connection string. A user requested for SQLAlchemy to support multiple hosts within a PostgreSQL URL string. The proposed fix allows this. In the event that the url contains multiple hosts the proposed code will convert the query["hosts"] tuple into a single string. This allows the hosts to then get converted into a valid dsn variable in the psycopg2 connect function. This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [X ] 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. - [ ] 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: #4392 Closes: #5554 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5554 Pull-request-sha: 3f7a0ab8df9f1411a9f1ac0e152583bc7bf0c365 Change-Id: I3f3768d51b8331de786ffdc025b7ecfc662eafe5 --- diff --git a/doc/build/changelog/unreleased_13/4392.rst b/doc/build/changelog/unreleased_13/4392.rst new file mode 100644 index 0000000000..ecb10ca25d --- /dev/null +++ b/doc/build/changelog/unreleased_13/4392.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: engine, postgresql, usecase + :tickets: 4392 + + A :class:`.URL` connection now supports multiple hosts for PostgreSQL. Any + query that contains multiple hosts will now group the hosts as a string + instead of a tuple of strings. Pull request courtesy Ramon Williams. diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg2.py b/lib/sqlalchemy/dialects/postgresql/psycopg2.py index 91576c4d20..3c9ef72c44 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg2.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg2.py @@ -71,6 +71,28 @@ using ``host`` as an additional keyword argument:: `PQconnectdbParams \ `_ +Multiple Hosts in Connection String +------------------------ +psycopg2 supports multiple connection points in the connection string. +When the ``host`` parameter is used multiple times in the query section of +the URL, SQLAlchemy will create a single string of the host and port +information provided to make the connections:: + + create_engine( + "postgresql+psycopg2://user:password@/dbname?host=HostA:port1&host=HostB&host=HostC" + ) + +A connection to each host is then attempted until either a connection is successful +or all connections are unsuccessful in which case an error is raised. + +.. versionadded:: 1.3.20 Support for multiple hosts in PostgreSQL connection + string. + +.. seealso:: + + `PQConnString \ + `_ + Empty DSN Connections / Environment Variable Connections --------------------------------------------------------- @@ -929,16 +951,25 @@ class PGDialect_psycopg2(PGDialect): def create_connect_args(self, url): opts = url.translate_connect_args(username="user") + + is_multihost = False + if "host" in url.query: + is_multihost = isinstance(url.query["host"], (list, tuple)) + if opts: if "port" in opts: opts["port"] = int(opts["port"]) opts.update(url.query) + if is_multihost: + opts["host"] = ",".join(url.query["host"]) # send individual dbname, user, password, host, port # parameters to psycopg2.connect() return ([], opts) elif url.query: # any other connection arguments, pass directly opts.update(url.query) + if is_multihost: + opts["host"] = ",".join(url.query["host"]) return ([], opts) else: # no connection arguments whatsoever; psycopg2.connect() diff --git a/test/dialect/postgresql/test_dialect.py b/test/dialect/postgresql/test_dialect.py index 971d4f12f7..46a01860d5 100644 --- a/test/dialect/postgresql/test_dialect.py +++ b/test/dialect/postgresql/test_dialect.py @@ -159,6 +159,25 @@ class DialectTest(fixtures.TestBase): eq_(cargs, []) eq_(cparams, {"host": "somehost", "any_random_thing": "yes"}) + def test_psycopg2_nonempty_connection_string_w_query_two(self): + dialect = psycopg2_dialect.dialect() + url_string = "postgresql://USER:PASS@/DB?host=hostA" + u = url.make_url(url_string) + cargs, cparams = dialect.create_connect_args(u) + eq_(cargs, []) + eq_(cparams["host"], "hostA") + + def test_psycopg2_nonempty_connection_string_w_query_three(self): + dialect = psycopg2_dialect.dialect() + url_string = ( + "postgresql://USER:PASS@/DB" + "?host=hostA:portA&host=hostB&host=hostC" + ) + u = url.make_url(url_string) + cargs, cparams = dialect.create_connect_args(u) + eq_(cargs, []) + eq_(cparams["host"], "hostA:portA,hostB,hostC") + class ExecuteManyMode(object): __only_on__ = "postgresql+psycopg2"