Skip to content

Fix overwrite get-uri for MySQL#48706

Merged
Lee-W merged 15 commits into
apache:mainfrom
guan404ming:add-get-uri-for-mysql
Apr 8, 2025
Merged

Fix overwrite get-uri for MySQL#48706
Lee-W merged 15 commits into
apache:mainfrom
guan404ming:add-get-uri-for-mysql

Conversation

@guan404ming

@guan404ming guan404ming commented Apr 2, 2025

Copy link
Copy Markdown
Member

Related Issue

Towards #38195

Why

The base DbApiHook.get_uri incorrectly assumes the Airflow connection URI (Connection.get_uri()) is a valid database-specific URI. This often fails, especially for MySQL which supports multiple drivers (mysqlclient, mysql-connector-python) requiring different URI schemes and needs proper handling of special characters in credentials, schema, or parameters.

This override provides a correct, MySQL-specific implementation, fixing connection issues caused by improperly formatted or encoded URIs generated by the base method.

How

  • Overrides the inherited DbApiHook.get_uri method within MySqlHook.
  • Determines the correct URI scheme (mysql:// or mysql+mysqlconnector://) based on the client field in the connection's extra parameters.
  • Uses urllib.parse.quote_plus to correctly URL-encode the username, password, schema, and any other parameters passed via extra.
  • Constructs the full MySQL connection URI string, including encoded components and query parameters derived from extra.
  • Adds comprehensive unit tests to validate URI generation for various configurations (different clients, special characters, ports, extra parameters).

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
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.

@guan404ming
guan404ming force-pushed the add-get-uri-for-mysql branch from 8f675aa to f2c55a2 Compare April 2, 2025 18:28
@guan404ming guan404ming changed the title feat: overwrite get-uri for MySQL feat: overwrite get-uri for MySQL Apr 2, 2025
@guan404ming guan404ming changed the title feat: overwrite get-uri for MySQL fix: overwrite get-uri for MySQL Apr 2, 2025
@Lee-W
Lee-W requested review from Lee-W and jason810496 April 3, 2025 03:15

@jason810496 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, left some nits for test cases.

Comment thread providers/mysql/src/airflow/providers/mysql/hooks/mysql.py Outdated
Comment thread providers/mysql/src/airflow/providers/mysql/hooks/mysql.py Outdated
Comment thread providers/mysql/src/airflow/providers/mysql/hooks/mysql.py Outdated
Comment thread providers/mysql/tests/unit/mysql/hooks/test_mysql.py Outdated
Co-Authored-By: LIU ZHE YOU <68415893+jason810496@users.noreply.github.com>
@guan404ming

guan404ming commented Apr 3, 2025

Copy link
Copy Markdown
Member Author

Updated, thanks for reviewing~

@guan404ming
guan404ming requested a review from jason810496 April 3, 2025 04:28

@jason810496 jason810496 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. I left a few suggestions regarding test conventions.

Comment thread providers/mysql/tests/unit/mysql/hooks/test_mysql.py
Comment thread providers/mysql/tests/unit/mysql/hooks/test_mysql.py Outdated
@guan404ming
guan404ming requested a review from jason810496 April 4, 2025 05:30
Comment thread providers/mysql/src/airflow/providers/mysql/hooks/mysql.py Outdated
Comment thread providers/mysql/src/airflow/providers/mysql/hooks/mysql.py Outdated
Comment thread providers/mysql/src/airflow/providers/mysql/hooks/mysql.py Outdated
@guan404ming

Copy link
Copy Markdown
Member Author

Thanks a lots for reviewing, I had learned the convention more and updated with your comments!

@guan404ming
guan404ming requested a review from Lee-W April 4, 2025 10:35
@Lee-W
Lee-W merged commit 7b177a2 into apache:main Apr 8, 2025
@guan404ming
guan404ming deleted the add-get-uri-for-mysql branch April 8, 2025 07:12
simonprydden pushed a commit to simonprydden/airflow that referenced this pull request Apr 8, 2025
* feat: overwrite `get-uri` for MySQL

* refactor: refine `get_uri` and unit test

Co-Authored-By: LIU ZHE YOU <68415893+jason810496@users.noreply.github.com>

* refactor: using `pytest.param` with the `id`

* refactor: apply suggestions from code review

Co-authored-by: Wei Lee <weilee.rx@gmail.com>

* refactor: use `urlencode` to add extra

---------

Co-authored-by: LIU ZHE YOU <68415893+jason810496@users.noreply.github.com>
Co-authored-by: Wei Lee <weilee.rx@gmail.com>
@guan404ming guan404ming changed the title fix: overwrite get-uri for MySQL Fix overwrite get-uri for MySQL Apr 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants