Skip to content

Commit f4ab6ce

Browse files
committed
🐛 ensure index names do not collide with table names
1 parent 91bd38f commit f4ab6ce

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

mysql_to_sqlite3/transporter.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,17 @@ def _build_create_table_sql(self, table_name: str) -> str:
419419
# check if the index name collides with any table name
420420
self._mysql_cur_dict.execute(
421421
"""
422-
SELECT COUNT(*)
422+
SELECT COUNT(*) AS `count`
423423
FROM information_schema.TABLES
424424
WHERE TABLE_SCHEMA = %s
425425
AND TABLE_NAME = %s
426426
""",
427427
(self._mysql_database, index_name),
428428
)
429-
index_name_collision: t.Optional[t.Dict[str, ToPythonOutputTypes]] = self._mysql_cur_dict.fetchone()
429+
collision: t.Optional[t.Dict[str, ToPythonOutputTypes]] = self._mysql_cur_dict.fetchone()
430+
table_collisions: int = 0
431+
if collision is not None:
432+
table_collisions = int(collision["count"]) # type: ignore[arg-type]
430433

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

0 commit comments

Comments
 (0)