]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
revise argument to mysqlclient/pymysql ping
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 17 Oct 2023 22:54:23 +0000 (18:54 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Oct 2023 13:28:00 +0000 (09:28 -0400)
Repaired a new incompatibility in the MySQL "pre-ping" routine where the
``False`` argument passed to ``connection.ping()``, which is intended to
disable an unwanted "automatic reconnect" feature,  is being deprecated in
MySQL drivers and backends, and is producing warnings for some versions of
MySQL's native client drivers.  It's removed for mysqlclient, whereas for
PyMySQL and drivers based on PyMySQL, the parameter will be deprecated and
removed at some point, so API introspection is used to future proof against
these various stages of removal.

Fixes: #10492
Change-Id: I8a52428c6f93a03b66a605cb0b85cc5924803d6d
references: #10489

doc/build/changelog/unreleased_14/10492.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/mysql/mysqldb.py
lib/sqlalchemy/dialects/mysql/pymysql.py

diff --git a/doc/build/changelog/unreleased_14/10492.rst b/doc/build/changelog/unreleased_14/10492.rst
new file mode 100644 (file)
index 0000000..8ddf573
--- /dev/null
@@ -0,0 +1,13 @@
+.. change::
+    :tags: bug, mysql
+    :tickets: 10492
+    :versions: 2.0.23
+
+    Repaired a new incompatibility in the MySQL "pre-ping" routine where the
+    ``False`` argument passed to ``connection.ping()``, which is intended to
+    disable an unwanted "automatic reconnect" feature,  is being deprecated in
+    MySQL drivers and backends, and is producing warnings for some versions of
+    MySQL's native client drivers.  It's removed for mysqlclient, whereas for
+    PyMySQL and drivers based on PyMySQL, the parameter will be deprecated and
+    removed at some point, so API introspection is used to future proof against
+    these various stages of removal.
index 0868401d436b6a0c6dbc165b1358d016f6b734e9..d1cf835c54ea659419eb53be52fdc4e7d07ecc53 100644 (file)
@@ -168,7 +168,7 @@ class MySQLDialect_mysqldb(MySQLDialect):
         return on_connect
 
     def do_ping(self, dbapi_connection):
-        dbapi_connection.ping(False)
+        dbapi_connection.ping()
         return True
 
     def do_executemany(self, cursor, statement, parameters, context=None):
index 67ccb17fd839fe664302ab9450a0b55c5fe035a4..6567202a45e4643c419f1225848a7c4992286c6c 100644 (file)
@@ -74,6 +74,40 @@ class MySQLDialect_pymysql(MySQLDialect_mysqldb):
     def import_dbapi(cls):
         return __import__("pymysql")
 
+    @langhelpers.memoized_property
+    def _send_false_to_ping(self):
+        """determine if pymysql has deprecated, changed the default of,
+        or removed the 'reconnect' argument of connection.ping().
+
+        See #10492 and
+        https://github.com/PyMySQL/mysqlclient/discussions/651#discussioncomment-7308971
+        for background.
+
+        """  # noqa: E501
+
+        try:
+            Connection = __import__("pymysql.connections").Connection
+        except (ImportError, AttributeError):
+            return True
+        else:
+            insp = langhelpers.get_callable_argspec(Connection.ping)
+            try:
+                reconnect_arg = insp.args[1]
+            except IndexError:
+                return False
+            else:
+                return reconnect_arg == "reconnect" and (
+                    not insp.defaults or insp.defaults[0] is not False
+                )
+
+    def do_ping(self, dbapi_connection):
+        if self._send_false_to_ping:
+            dbapi_connection.ping(False)
+        else:
+            dbapi_connection.ping()
+
+        return True
+
     def create_connect_args(self, url, _translate_args=None):
         if _translate_args is None:
             _translate_args = dict(username="user")