Skip to content

Commit cd08aa4

Browse files
committed
🐛 fix #125
1 parent bf3bdaf commit cd08aa4

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/sqlite3_to_mysql/transporter.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class SQLite3toMySQL(SQLite3toMySQLAttributes):
4949

5050
COLUMN_PATTERN: t.Pattern[str] = re.compile(r"^[^(]+")
5151
COLUMN_LENGTH_PATTERN: t.Pattern[str] = re.compile(r"\(\d+\)")
52+
COLUMN_PRECISION_AND_SCALE_PATTERN: t.Pattern[str] = re.compile(r"\(\d+,\d+\)")
5253
COLUMN_UNSIGNED_PATTERN: t.Pattern[str] = re.compile(r"\bUNSIGNED\b", re.IGNORECASE)
5354

5455
MYSQL_CONNECTOR_VERSION: version.Version = version.parse(mysql_connector_version_string)
@@ -291,6 +292,8 @@ def _translate_type_from_sqlite_to_mysql(self, column_type: str) -> str:
291292
if data_type.startswith(("BIGINT", "INT8")):
292293
return f"BIGINT{self._column_type_length(column_type)}{' UNSIGNED' if unsigned else ''}"
293294
if data_type.startswith(("INT64", "NUMERIC")):
295+
if data_type == "NUMERIC" and self._column_type_precision_and_scale(full_column_type) != "":
296+
return f"DECIMAL{self._column_type_precision_and_scale(column_type)}{' UNSIGNED' if unsigned else ''}"
294297
return f"BIGINT{self._column_type_length(column_type, 19)}{' UNSIGNED' if unsigned else ''}"
295298
if data_type.startswith(("INTEGER", "INT")):
296299
length = self._column_type_length(column_type)
@@ -320,6 +323,13 @@ def _column_type_length(cls, column_type: str, default: t.Optional[t.Union[str,
320323
return f"({default})"
321324
return ""
322325

326+
@classmethod
327+
def _column_type_precision_and_scale(cls, column_type: str) -> str:
328+
suffix: t.Optional[t.Match[str]] = cls.COLUMN_PRECISION_AND_SCALE_PATTERN.search(column_type)
329+
if suffix:
330+
return suffix.group(0)
331+
return ""
332+
323333
def _create_table(self, table_name: str, transfer_rowid: bool = False) -> None:
324334
primary_keys: t.List[t.Dict[str, str]] = []
325335

tests/unit/sqlite3_to_mysql_test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def test_translate_type_from_sqlite_to_mysql_all_valid_columns(
171171
("INT8(19)", "BIGINT(19)"),
172172
("INT8(19) UNSIGNED", "BIGINT(19) UNSIGNED"),
173173
("NUMERIC", "BIGINT(19)"),
174+
("NUMERIC(10,5)", "DECIMAL(10,5)"),
174175
("DOUBLE", "DOUBLE"),
175176
("DOUBLE UNSIGNED", "DOUBLE UNSIGNED"),
176177
("DOUBLE(10,5)", "DOUBLE(10,5)"),

0 commit comments

Comments
 (0)