@@ -53,6 +53,8 @@ class SQLite3toMySQL:
53
53
COLUMN_PATTERN = re .compile (r"^[^(]+" )
54
54
COLUMN_LENGTH_PATTERN = re .compile (r"\(\d+\)$" )
55
55
56
+ MYSQL_CONNECTOR_VERSION = version .parse (mysql_connector_version_string )
57
+
56
58
def __init__ (self , ** kwargs ):
57
59
"""Constructor."""
58
60
if not kwargs .get ("sqlite_file" ):
@@ -122,15 +124,39 @@ def __init__(self, **kwargs):
122
124
123
125
self ._mysql_charset = kwargs .get ("mysql_charset" ) or "utf8mb4"
124
126
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"
134
160
135
161
self .ignore_duplicate_keys = kwargs .get ("ignore_duplicate_keys" ) or False
136
162
@@ -193,14 +219,13 @@ def _connect(self, retried_mysql_57=False):
193
219
)
194
220
except mysql .connector .ProgrammingError as err :
195
221
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 )
200
222
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
203
225
):
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
204
229
self ._logger .warning (
205
230
"Looks like you're using mysql-connector-python >= 8.0.30 with an older version of MySQL."
206
231
)
0 commit comments