Skip to content

Instantly share code, notes, and snippets.

@meeuw
Created February 1, 2015 19:57
Show Gist options
  • Save meeuw/221cc7e7c3f28abe09b8 to your computer and use it in GitHub Desktop.
Save meeuw/221cc7e7c3f28abe09b8 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import json
table = 'table'
with open('output.csvdiff') as f:
diff = json.loads(f.read())
for added in diff["added"]:
print "INSERT INTO %s (%s) VALUES ('%s');" % (table, ", ".join(added.keys()), "', '".join(added.values()))
for changed in diff["changed"]:
fields = []
for field, value in changed["fields"].iteritems():
fields.append("`%s` = '%s'" % (field, value["to"]))
keys = []
for i in range(len(changed["key"])):
keys.append("`%s` = '%s'" % (diff["_index"][i], changed["key"][i]))
print "UPDATE %s SET %s WHERE %s;" % (table, ", ".join(fields), ", ".join(keys))
for removed in diff["removed"]:
keys = []
for i in range(len(diff["_index"])):
keys.append("`%s` = '%s'" % (diff["_index"][i], removed[diff["_index"][i]]))
print "DELETE FROM %s WHERE %s;" % (table, ", ".join(keys))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment