Skip to content

Commit 5749273

Browse files
committed
🐛 fix MySQL connection parameters to handle optional host and port
1 parent 545161c commit 5749273

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/sqlite3_to_mysql/transporter.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def __init__(self, **kwargs: tx.Unpack[SQLite3toMySQLParams]):
8080
raise FileNotFoundError("MySQL socket does not exist")
8181
else:
8282
self._mysql_socket = realpath(str(kwargs.get("mysql_socket")))
83+
self._mysql_host = None
8384
self._mysql_port = None
8485
else:
8586
self._mysql_socket = None
@@ -147,18 +148,24 @@ def __init__(self, **kwargs: tx.Unpack[SQLite3toMySQLParams]):
147148
if not self._mysql_transfer_data and not self._mysql_create_tables:
148149
raise ValueError("Unable to continue without transferring data or creating tables!")
149150

151+
connection_args: t.Dict[str, t.Any] = {
152+
"user": self._mysql_user,
153+
"use_pure": True,
154+
"charset": self._mysql_charset,
155+
"collation": self._mysql_collation,
156+
}
157+
if self._mysql_password is not None:
158+
connection_args["password"] = self._mysql_password
159+
if self._mysql_socket is not None:
160+
connection_args["unix_socket"] = self._mysql_socket
161+
else:
162+
connection_args["host"] = self._mysql_host
163+
connection_args["port"] = self._mysql_port
164+
if not self._mysql_ssl_disabled:
165+
connection_args["ssl_disabled"] = False
166+
150167
try:
151-
_mysql_connection = mysql.connector.connect(
152-
user=self._mysql_user,
153-
password=self._mysql_password,
154-
host=self._mysql_host,
155-
port=self._mysql_port,
156-
unix_socket=self._mysql_socket,
157-
ssl_disabled=self._mysql_ssl_disabled,
158-
use_pure=True,
159-
charset=self._mysql_charset,
160-
collation=self._mysql_collation,
161-
)
168+
_mysql_connection = mysql.connector.connect(**connection_args)
162169
if isinstance(_mysql_connection, mysql.connector.MySQLConnection):
163170
self._mysql = _mysql_connection
164171
else:

src/sqlite3_to_mysql/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class SQLite3toMySQLAttributes:
4848
_without_foreign_keys: bool
4949
_mysql_user: str
5050
_mysql_password: t.Optional[str]
51-
_mysql_host: str
51+
_mysql_host: t.Optional[str]
5252
_mysql_port: t.Optional[int]
5353
_mysql_socket: t.Optional[t.Union[str, "os.PathLike[t.Any]"]]
5454
_mysql_ssl_disabled: bool

0 commit comments

Comments
 (0)