Skip to content

Commit f72a112

Browse files
committed
✨ Add option to truncate existing tables before inserting data
1 parent 89cd387 commit f72a112

File tree

5 files changed

+38
-3
lines changed

5 files changed

+38
-3
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ Options:
5959
UPDATE will update existing rows; IGNORE
6060
will ignore insert errors. Defaults to
6161
IGNORE.
62+
-E, --mysql-truncate-tables Truncates existing tables before inserting
63+
data.
6264
--mysql-integer-type TEXT MySQL default integer field type. Defaults
6365
to INT(11).
6466
--mysql-string-type TEXT MySQL default string field type. Defaults to
6567
VARCHAR(255).
66-
--mysql-text-type [MEDIUMTEXT|TINYTEXT|TEXT|LONGTEXT]
68+
--mysql-text-type [MEDIUMTEXT|TEXT|TINYTEXT|LONGTEXT]
6769
MySQL default text field type. Defaults to
6870
TEXT.
6971
--mysql-charset TEXT MySQL database and table character set

sqlite3_to_mysql/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
__title__ = "sqlite3-to-mysql"
33
__description__ = "A simple Python tool to transfer data from SQLite 3 to MySQL"
44
__url__ = "https://github.com/techouse/sqlite3-to-mysql"
5-
__version__ = "1.4.14"
5+
__version__ = "1.4.15"
66
__author__ = "Klemen Tusar"
77
__author_email__ = "techouse@gmail.com"
88
__license__ = "MIT"

sqlite3_to_mysql/cli.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
help="MySQL insert method. DEFAULT will throw errors when encountering duplicate records; "
7474
"UPDATE will update existing rows; IGNORE will ignore insert errors. Defaults to IGNORE.",
7575
)
76+
@click.option(
77+
"-E",
78+
"--mysql-truncate-tables",
79+
is_flag=True,
80+
help="Truncates existing tables before inserting data.",
81+
)
7682
@click.option(
7783
"--mysql-integer-type",
7884
default="INT(11)",
@@ -140,6 +146,7 @@ def cli(
140146
mysql_port,
141147
skip_ssl,
142148
mysql_insert_method,
149+
mysql_truncate_tables,
143150
mysql_integer_type,
144151
mysql_string_type,
145152
mysql_text_type,
@@ -181,6 +188,7 @@ def cli(
181188
mysql_port=mysql_port,
182189
mysql_ssl_disabled=skip_ssl,
183190
mysql_insert_method=mysql_insert_method,
191+
mysql_truncate_tables=mysql_truncate_tables,
184192
mysql_integer_type=mysql_integer_type,
185193
mysql_string_type=mysql_string_type,
186194
mysql_text_type=mysql_text_type,

sqlite3_to_mysql/debug_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import platform
99
import sqlite3
1010
import sys
11-
from distutils.spawn import find_executable
11+
from distutils.spawn import find_executable # pylint: disable=W0402
1212
from subprocess import check_output
1313

1414
import click

sqlite3_to_mysql/transporter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ def __init__(self, **kwargs):
102102
if self._mysql_insert_method not in MYSQL_INSERT_METHOD:
103103
self._mysql_insert_method = "IGNORE"
104104

105+
self._mysql_truncate_tables = kwargs.get("mysql_truncate_tables") or False
106+
105107
self._mysql_integer_type = str(
106108
kwargs.get("mysql_integer_type") or "INT(11)"
107109
).upper()
@@ -385,6 +387,25 @@ def _create_table(self, table_name, transfer_rowid=False):
385387
)
386388
raise
387389

390+
def _truncate_table(self, table_name):
391+
self._mysql_cur.execute(
392+
"""
393+
SELECT `TABLE_NAME`
394+
FROM `INFORMATION_SCHEMA`.`TABLES`
395+
WHERE `TABLE_SCHEMA` = %s
396+
AND `TABLE_NAME` = %s
397+
LIMIT 1
398+
""",
399+
(self._mysql_database, safe_identifier_length(table_name)),
400+
)
401+
if len(self._mysql_cur.fetchall()) > 0:
402+
self._logger.info("Truncating table %s", safe_identifier_length(table_name))
403+
self._mysql_cur.execute(
404+
"TRUNCATE TABLE `{}`".format(
405+
safe_identifier_length(table_name),
406+
),
407+
)
408+
388409
def _add_indices(self, table_name):
389410
self._sqlite_cur.execute('PRAGMA table_info("{}")'.format(table_name))
390411
table_columns = {}
@@ -678,6 +699,10 @@ def transfer(self):
678699
# create the table
679700
self._create_table(table["name"], transfer_rowid=transfer_rowid)
680701

702+
# truncate the table on request
703+
if self._mysql_truncate_tables:
704+
self._truncate_table(table["name"])
705+
681706
# get the size of the data
682707
self._sqlite_cur.execute(
683708
'SELECT COUNT(*) AS total_records FROM "{}"'.format(table["name"])

0 commit comments

Comments
 (0)