Skip to content

Commit

Permalink
Add collation support in profiles.yml / Fix incorrect collation for u…
Browse files Browse the repository at this point in the history
…tf8mb4 (#173)

* Add support for charset and collation in profile.yml

* Change mysql-connect-python version

* Update readme with new profile options

* Update setup.py with less restricting mysql-connector-python version after code review

Co-authored-by: Matthew Wallace <[email protected]>

* Add collation and charset support for mariadb and mysql5

---------

Co-authored-by: Matthew Wallace <[email protected]>
  • Loading branch information
kzajaczkowski and mwallace582 authored Apr 26, 2024
1 parent 1ad3bf1 commit 149157d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,21 @@ your_profile_name:
username: your_mysql_username
password: your_mysql_password
ssl_disabled: True
charset: utf8mb4
collation: utf8mb4_0900_ai_ci
```

| Option | Description | Required? | Example |
| --------------- | ----------------------------------------------------------------------------------- | ------------------------------------------------------------------ | ---------------------------------------------- |
| type | The specific adapter to use | Required | `mysql`, `mysql5` or `mariadb` |
| type | The specific adapter to use | Required | `mysql`, `mysql5` or `mariadb` |
| server | The server (hostname) to connect to | Required | `yourorg.mysqlhost.com` |
| port | The port to use | Optional | `3306` |
| schema | Specify the schema (database) to build models into | Required | `analytics` |
| username | The username to use to connect to the server | Required | `dbt_admin` |
| password | The password to use for authenticating to the server | Required | `correct-horse-battery-staple` |
| ssl_disabled | Set to enable or disable TLS connectivity to mysql5.x | Optional | `True` or `False` |
| charset | Specify charset to be used by a connection | Optional | `utf8mb4` |
| collation | Set to enable or disable TLS connectivity to mysql5.x | Optional | `utf8mb4_0900_ai_ci` |

### Notes

Expand Down
7 changes: 7 additions & 0 deletions dbt/adapters/mariadb/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MariaDBCredentials(Credentials):
password: Optional[str] = None
charset: Optional[str] = None
ssl_disabled: Optional[bool] = None
collation: Optional[str] = None

_ALIASES = {
"UID": "username",
Expand Down Expand Up @@ -98,6 +99,12 @@ def open(cls, connection):
if credentials.port:
kwargs["port"] = credentials.port

if credentials.charset:
kwargs["charset"] = credentials.charset

if credentials.collation:
kwargs["collation"] = credentials.collation

try:
connection.handle = mysql.connector.connect(**kwargs)
connection.state = "open"
Expand Down
7 changes: 7 additions & 0 deletions dbt/adapters/mysql/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class MySQLCredentials(Credentials):
username: Optional[str] = None
password: Optional[str] = None
charset: Optional[str] = None
collation: Optional[str] = None

_ALIASES = {
"UID": "username",
Expand Down Expand Up @@ -94,6 +95,12 @@ def open(cls, connection):
if credentials.port:
kwargs["port"] = credentials.port

if credentials.charset:
kwargs["charset"] = credentials.charset

if credentials.collation:
kwargs["collation"] = credentials.collation

try:
connection.handle = mysql.connector.connect(**kwargs)
connection.state = "open"
Expand Down
7 changes: 7 additions & 0 deletions dbt/adapters/mysql5/connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class MySQLCredentials(Credentials):
password: Optional[str] = None
charset: Optional[str] = None
ssl_disabled: Optional[bool] = None
collation: Optional[str] = None

_ALIASES = {
"UID": "username",
Expand Down Expand Up @@ -98,6 +99,12 @@ def open(cls, connection):
if credentials.port:
kwargs["port"] = credentials.port

if credentials.charset:
kwargs["charset"] = credentials.charset

if credentials.collation:
kwargs["collation"] = credentials.collation

try:
connection.handle = mysql.connector.connect(**kwargs)
connection.state = "open"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def _get_dbt_core_version():
include_package_data=True,
install_requires=[
"dbt-core~={}".format(dbt_core_version),
"mysql-connector-python>=8.0.0,<8.1",
"mysql-connector-python>=8.0.0",
],
zip_safe=False,
classifiers=[
Expand Down

0 comments on commit 149157d

Please sign in to comment.