Created
May 18, 2014 18:50
-
-
Save meeuw/b9a3b4e9696cebbf1f2a to your computer and use it in GitHub Desktop.
convert doctrine annotations to alter table / rename table sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import fileinput | |
import re | |
import json | |
with open('cols.json') as f: cols = json.load(f) | |
oldtablename = None | |
for line in fileinput.input(): | |
if '@ORM\Table' in line: | |
m = re.match(r'.*Table\(name="([^"]*).*', line) | |
oldtablename = m.group(1) | |
if oldtablename and line.startswith('class'): | |
clsname = line.strip().split(' ')[1] | |
newtablename = '' | |
first = True | |
for c in clsname: | |
if c.isupper(): | |
if not first: newtablename += '_' | |
newtablename += c.lower() | |
else: newtablename += c | |
first = False | |
print 'rename table', oldtablename, 'to', newtablename, ';' | |
if '@ORM\Column' in line: | |
m = re.match(r'.*name="([^"]*).*', line) | |
oldcolname = m.group(1) | |
if 'private $' in line and oldcolname != None: | |
m = re.match(r'.*\$([^;]*).*', line) | |
if oldcolname != m.group(1): | |
print 'alter table', newtablename, 'CHANGE', oldcolname, m.group(1), cols[oldtablename][oldcolname], ';' | |
oldcolname = None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment