Skip to content

Commit e4de815

Browse files
authored
Merge pull request #29 from techouse/exclude-mysql-tables
✨ Add option to exclude specific MySQL tables (#27)
2 parents 8558f1c + dcab766 commit e4de815

File tree

5 files changed

+338
-45
lines changed

5 files changed

+338
-45
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,15 @@ Options:
4141
-t, --mysql-tables TUPLE Transfer only these specific tables (space
4242
separated table names). Implies --without-
4343
foreign-keys which inhibits the transfer of
44-
foreign keys.
44+
foreign keys. Can not be used together with
45+
--exclude-mysql-tables.
46+
47+
-e, --exclude-mysql-tables TUPLE
48+
Transfer all tables except these specific
49+
tables (space separated table names).
50+
Implies --without-foreign-keys which
51+
inhibits the transfer of foreign keys. Can
52+
not be used together with --mysql-tables.
4553
4654
-L, --limit-rows INTEGER Transfer only a limited number of rows from
4755
each table.

mysql_to_sqlite3/cli.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,17 @@
3838
type=tuple,
3939
cls=OptionEatAll,
4040
help="Transfer only these specific tables (space separated table names). "
41-
"Implies --without-foreign-keys which inhibits the transfer of foreign keys.",
41+
"Implies --without-foreign-keys which inhibits the transfer of foreign keys. "
42+
"Can not be used together with --exclude-mysql-tables.",
43+
)
44+
@click.option(
45+
"-e",
46+
"--exclude-mysql-tables",
47+
type=tuple,
48+
cls=OptionEatAll,
49+
help="Transfer all tables except these specific tables (space separated table names). "
50+
"Implies --without-foreign-keys which inhibits the transfer of foreign keys. "
51+
"Can not be used together with --mysql-tables.",
4252
)
4353
@click.option(
4454
"-L",
@@ -123,6 +133,7 @@ def cli(
123133
mysql_password,
124134
mysql_database,
125135
mysql_tables,
136+
exclude_mysql_tables,
126137
limit_rows,
127138
collation,
128139
prefix_indices,
@@ -141,12 +152,18 @@ def cli(
141152
):
142153
"""Transfer MySQL to SQLite using the provided CLI options."""
143154
try:
155+
if mysql_tables and exclude_mysql_tables:
156+
raise click.UsageError(
157+
"Illegal usage: --mysql-tables and --exclude-mysql-tables are mutually exclusive!"
158+
)
159+
144160
converter = MySQLtoSQLite(
145161
sqlite_file=sqlite_file,
146162
mysql_user=mysql_user,
147163
mysql_password=mysql_password or prompt_mysql_password,
148164
mysql_database=mysql_database,
149165
mysql_tables=mysql_tables,
166+
exclude_mysql_tables=exclude_mysql_tables,
150167
limit_rows=limit_rows,
151168
collation=collation,
152169
prefix_indices=prefix_indices,

mysql_to_sqlite3/transporter.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,17 @@ def __init__(self, **kwargs):
5252
else tuple()
5353
)
5454

55+
self._exclude_mysql_tables = (
56+
tuple(kwargs.get("exclude_mysql_tables"))
57+
if kwargs.get("exclude_mysql_tables") is not None
58+
else tuple()
59+
)
60+
61+
if len(self._mysql_tables) > 0 and len(self._exclude_mysql_tables) > 0:
62+
raise ValueError(
63+
"mysql_tables and exclude_mysql_tables are mutually exclusive"
64+
)
65+
5566
self._limit_rows = int(kwargs.get("limit_rows") or 0)
5667

5768
if kwargs.get("collation") is not None and kwargs.get("collation").upper() in {
@@ -67,7 +78,7 @@ def __init__(self, **kwargs):
6778

6879
self._without_foreign_keys = (
6980
True
70-
if len(self._mysql_tables) > 0
81+
if len(self._mysql_tables) > 0 or len(self._exclude_mysql_tables) > 0
7182
else (kwargs.get("without_foreign_keys") or False)
7283
)
7384

@@ -493,19 +504,25 @@ def _transfer_table_data(
493504

494505
def transfer(self):
495506
"""The primary and only method with which we transfer all the data."""
496-
if len(self._mysql_tables) > 0:
507+
if len(self._mysql_tables) > 0 or len(self._exclude_mysql_tables) > 0:
497508
# transfer only specific tables
509+
specific_tables = (
510+
self._exclude_mysql_tables
511+
if len(self._exclude_mysql_tables) > 0
512+
else self._mysql_tables
513+
)
498514

499515
self._mysql_cur_prepared.execute(
500516
"""
501517
SELECT TABLE_NAME
502518
FROM information_schema.TABLES
503519
WHERE TABLE_SCHEMA = SCHEMA()
504-
AND TABLE_NAME IN ({placeholders})
520+
AND TABLE_NAME {exclude} IN ({placeholders})
505521
""".format(
506-
placeholders=("%s, " * len(self._mysql_tables)).rstrip(" ,")
522+
exclude="NOT" if len(self._exclude_mysql_tables) > 0 else "",
523+
placeholders=("%s, " * len(specific_tables)).rstrip(" ,"),
507524
),
508-
self._mysql_tables,
525+
specific_tables,
509526
)
510527
tables = (row[0] for row in self._mysql_cur_prepared.fetchall())
511528
else:

0 commit comments

Comments
 (0)