This is an automated email from the ASF dual-hosted git repository.

phanikumv pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new 7ad586ed6a fix(providers/common/sql): add dummy connection setter for 
backward compatibility (#42490)
7ad586ed6a is described below

commit 7ad586ed6a6cb93fc25ae0405eb9f6c17d360ef0
Author: Wei Lee <[email protected]>
AuthorDate: Thu Sep 26 21:39:00 2024 +0900

    fix(providers/common/sql): add dummy connection setter for backward 
compatibility (#42490)
    
    the introduction of connection property breaks 
apache-airflow-providers-mysql<5.7.1,
    apache-airflow-providers-elasticsearch<5.5.1
     and apache-airflow-providers-postgres<5.13.0
---
 airflow/providers/common/sql/hooks/sql.py  | 11 +++++++++++
 airflow/providers/common/sql/hooks/sql.pyi |  2 ++
 tests/providers/mysql/hooks/test_mysql.py  | 15 +++++++++++++++
 3 files changed, 28 insertions(+)

diff --git a/airflow/providers/common/sql/hooks/sql.py 
b/airflow/providers/common/sql/hooks/sql.py
index f2d11b21d7..c0a3ed5f9a 100644
--- a/airflow/providers/common/sql/hooks/sql.py
+++ b/airflow/providers/common/sql/hooks/sql.py
@@ -210,6 +210,17 @@ class DbApiHook(BaseHook):
             self._connection = self.get_connection(self.get_conn_id())
         return self._connection
 
+    @connection.setter
+    def connection(self, value: Any) -> None:
+        # This setter is for backward compatibility and should not be used.
+        # Since the introduction of connection property, the providers listed 
below
+        # breaks due to assigning value to self.connection
+        #
+        # apache-airflow-providers-mysql<5.7.1
+        # apache-airflow-providers-elasticsearch<5.5.1
+        # apache-airflow-providers-postgres<5.13.0
+        pass
+
     @property
     def connection_extra(self) -> dict:
         return self.connection.extra_dejson
diff --git a/airflow/providers/common/sql/hooks/sql.pyi 
b/airflow/providers/common/sql/hooks/sql.pyi
index 21081f06d3..e54b033991 100644
--- a/airflow/providers/common/sql/hooks/sql.pyi
+++ b/airflow/providers/common/sql/hooks/sql.pyi
@@ -70,6 +70,8 @@ class DbApiHook(BaseHook):
     def placeholder(self): ...
     @property
     def connection(self) -> Connection: ...
+    @connection.setter
+    def connection(self, value: Any) -> None: ...
     @property
     def connection_extra(self) -> dict: ...
     @cached_property
diff --git a/tests/providers/mysql/hooks/test_mysql.py 
b/tests/providers/mysql/hooks/test_mysql.py
index cb6005ca8c..48fc62fe2c 100644
--- a/tests/providers/mysql/hooks/test_mysql.py
+++ b/tests/providers/mysql/hooks/test_mysql.py
@@ -67,6 +67,21 @@ class TestMySqlHookConn:
         assert kwargs["host"] == "host"
         assert kwargs["db"] == "schema"
 
+    @mock.patch("MySQLdb.connect")
+    def test_dummy_connection_setter(self, mock_connect):
+        self.db_hook.get_conn()
+
+        self.db_hook.connection = "Won't affect anything"
+        assert self.db_hook.connection != "Won't affect anything"
+
+        assert mock_connect.call_count == 1
+        args, kwargs = mock_connect.call_args
+        assert args == ()
+        assert kwargs["user"] == "login"
+        assert kwargs["passwd"] == "password"
+        assert kwargs["host"] == "host"
+        assert kwargs["db"] == "schema"
+
     @mock.patch("MySQLdb.connect")
     def test_get_uri(self, mock_connect):
         self.connection.extra = json.dumps({"charset": "utf-8"})

Reply via email to