Skip to content

Commit

Permalink
🐛 fix #125
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Aug 28, 2024
1 parent bf3bdaf commit cd08aa4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/sqlite3_to_mysql/transporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SQLite3toMySQL(SQLite3toMySQLAttributes):

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

MYSQL_CONNECTOR_VERSION: version.Version = version.parse(mysql_connector_version_string)
Expand Down Expand Up @@ -291,6 +292,8 @@ def _translate_type_from_sqlite_to_mysql(self, column_type: str) -> str:
if data_type.startswith(("BIGINT", "INT8")):
return f"BIGINT{self._column_type_length(column_type)}{' UNSIGNED' if unsigned else ''}"
if data_type.startswith(("INT64", "NUMERIC")):
if data_type == "NUMERIC" and self._column_type_precision_and_scale(full_column_type) != "":
return f"DECIMAL{self._column_type_precision_and_scale(column_type)}{' UNSIGNED' if unsigned else ''}"
return f"BIGINT{self._column_type_length(column_type, 19)}{' UNSIGNED' if unsigned else ''}"
if data_type.startswith(("INTEGER", "INT")):
length = self._column_type_length(column_type)
Expand Down Expand Up @@ -320,6 +323,13 @@ def _column_type_length(cls, column_type: str, default: t.Optional[t.Union[str,
return f"({default})"
return ""

@classmethod
def _column_type_precision_and_scale(cls, column_type: str) -> str:
suffix: t.Optional[t.Match[str]] = cls.COLUMN_PRECISION_AND_SCALE_PATTERN.search(column_type)
if suffix:
return suffix.group(0)
return ""

def _create_table(self, table_name: str, transfer_rowid: bool = False) -> None:
primary_keys: t.List[t.Dict[str, str]] = []

Expand Down
1 change: 1 addition & 0 deletions tests/unit/sqlite3_to_mysql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ def test_translate_type_from_sqlite_to_mysql_all_valid_columns(
("INT8(19)", "BIGINT(19)"),
("INT8(19) UNSIGNED", "BIGINT(19) UNSIGNED"),
("NUMERIC", "BIGINT(19)"),
("NUMERIC(10,5)", "DECIMAL(10,5)"),
("DOUBLE", "DOUBLE"),
("DOUBLE UNSIGNED", "DOUBLE UNSIGNED"),
("DOUBLE(10,5)", "DOUBLE(10,5)"),
Expand Down

0 comments on commit cd08aa4

Please sign in to comment.