Skip to content

Commit aac307d

Browse files
committed
🚑 Fix #46
1 parent 04f9887 commit aac307d

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

sqlite3_to_mysql/transporter.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class SQLite3toMySQL:
5353
COLUMN_PATTERN = re.compile(r"^[^(]+")
5454
COLUMN_LENGTH_PATTERN = re.compile(r"\(\d+\)$")
5555

56+
MYSQL_CONNECTOR_VERSION = version.parse(mysql_connector_version_string)
57+
5658
def __init__(self, **kwargs):
5759
"""Constructor."""
5860
if not kwargs.get("sqlite_file"):
@@ -122,15 +124,39 @@ def __init__(self, **kwargs):
122124

123125
self._mysql_charset = kwargs.get("mysql_charset") or "utf8mb4"
124126

125-
self._mysql_collation = (
126-
kwargs.get("mysql_collation")
127-
or CharacterSet.get_default_collation(self._mysql_charset.lower())[0]
128-
)
129-
if (
130-
not kwargs.get("mysql_collation")
131-
and self._mysql_collation == "utf8mb4_0900_ai_ci"
132-
):
133-
self._mysql_collation = "utf8mb4_general_ci"
127+
try:
128+
self._mysql_collation = (
129+
kwargs.get("mysql_collation")
130+
or CharacterSet.get_default_collation(self._mysql_charset.lower())[0]
131+
)
132+
if (
133+
not kwargs.get("mysql_collation")
134+
and self._mysql_collation == "utf8mb4_0900_ai_ci"
135+
):
136+
self._mysql_collation = "utf8mb4_general_ci"
137+
except mysql.connector.ProgrammingError:
138+
if (
139+
self.MYSQL_CONNECTOR_VERSION.major >= 8
140+
and self.MYSQL_CONNECTOR_VERSION.micro >= 30
141+
):
142+
# Looks like we're dealing with mysql-connector-python >= 8.0.30 and MySQL 5.7 or MariaDB
143+
# https://github.com/techouse/sqlite3-to-mysql/issues/46
144+
# https://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-30.html
145+
self._logger.warning(
146+
"Looks like you're using mysql-connector-python >= 8.0.30 with an older version of MySQL."
147+
)
148+
CharacterSet.set_mysql_version((5, 7))
149+
self._mysql_collation = (
150+
kwargs.get("mysql_collation")
151+
or CharacterSet.get_default_collation(self._mysql_charset.lower())[
152+
0
153+
]
154+
)
155+
if (
156+
not kwargs.get("mysql_collation")
157+
and self._mysql_collation == "utf8mb4_0900_ai_ci"
158+
):
159+
self._mysql_collation = "utf8mb4_general_ci"
134160

135161
self.ignore_duplicate_keys = kwargs.get("ignore_duplicate_keys") or False
136162

@@ -193,14 +219,13 @@ def _connect(self, retried_mysql_57=False):
193219
)
194220
except mysql.connector.ProgrammingError as err:
195221
if not retried_mysql_57:
196-
# Looks like we're dealing with mysql-connector-python >= 8.0.30 and MySQL 5.7 or MariaDB
197-
# https://github.com/techouse/sqlite3-to-mysql/issues/46
198-
# https://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-30.html
199-
mysql_connector_version = version.parse(mysql_connector_version_string)
200222
if (
201-
mysql_connector_version.major >= 8
202-
and mysql_connector_version.micro >= 30
223+
self.MYSQL_CONNECTOR_VERSION.major >= 8
224+
and self.MYSQL_CONNECTOR_VERSION.micro >= 30
203225
):
226+
# Looks like we're dealing with mysql-connector-python >= 8.0.30 and MySQL 5.7 or MariaDB
227+
# https://github.com/techouse/sqlite3-to-mysql/issues/46
228+
# https://dev.mysql.com/doc/relnotes/connector-python/en/news-8-0-30.html
204229
self._logger.warning(
205230
"Looks like you're using mysql-connector-python >= 8.0.30 with an older version of MySQL."
206231
)

0 commit comments

Comments
 (0)