Skip to content
Merged
Changes from 1 commit
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
16 changes: 5 additions & 11 deletions drt/destinations/mysql.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,7 @@ def get_row_count(self, config: DestinationConfig) -> int:
conn = self._connect(config)
try:
cur = conn.cursor()
# Escape table name with backticks for safety
escaped_table = (
"`.`".join(config.table.split("."))
if "." in config.table
else config.table
)
cur.execute(f"SELECT COUNT(*) FROM `{escaped_table}`")
cur.execute(f"SELECT COUNT(*) FROM {self._quote_ident(config.table)}")
row = cur.fetchone()
return row[0] if row else 0
finally:
Expand Down Expand Up @@ -187,7 +181,7 @@ def _load_replace(
result = SyncResult()

if not self._replace_truncated:
cur.execute(f"TRUNCATE TABLE `{table}`")
cur.execute(f"TRUNCATE TABLE {self._quote_ident(table)}")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

self._replace_truncated = True

sql = self._build_insert_sql(table, columns)
Expand Down Expand Up @@ -350,7 +344,7 @@ def _build_insert_sql(table: str, columns: list[str]) -> str:
"""Build plain INSERT SQL (no conflict handling)."""
cols_str = ", ".join(f"`{c}`" for c in columns)
placeholders = ", ".join(["%s"] * len(columns))
return f"INSERT INTO `{table}` ({cols_str}) VALUES ({placeholders})"
return f"INSERT INTO {MySQLDestination._quote_ident(table)} ({cols_str}) VALUES ({placeholders})"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback


@staticmethod
def _build_upsert_sql(
Expand All @@ -365,11 +359,11 @@ def _build_upsert_sql(
if update_cols:
set_clause = ", ".join(f"`{c}` = VALUES(`{c}`)" for c in update_cols)
return (
f"INSERT INTO `{table}` ({cols_str}) VALUES ({placeholders}) "
f"INSERT INTO {MySQLDestination._quote_ident(table)} ({cols_str}) VALUES ({placeholders}) "
f"ON DUPLICATE KEY UPDATE {set_clause}"
)
# All columns are part of the key — just ignore duplicates
return f"INSERT IGNORE INTO `{table}` ({cols_str}) VALUES ({placeholders})"
return f"INSERT IGNORE INTO {MySQLDestination._quote_ident(table)} ({cols_str}) VALUES ({placeholders})"

@staticmethod
def _connect(config: MySQLDestinationConfig) -> Any:
Expand Down
Loading