Skip to content

Instantly share code, notes, and snippets.

@madssj
Last active December 18, 2015 01:29
Show Gist options
  • Save madssj/5704513 to your computer and use it in GitHub Desktop.
Save madssj/5704513 to your computer and use it in GitHub Desktop.
South migration convert from 2 to 1.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment