Skip to content

Commit

Permalink
🐛 ensure index names do not collide with table names
Browse files Browse the repository at this point in the history
  • Loading branch information
techouse committed Jan 14, 2024
1 parent 91bd38f commit f4ab6ce
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions mysql_to_sqlite3/transporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,14 +419,17 @@ def _build_create_table_sql(self, table_name: str) -> str:
# check if the index name collides with any table name
self._mysql_cur_dict.execute(
"""
SELECT COUNT(*)
SELECT COUNT(*) AS `count`
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = %s
AND TABLE_NAME = %s
""",
(self._mysql_database, index_name),
)
index_name_collision: t.Optional[t.Dict[str, ToPythonOutputTypes]] = self._mysql_cur_dict.fetchone()
collision: t.Optional[t.Dict[str, ToPythonOutputTypes]] = self._mysql_cur_dict.fetchone()
table_collisions: int = 0
if collision is not None:
table_collisions = int(collision["count"]) # type: ignore[arg-type]

columns: str = ""
if isinstance(index["columns"], bytes):
Expand All @@ -443,7 +446,7 @@ def _build_create_table_sql(self, table_name: str) -> str:
indices += """CREATE {unique} INDEX IF NOT EXISTS "{name}" ON "{table}" ({columns});""".format(
unique="UNIQUE" if index["unique"] in {1, "1"} else "",
name=f"{table_name}_{index_name}"
if (index_name_collision is not None or self._prefix_indices)
if (table_collisions > 0 or self._prefix_indices)
else index_name,
table=table_name,
columns=", ".join(f'"{column}"' for column in columns.split(",")),
Expand Down

0 comments on commit f4ab6ce

Please sign in to comment.