Tags: ,

One of my devlings mistakenly broke the file paths for our image store (or she seems to think it was her), so we needed to go to the backups. I needed to convert the SQL backups to UPDATE statements to fix it. Python 2.3's new csv module made it really easy.

I've just realised I could've done this a lot more elegantly with iterators, but here's what I actually used:

#!/usr/bin/env python

import csv
import re
import sys

class SQLDialect(csv.Dialect):
    delimiter = ','
    quotechar = "'"
    escapechar = '\\'
    doublequote = False
    quoting = csv.QUOTE_NONNUMERIC
    skipinitialspace = True
    lineterminator = '\n'

dialect = SQLDialect()
path_match = re.compile("^/[0-9a-f]/[0-9a-f]$")

for line in sys.stdin:
    values = line.find(' VALUES ')
    line = line[values+9:]
    eor = 0
    while eor != -1:
        eor = line.find("'),(")
        if eor == -1:
            mine = line[:-3]
        else:
            mine = line[0:eor+1]
            line = line[eor+4:]
        cr = csv.reader([mine], dialect)
        data = list(cr)[0]
        print "UPDATE pictures SET file_path = '%(path)s' WHERE id = %(id)d" % ({"id": int(data[0]), "path": data[10]})

blog comments powered by Disqus