Converting a south 0.7 migration back to 0.6

I had a minor fight with south earlier today, where someone had created a migration with south 0.7, and I needed it to work with south 0.6.

Needless to say that it would be a pain to manually convert it from the better 0.7 format back into 0.6, so I wrote a fairly small python script, that does the job.

Don’t blame me if the script causes your data to be erased or tables to be dropped. It’s ment as a guideline, although it worked for me out of the box.

YMMV.

Usage: python <south_07to06.py> <path/to/migration.py>

import sys
import re
 
create_table_re = re.compile("db.create_table('([^']+)',")
field_name_re = re.compile("^(s*)('([^']+)', self.gf")
 
cur_model = None
 
input = open(sys.argv[1])
 
for line in input.readlines():
    line = line.rstrip()
    match = create_table_re.search(line)
 
    if line.startswith("class Migration"):
        line = "class Migration():"
    elif line.startswith("from south.v2"):
        continue
    elif 'Meta' in line and 'object_name' in line:
        line = re.sub(r"(?:, )?'object_name': '[^']+'", "", line)
 
    if not cur_model and match:
        cur_model = match.group(1)
    elif cur_model and line.strip() == '))':
        cur_model = None
    elif cur_model:
        field_match = field_name_re.search(line)
 
        if field_match:
            spaces = field_match.group(1)
            field_name = field_match.group(2)
            line = "%s('%s', orm['%s:%s']), " % (spaces, field_name, cur_model.replace("_", "."), field_name)
 
    print line

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">