Skip to content

Instantly share code, notes, and snippets.

@meeuw
Created May 18, 2014 18:50
Show Gist options
  • Save meeuw/b9a3b4e9696cebbf1f2a to your computer and use it in GitHub Desktop.
Save meeuw/b9a3b4e9696cebbf1f2a to your computer and use it in GitHub Desktop.
convert doctrine annotations to alter table / rename table sql
#!/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