Skip to content

Instantly share code, notes, and snippets.

@Dem0n3D
Last active August 29, 2015 14:09
Show Gist options
  • Save Dem0n3D/b79c0200e53b3d74a2a4 to your computer and use it in GitHub Desktop.
Save Dem0n3D/b79c0200e53b3d74a2a4 to your computer and use it in GitHub Desktop.
import re
import sys
import subprocess
def file_len(fname):
p = subprocess.Popen(['grep', fname, '-e', 'INSERT', '-c'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result, err = p.communicate()
if p.returncode != 0:
raise IOError(err)
return int(result.strip().split()[0])
lcount = file_len(sys.argv[1])
fin = open(sys.argv[1], 'r')
fout = open(sys.argv[2], 'w')
s = '\n'.join(fin.readlines())
inserts = {}
tables = {}
j = 1
for g in re.finditer('INSERT INTO `(.*)` VALUES\s?(\(.*?\));\n', s):
t = g.group(1)
inserts[t] = inserts[t] + [ g.group(2) ] if t in inserts.keys() else [ g.group(2) ]
print(100*j/lcount)
j += 1
for g in re.finditer('CREATE TABLE `(.*)` \((.*?)\);\n', s):
tables[g.group(1)] = g.group(2)
j = 1
print tables
print inserts
for t in tables:
fout.write('CREATE TABLE `{table}` ({expr});\n'.format(table=t, expr=tables[t]))
for i in xrange(len(inserts[t])):
if(i%100000 == 0):
fout.write('INSERT INTO `{table}` VALUES '.format(table=t))
fout.write(inserts[t][i])
fout.write(';\n' if ((i+1)%100000 == 0 or i == len(inserts[t])-1) else ', ')
print(100*j/lcount)
j += 1
fout.write('\n\n')
fout.close()
fin.close()
@Dem0n3D
Copy link
Author

Dem0n3D commented Nov 18, 2014

Converts MySQL dump from simple INSERTs to extended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment