From f0c1f9cf2cc491553e34bd8f75e9d29be6fc781f Mon Sep 17 00:00:00 2001 From: Isaac Garza Date: Wed, 7 Mar 2018 11:13:54 -0800 Subject: [PATCH 1/3] Grab SQL key headers Reads the keys under the CREATE SQL statement and writes it out first to the .csv file --- mysqldump_to_csv.py | 35 +++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/mysqldump_to_csv.py b/mysqldump_to_csv.py index b49cfe7..448c32d 100644 --- a/mysqldump_to_csv.py +++ b/mysqldump_to_csv.py @@ -17,13 +17,32 @@ def is_insert(line): """ return line.startswith('INSERT INTO') or False - -def get_values(line): +def get_insert_values(line): """ Returns the portion of an INSERT statement containing values """ return line.partition('` VALUES ')[2] +def get_create_keys(fileinput): + """ + Returns all of the value keys within the CREATE statement. + """ + reading_keys = False + keys = [] + for line in fileinput: + if line.startswith('CREATE TABLE'): + reading_keys = True + continue + + elif line.startswith(' PRIMARY KEY'): + reading_keys = False + break + + if reading_keys: + new_key = line.partition("`")[2].partition("`")[0] + keys.append(new_key) + return keys + def values_sanity_check(values): """ @@ -35,7 +54,7 @@ def values_sanity_check(values): return True -def parse_values(values, outfile): +def parse_values(values, outfile, keys=None): """ Given a file handle and the raw values from a MySQL INSERT statement, write the equivalent CSV to the file @@ -50,6 +69,8 @@ def parse_values(values, outfile): ) writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL) + if keys: + writer.writerow(keys) for reader_row in reader: for column in reader_row: # If our current string is empty... @@ -100,13 +121,15 @@ def main(): # Iterate over all lines in all files # listed in sys.argv[1:] # or stdin if no args given. + inp = fileinput.input() + keys = get_create_keys(inp) try: - for line in fileinput.input(): + for line in inp: # Look for an INSERT statement and parse it. if is_insert(line): - values = get_values(line) + values = get_insert_values(line) if values_sanity_check(values): - parse_values(values, sys.stdout) + parse_values(values, sys.stdout, keys) except KeyboardInterrupt: sys.exit(0) From cdcf82a44a20637f8b196a1e0dcc12943a71c833 Mon Sep 17 00:00:00 2001 From: Isaac Garza Date: Thu, 8 Mar 2018 10:40:51 -0800 Subject: [PATCH 2/3] Created write_keys() Created standalone write_keys() function to avoid writing the keys row more than once --- mysqldump_to_csv.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/mysqldump_to_csv.py b/mysqldump_to_csv.py index 448c32d..91dc015 100644 --- a/mysqldump_to_csv.py +++ b/mysqldump_to_csv.py @@ -53,8 +53,15 @@ def values_sanity_check(values): # Assertions have not been raised return True +def write_keys(keys, outfile): + """ + Given a file handle and the raw keys from a MySQL CREATE + statement, write the equivalent CSV to the file + """ + writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL) + writer.writerow(keys) -def parse_values(values, outfile, keys=None): +def parse_values(values, outfile): """ Given a file handle and the raw values from a MySQL INSERT statement, write the equivalent CSV to the file @@ -69,8 +76,6 @@ def parse_values(values, outfile, keys=None): ) writer = csv.writer(outfile, quoting=csv.QUOTE_MINIMAL) - if keys: - writer.writerow(keys) for reader_row in reader: for column in reader_row: # If our current string is empty... @@ -123,13 +128,14 @@ def main(): # or stdin if no args given. inp = fileinput.input() keys = get_create_keys(inp) + write_keys(keys, sys.stdout) try: for line in inp: # Look for an INSERT statement and parse it. if is_insert(line): values = get_insert_values(line) if values_sanity_check(values): - parse_values(values, sys.stdout, keys) + parse_values(values, sys.stdout) except KeyboardInterrupt: sys.exit(0) From 7b86847385180aa517022c55245e82cc888ca1a3 Mon Sep 17 00:00:00 2001 From: Isaac Garza Date: Thu, 8 Mar 2018 12:12:28 -0800 Subject: [PATCH 3/3] Covering edge case with no 'PRIMARY KEY' --- mysqldump_to_csv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mysqldump_to_csv.py b/mysqldump_to_csv.py index 91dc015..2775568 100644 --- a/mysqldump_to_csv.py +++ b/mysqldump_to_csv.py @@ -34,7 +34,7 @@ def get_create_keys(fileinput): reading_keys = True continue - elif line.startswith(' PRIMARY KEY'): + elif line.startswith(' PRIMARY KEY') or line.startswith(' KEY'): reading_keys = False break