Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/sqlite3_to_mysql/transporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class SQLite3toMySQL(SQLite3toMySQLAttributes):
r"^(datetime|date|time)\s*\(\s*'now'(?:\s*,\s*'(localtime|utc)')?\s*\)$",
re.IGNORECASE,
)
SQLITE_CURRENT_TS_FUNC: t.Pattern[str] = re.compile(
r"^(datetime|date|time)\s*\(\s*current_timestamp(?:\s*,\s*'(localtime|utc)')?\s*\)$",
re.IGNORECASE,
)
STRFTIME_NOW: t.Pattern[str] = re.compile(
r"^strftime\s*\(\s*'([^']+)'\s*,\s*'now'(?:\s*,\s*'(localtime|utc)')?\s*\)$",
re.IGNORECASE,
Expand Down Expand Up @@ -577,6 +581,10 @@ def _translate_default_for_mysql(self, column_type: str, default: str) -> str:

s: str = self._strip_wrapping_parentheses(raw)
u: str = s.upper()
sqlite_current_ts_match: t.Optional[re.Match[str]] = self.SQLITE_CURRENT_TS_FUNC.match(s)
sqlite_current_ts_func: t.Optional[str] = (
sqlite_current_ts_match.group(1).lower() if sqlite_current_ts_match else None
)

# NULL passthrough
if u == "NULL":
Expand All @@ -590,6 +598,7 @@ def _translate_default_for_mysql(self, column_type: str, default: str) -> str:
if base.startswith("TIMESTAMP") and (
self.CURRENT_TS.match(s)
or (self.SQLITE_NOW_FUNC.match(s) and s.lower().startswith("datetime"))
or sqlite_current_ts_func == "datetime"
or self.STRFTIME_NOW.match(s)
):
len_match: t.Optional[re.Match[str]] = self.COLUMN_LENGTH_PATTERN.search(column_type)
Expand All @@ -609,6 +618,7 @@ def _translate_default_for_mysql(self, column_type: str, default: str) -> str:
if base.startswith("DATETIME") and (
self.CURRENT_TS.match(s)
or (self.SQLITE_NOW_FUNC.match(s) and s.lower().startswith("datetime"))
or sqlite_current_ts_func == "datetime"
or self.STRFTIME_NOW.match(s)
):
if not self._allow_current_ts_dt:
Expand All @@ -633,6 +643,7 @@ def _translate_default_for_mysql(self, column_type: str, default: str) -> str:
self.CURRENT_DATE.match(s)
or self.CURRENT_TS.match(s) # map CURRENT_TIMESTAMP → CURRENT_DATE for DATE
or (self.SQLITE_NOW_FUNC.match(s) and s.lower().startswith("date"))
or sqlite_current_ts_func == "date"
or self.STRFTIME_NOW.match(s)
)
and self._allow_expr_defaults
Expand All @@ -647,6 +658,7 @@ def _translate_default_for_mysql(self, column_type: str, default: str) -> str:
self.CURRENT_TIME.match(s)
or self.CURRENT_TS.match(s) # map CURRENT_TIMESTAMP → CURRENT_TIME for TIME
or (self.SQLITE_NOW_FUNC.match(s) and s.lower().startswith("time"))
or sqlite_current_ts_func == "time"
or self.STRFTIME_NOW.match(s)
)
and self._allow_expr_defaults
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/sqlite3_to_mysql_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,19 @@ def _mk(*, expr: bool, ts_dt: bool, fsp: bool) -> SQLite3toMySQL:
("TIMESTAMP(3)", "CURRENT_TIMESTAMP", {"expr": False, "ts_dt": True, "fsp": True}, "CURRENT_TIMESTAMP(3)"),
# SQLite-style now -> map to CURRENT_TIMESTAMP (with FSP when allowed)
("DATETIME(2)", "datetime('now')", {"expr": False, "ts_dt": True, "fsp": True}, "CURRENT_TIMESTAMP(2)"),
# datetime(current_timestamp, ...) should also map to CURRENT/UTC TIMESTAMP
(
"DATETIME",
"datetime(current_timestamp, 'localtime')",
{"expr": False, "ts_dt": True, "fsp": False},
"CURRENT_TIMESTAMP",
),
(
"DATETIME(3)",
"datetime(current_timestamp, 'utc')",
{"expr": False, "ts_dt": True, "fsp": True},
"UTC_TIMESTAMP(3)",
),
# --- DATE mapping (from 'now' forms or CURRENT_TIMESTAMP) ---
# Only map when expression defaults are allowed
("DATE", "datetime('now')", {"expr": True, "ts_dt": False, "fsp": False}, "CURRENT_DATE"),
Expand Down
Loading