]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Propagate query-arg-only URL to psycopg2; don't send blank host
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 9 Apr 2019 13:16:16 +0000 (09:16 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 9 Apr 2019 21:40:14 +0000 (17:40 -0400)
Fixed regression from release 1.3.2 caused by :ticket:`4562` where a URL
that contained only a query string and no hostname, such as for the
purposes of specifying a service file with connection information, would no
longer be propagated to psycopg2 properly.   The change in :ticket:`4562`
has been adjusted to further suit psycopg2's exact requirements, which is
that if there are any connection parameters whatsoever, the "dsn" parameter
is no longer required, so in this case the query string parameters are
passed alone.

Fixes: #4601
Change-Id: Ic29a8b77bcf50ee996968bab25aaac7ae4bfc26f

doc/build/changelog/unreleased_13/4601.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/postgresql/psycopg2.py
test/dialect/postgresql/test_dialect.py

diff --git a/doc/build/changelog/unreleased_13/4601.rst b/doc/build/changelog/unreleased_13/4601.rst
new file mode 100644 (file)
index 0000000..950d746
--- /dev/null
@@ -0,0 +1,12 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 4601
+
+    Fixed regression from release 1.3.2 caused by :ticket:`4562` where a URL
+    that contained only a query string and no hostname, such as for the
+    purposes of specifying a service file with connection information, would no
+    longer be propagated to psycopg2 properly.   The change in :ticket:`4562`
+    has been adjusted to further suit psycopg2's exact requirements, which is
+    that if there are any connection parameters whatsoever, the "dsn" parameter
+    is no longer required, so in this case the query string parameters are
+    passed alone.
\ No newline at end of file
index 199c1c776486aeeb41b7374abac074b0a422738f..fb127f1c75b43c87a01cc64e1a1e352fa0f63483 100644 (file)
@@ -769,8 +769,13 @@ class PGDialect_psycopg2(PGDialect):
             # 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)
+            return ([], opts)
         else:
-            # send a blank string for "dsn" to psycopg2.connect()
+            # no connection arguments whatsoever; psycopg2.connect()
+            # requires that "dsn" be present as a blank string.
             return ([''], opts)
 
     def is_disconnect(self, e, connection, cursor):
index 8c19e31bb3b1b90b19332fefb77b18da2469aa8e..1a735c41d37714ea12a3cf184ecf1f1cf978e8d0 100644 (file)
@@ -129,6 +129,27 @@ class DialectTest(fixtures.TestBase):
         eq_(cargs, [])
         eq_(cparams, {"host": "host"})
 
+    def test_psycopg2_empty_connection_string_w_query_one(self):
+        dialect = psycopg2_dialect.dialect()
+        u = url.make_url("postgresql:///?service=swh-log")
+        cargs, cparams = dialect.create_connect_args(u)
+        eq_(cargs, [])
+        eq_(cparams, {"service": "swh-log"})
+
+    def test_psycopg2_empty_connection_string_w_query_two(self):
+        dialect = psycopg2_dialect.dialect()
+        u = url.make_url("postgresql:///?any_random_thing=yes")
+        cargs, cparams = dialect.create_connect_args(u)
+        eq_(cargs, [])
+        eq_(cparams, {"any_random_thing": "yes"})
+
+    def test_psycopg2_nonempty_connection_string_w_query(self):
+        dialect = psycopg2_dialect.dialect()
+        u = url.make_url("postgresql://somehost/?any_random_thing=yes")
+        cargs, cparams = dialect.create_connect_args(u)
+        eq_(cargs, [])
+        eq_(cparams, {"host": "somehost", "any_random_thing": "yes"})
+
 
 class BatchInsertsTest(fixtures.TablesTest):
     __only_on__ = "postgresql+psycopg2"