You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "guan404ming (via GitHub)" <gi...@apache.org> on 2025/04/02 18:26:13 UTC
[PR] feat: overwrite `get-uri` for MySQL [airflow]
guan404ming opened a new pull request, #48706:
URL: https://github.com/apache/airflow/pull/48706
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
<!-- Please keep an empty line above the dashes. -->
Towards #38195
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on PR #48706:
URL: https://github.com/apache/airflow/pull/48706#issuecomment-2778270130
Thanks a lots for reviewing, I had learned the convention more and updated with your comments!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2028278049
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +364,44 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ conn = self.connection or self.get_connection(self.get_conn_id())
+ conn_schema = self.schema or conn.schema or ""
+ client_name = conn.extra_dejson.get("client", "mysqlclient")
+
+ # Determine URI prefix based on client
+ if client_name == "mysql-connector-python":
+ uri_prefix = "mysql+mysqlconnector://"
+ else: # default: mysqlclient
+ uri_prefix = "mysql://"
+
+ auth_part = ""
+ if conn.login:
+ auth_part += quote_plus(conn.login)
+ if conn.password:
+ auth_part += ":" + quote_plus(conn.password)
+ auth_part += "@"
Review Comment:
```suggestion
auth_part = ""
if conn.login:
auth_part = quote_plus(conn.login)
if conn.password:
auth_part = f"{auth_part}:{quote_plus(conn.password)}"
auth_part = f"{auth_part}@"
```
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +364,44 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ conn = self.connection or self.get_connection(self.get_conn_id())
+ conn_schema = self.schema or conn.schema or ""
+ client_name = conn.extra_dejson.get("client", "mysqlclient")
+
+ # Determine URI prefix based on client
+ if client_name == "mysql-connector-python":
+ uri_prefix = "mysql+mysqlconnector://"
+ else: # default: mysqlclient
+ uri_prefix = "mysql://"
+
+ auth_part = ""
+ if conn.login:
+ auth_part += quote_plus(conn.login)
+ if conn.password:
+ auth_part += ":" + quote_plus(conn.password)
+ auth_part += "@"
+
+ host_part = conn.host or "localhost"
+ if conn.port:
+ host_part += f":{conn.port}"
Review Comment:
```suggestion
host_part = conn.host or "localhost"
if conn.port:
host_part = f"{host_part}:{conn.port}"
```
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +364,44 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ conn = self.connection or self.get_connection(self.get_conn_id())
+ conn_schema = self.schema or conn.schema or ""
+ client_name = conn.extra_dejson.get("client", "mysqlclient")
+
+ # Determine URI prefix based on client
+ if client_name == "mysql-connector-python":
+ uri_prefix = "mysql+mysqlconnector://"
+ else: # default: mysqlclient
+ uri_prefix = "mysql://"
+
+ auth_part = ""
+ if conn.login:
+ auth_part += quote_plus(conn.login)
+ if conn.password:
+ auth_part += ":" + quote_plus(conn.password)
+ auth_part += "@"
+
+ host_part = conn.host or "localhost"
+ if conn.port:
+ host_part += f":{conn.port}"
+
+ schema_part = f"/{quote_plus(conn_schema)}" if conn_schema else ""
+
+ uri = f"{uri_prefix}{auth_part}{host_part}{schema_part}"
+
+ # Add extra connection parameters
+ extra = conn.extra_dejson.copy()
+ if "client" in extra:
+ extra.pop("client")
+
+ params = []
+ for k, v in extra.items():
+ if v:
+ params.append(f"{k}={quote_plus(str(v))}")
+ if params:
+ uri += "?" + "&".join(params)
Review Comment:
we can use https://docs.python.org/3/library/urllib.parse.html#urllib.parse.urlencode for this whole section
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W merged PR #48706:
URL: https://github.com/apache/airflow/pull/48706
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2027970956
##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -86,12 +86,89 @@ def test_dummy_connection_setter(self, mock_connect):
assert kwargs["db"] == "schema"
@mock.patch("MySQLdb.connect")
- def test_get_uri(self, mock_connect):
- self.connection.extra = json.dumps({"charset": "utf-8"})
- self.db_hook.get_conn()
- assert mock_connect.call_count == 1
- args, kwargs = mock_connect.call_args
- assert self.db_hook.get_uri() == "mysql://login:password@host/schema?charset=utf-8"
+ @pytest.mark.parametrize(
+ "connection_params, expected_uri",
+ [
+ (
+ {
+ "login": "login",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://login:password@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "pass/word!",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://user%40domain:pass%2Fword%21@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "db/name",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/db%2Fname",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps(
+ {
+ "client": "mysql-connector-python",
+ "ssl_ca": "/path/to/ca",
+ "ssl_cert": "/path/to/cert with space",
+ }
+ ),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema?ssl_ca=%2Fpath%2Fto%2Fca&ssl_cert=%2Fpath%2Fto%2Fcert+with+space",
+ ),
+ ],
+ )
Review Comment:
How about using "pytest.param" with the "id" argument to improve test readability and make it easier to identify each case?
##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -86,12 +86,89 @@ def test_dummy_connection_setter(self, mock_connect):
assert kwargs["db"] == "schema"
@mock.patch("MySQLdb.connect")
- def test_get_uri(self, mock_connect):
- self.connection.extra = json.dumps({"charset": "utf-8"})
- self.db_hook.get_conn()
- assert mock_connect.call_count == 1
- args, kwargs = mock_connect.call_args
- assert self.db_hook.get_uri() == "mysql://login:password@host/schema?charset=utf-8"
+ @pytest.mark.parametrize(
+ "connection_params, expected_uri",
+ [
+ (
+ {
+ "login": "login",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://login:password@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "pass/word!",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://user%40domain:pass%2Fword%21@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "db/name",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/db%2Fname",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps(
+ {
+ "client": "mysql-connector-python",
+ "ssl_ca": "/path/to/ca",
+ "ssl_cert": "/path/to/cert with space",
+ }
+ ),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema?ssl_ca=%2Fpath%2Fto%2Fca&ssl_cert=%2Fpath%2Fto%2Fcert+with+space",
+ ),
+ ],
+ )
+ def test_get_uri_parametrized(self, mock_connect, connection_params, expected_uri):
Review Comment:
```suggestion
def test_get_uri(self, mock_connect, connection_params, expected_uri):
```
Leave it as "test_get_uri" is fine, we don't need to mention "parameterize" in method name.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2028103095
##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -86,12 +86,89 @@ def test_dummy_connection_setter(self, mock_connect):
assert kwargs["db"] == "schema"
@mock.patch("MySQLdb.connect")
- def test_get_uri(self, mock_connect):
- self.connection.extra = json.dumps({"charset": "utf-8"})
- self.db_hook.get_conn()
- assert mock_connect.call_count == 1
- args, kwargs = mock_connect.call_args
- assert self.db_hook.get_uri() == "mysql://login:password@host/schema?charset=utf-8"
+ @pytest.mark.parametrize(
+ "connection_params, expected_uri",
+ [
+ (
+ {
+ "login": "login",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://login:password@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "pass/word!",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://user%40domain:pass%2Fword%21@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "db/name",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/db%2Fname",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps(
+ {
+ "client": "mysql-connector-python",
+ "ssl_ca": "/path/to/ca",
+ "ssl_cert": "/path/to/cert with space",
+ }
+ ),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema?ssl_ca=%2Fpath%2Fto%2Fca&ssl_cert=%2Fpath%2Fto%2Fcert+with+space",
+ ),
+ ],
+ )
Review Comment:
Thanks, I've updated!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2026148650
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +363,46 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ from urllib.parse import quote_plus
Review Comment:
Sure!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2028103249
##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -86,12 +86,89 @@ def test_dummy_connection_setter(self, mock_connect):
assert kwargs["db"] == "schema"
@mock.patch("MySQLdb.connect")
- def test_get_uri(self, mock_connect):
- self.connection.extra = json.dumps({"charset": "utf-8"})
- self.db_hook.get_conn()
- assert mock_connect.call_count == 1
- args, kwargs = mock_connect.call_args
- assert self.db_hook.get_uri() == "mysql://login:password@host/schema?charset=utf-8"
+ @pytest.mark.parametrize(
+ "connection_params, expected_uri",
+ [
+ (
+ {
+ "login": "login",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://login:password@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "pass/word!",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"charset": "utf-8"}),
+ },
+ "mysql://user%40domain:pass%2Fword%21@host/schema?charset=utf-8",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": None,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "db/name",
+ "port": 3307,
+ "extra": json.dumps({"client": "mysql-connector-python"}),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/db%2Fname",
+ ),
+ (
+ {
+ "login": "user@domain",
+ "password": "password",
+ "host": "host",
+ "schema": "schema",
+ "port": 3307,
+ "extra": json.dumps(
+ {
+ "client": "mysql-connector-python",
+ "ssl_ca": "/path/to/ca",
+ "ssl_cert": "/path/to/cert with space",
+ }
+ ),
+ },
+ "mysql+mysqlconnector://user%40domain:password@host:3307/schema?ssl_ca=%2Fpath%2Fto%2Fca&ssl_cert=%2Fpath%2Fto%2Fcert+with+space",
+ ),
+ ],
+ )
+ def test_get_uri_parametrized(self, mock_connect, connection_params, expected_uri):
Review Comment:
Thanks, I've updated!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "jason810496 (via GitHub)" <gi...@apache.org>.
jason810496 commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2026145582
##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -93,6 +93,35 @@ def test_get_uri(self, mock_connect):
args, kwargs = mock_connect.call_args
assert self.db_hook.get_uri() == "mysql://login:password@host/schema?charset=utf-8"
+ self.connection.login = "user@domain"
+ self.connection.password = "pass/word!"
+ assert self.db_hook.get_uri() == "mysql://user%40domain:pass%2Fword%21@host/schema?charset=utf-8"
+
+ # Test with mysql-connector-python client
+ self.connection.login = "user@domain"
+ self.connection.password = "password"
+ self.connection.extra = json.dumps({"client": "mysql-connector-python"})
+ assert self.db_hook.get_uri() == "mysql+mysqlconnector://user%40domain:password@host/schema"
+
+ self.connection.port = 3307
+ assert self.db_hook.get_uri() == "mysql+mysqlconnector://user%40domain:password@host:3307/schema"
+
+ self.connection.schema = "db/name"
+ assert self.db_hook.get_uri() == "mysql+mysqlconnector://user%40domain:password@host:3307/db%2Fname"
+
+ self.connection.schema = "schema"
+ self.connection.extra = json.dumps(
+ {
+ "client": "mysql-connector-python",
+ "ssl_ca": "/path/to/ca",
+ "ssl_cert": "/path/to/cert with space",
+ }
+ )
+ assert (
+ self.db_hook.get_uri()
+ == "mysql+mysqlconnector://user%40domain:password@host:3307/schema?ssl_ca=%2Fpath%2Fto%2Fca&ssl_cert=%2Fpath%2Fto%2Fcert+with+space"
+ )
+
Review Comment:
How about refactoring the test cases using "pytest.parametrize"?
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +363,46 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ from urllib.parse import quote_plus
Review Comment:
I think we could place the import at top level?
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +363,46 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ from urllib.parse import quote_plus
+
+ conn = self.connection or self.get_connection(self.get_conn_id())
+ conn_schema = self.schema or conn.schema or ""
+ client_name = conn.extra_dejson.get("client", "mysqlclient")
+
+ # Determine URI prefix based on client
+ if client_name == "mysql-connector-python":
+ uri_prefix = "mysql+mysqlconnector://"
+ else: # default: mysqlclient
+ uri_prefix = "mysql://"
+
+ auth_part = ""
+ if conn.login is not None:
+ auth_part += quote_plus(conn.login)
+ if conn.password is not None:
Review Comment:
```suggestion
if conn.password:
```
##########
providers/mysql/src/airflow/providers/mysql/hooks/mysql.py:
##########
@@ -363,3 +363,46 @@ def get_openlineage_database_dialect(self, _):
def get_openlineage_default_schema(self):
"""MySQL has no concept of schema."""
return None
+
+ def get_uri(self) -> str:
+ """Get URI for MySQL connection."""
+ from urllib.parse import quote_plus
+
+ conn = self.connection or self.get_connection(self.get_conn_id())
+ conn_schema = self.schema or conn.schema or ""
+ client_name = conn.extra_dejson.get("client", "mysqlclient")
+
+ # Determine URI prefix based on client
+ if client_name == "mysql-connector-python":
+ uri_prefix = "mysql+mysqlconnector://"
+ else: # default: mysqlclient
+ uri_prefix = "mysql://"
+
+ auth_part = ""
+ if conn.login is not None:
Review Comment:
```suggestion
if conn.login:
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on code in PR #48706:
URL: https://github.com/apache/airflow/pull/48706#discussion_r2026156997
##########
providers/mysql/tests/unit/mysql/hooks/test_mysql.py:
##########
@@ -93,6 +93,35 @@ def test_get_uri(self, mock_connect):
args, kwargs = mock_connect.call_args
assert self.db_hook.get_uri() == "mysql://login:password@host/schema?charset=utf-8"
+ self.connection.login = "user@domain"
+ self.connection.password = "pass/word!"
+ assert self.db_hook.get_uri() == "mysql://user%40domain:pass%2Fword%21@host/schema?charset=utf-8"
+
+ # Test with mysql-connector-python client
+ self.connection.login = "user@domain"
+ self.connection.password = "password"
+ self.connection.extra = json.dumps({"client": "mysql-connector-python"})
+ assert self.db_hook.get_uri() == "mysql+mysqlconnector://user%40domain:password@host/schema"
+
+ self.connection.port = 3307
+ assert self.db_hook.get_uri() == "mysql+mysqlconnector://user%40domain:password@host:3307/schema"
+
+ self.connection.schema = "db/name"
+ assert self.db_hook.get_uri() == "mysql+mysqlconnector://user%40domain:password@host:3307/db%2Fname"
+
+ self.connection.schema = "schema"
+ self.connection.extra = json.dumps(
+ {
+ "client": "mysql-connector-python",
+ "ssl_ca": "/path/to/ca",
+ "ssl_cert": "/path/to/cert with space",
+ }
+ )
+ assert (
+ self.db_hook.get_uri()
+ == "mysql+mysqlconnector://user%40domain:password@host:3307/schema?ssl_ca=%2Fpath%2Fto%2Fca&ssl_cert=%2Fpath%2Fto%2Fcert+with+space"
+ )
+
Review Comment:
That would be much better, thanks~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix: overwrite `get-uri` for `MySQL` [airflow]
Posted by "guan404ming (via GitHub)" <gi...@apache.org>.
guan404ming commented on PR #48706:
URL: https://github.com/apache/airflow/pull/48706#issuecomment-2774462051
Updated, thanks for revieweing~
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org