Skip to content

Commit

Permalink
fix(providers/common/sql): add dummy connection setter for backward c…
Browse files Browse the repository at this point in the history
…ompatibility (apache#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
  • Loading branch information
Lee-W authored Sep 26, 2024
1 parent 5af7463 commit 7ad586e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
11 changes: 11 additions & 0 deletions airflow/providers/common/sql/hooks/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,17 @@ def connection(self) -> Connection:
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
Expand Down
2 changes: 2 additions & 0 deletions airflow/providers/common/sql/hooks/sql.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions tests/providers/mysql/hooks/test_mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ def test_get_conn(self, mock_connect):
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"})
Expand Down

0 comments on commit 7ad586e

Please sign in to comment.