]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Support for multiple hosts in PostgreSQL connection string 5554/head
authorRamonWill <ramonwilliams@hotmail.co.uk>
Wed, 2 Sep 2020 22:04:06 +0000 (23:04 +0100)
committerRamonWill <ramonwilliams@hotmail.co.uk>
Wed, 2 Sep 2020 22:04:06 +0000 (23:04 +0100)
doc/build/changelog/unreleased_13/4392.rst [new file with mode: 0644]
lib/sqlalchemy/engine/url.py
test/dialect/postgresql/test_dialect.py

diff --git a/doc/build/changelog/unreleased_13/4392.rst b/doc/build/changelog/unreleased_13/4392.rst
new file mode 100644 (file)
index 0000000..ecb10ca
--- /dev/null
@@ -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.
index 6d2f4aa244979cf6238384a0b1a67ee41244180c..763af74fd36c81aead645eedbce4cb192e8d1dea 100644 (file)
@@ -755,6 +755,13 @@ def _parse_rfc1738_args(name):
             query = None
         components["query"] = query
 
+        if components["query"]:
+            if "host" in components["query"]:
+                if not isinstance(components["query"]["host"], str):
+                    components["query"]["host"] = ",".join(
+                        components["query"]["host"]
+                    )
+
         if components["username"] is not None:
             components["username"] = _rfc_1738_unquote(components["username"])
 
index 6eaa3295b9aa09882ea03f3d86083c6ce665742b..140e19b17abcb85c7e940e064ec589ec36b14f6c 100644 (file)
@@ -158,6 +158,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"