]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
harden HSTORE registration
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Dec 2024 15:58:01 +0000 (10:58 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 16 Dec 2024 17:08:49 +0000 (12:08 -0500)
* use the driver_connection when we register on the connection
* assert targets passed to register_hstore assert as boolean true;
  psycopg docs say "if None, register globally" but looking in the
  source it's actually registering globally if any false-evaluating
  object is passed.

Change-Id: Ie1fd7c96714b7fe76ef964501691fa48352be259

lib/sqlalchemy/dialects/postgresql/hstore.py
lib/sqlalchemy/dialects/postgresql/psycopg.py

index 5a2d451316dc4d906cb5203c80ad0b5d792d1494..291af36c69b5b4edc709d469984cccac60bd1f02 100644 (file)
@@ -195,6 +195,9 @@ class HSTORE(sqltypes.Indexable, sqltypes.Concatenable, sqltypes.TypeEngine):
     comparator_factory = Comparator
 
     def bind_processor(self, dialect):
+        # note that dialect-specific types like that of psycopg and
+        # psycopg2 will override this method to allow driver-level conversion
+        # instead, see _PsycopgHStore
         def process(value):
             if isinstance(value, dict):
                 return _serialize_hstore(value)
@@ -204,6 +207,9 @@ class HSTORE(sqltypes.Indexable, sqltypes.Concatenable, sqltypes.TypeEngine):
         return process
 
     def result_processor(self, dialect, coltype):
+        # note that dialect-specific types like that of psycopg and
+        # psycopg2 will override this method to allow driver-level conversion
+        # instead, see _PsycopgHStore
         def process(value):
             if value is not None:
                 return _parse_hstore(value)
index 60b684450014eb11ffc65643d9390a7207c70274..52116bbc0aa8d3d022fb5867a8b680756e203d95 100644 (file)
@@ -406,10 +406,12 @@ class PGDialect_psycopg(_PGDialect_common_psycopg):
 
                 # register the adapter for connections made subsequent to
                 # this one
+                assert self._psycopg_adapters_map
                 register_hstore(info, self._psycopg_adapters_map)
 
                 # register the adapter for this connection
-                register_hstore(info, connection.connection)
+                assert connection.connection
+                register_hstore(info, connection.connection.driver_connection)
 
     @classmethod
     def import_dbapi(cls):