1 from peak.rules import abstract, when, around
2 from turbogears import config
3 from turbogears.util import get_model
4 from turbogears.decorator import simple_decorator
5 try:
6 from sqlalchemy import MetaData, exceptions, Table, String, Unicode
7 from turbogears.database import bind_metadata, metadata, get_engine
8 except ImportError:
9 from turbogears.util import missing_dependency_error
10 no_sqlalchemy = missing_dependency_error('SQLAlchemy')
11 else:
12 from sqlalchemy import Text, UnicodeText
13 no_sqlalchemy = False
14
15
16 @abstract()
17 -def sacommand(command, args):
19
20 @around(sacommand, "command and command != 'help' and no_sqlalchemy")
23
24 @when(sacommand, "command == 'help'")
25 -def help(command, args):
26 print """TurboGears SQLAlchemy Helper
27
28 tg-admin sql command [options]
29
30 Available commands:
31 create Create tables
32 execute Execute SQL statements
33 help Show help
34 list List tables that appear in the model
35 status Show differences between model and database
36 """
37
38 @when(sacommand, "command == 'create'")
44
45 @when(sacommand, "command == 'list'")
50
51 @when(sacommand, "command == 'execute'")
53 eng = get_engine()
54 for cmd in args[2:]:
55 ret = eng.execute(cmd)
56 try:
57 print list(ret)
58 except:
59
60 pass
61
62 @when(sacommand, "command == 'status'")
71
73 return [' ' + l for l in ls]
74
88
90 rc = []
91 dbcols = dict([(s.lower(), s) for s in dbt.columns.keys()])
92 for pyc in pyt.columns:
93 name = pyc.name.lower()
94 if dbcols.has_key(name):
95 ret = compare_column(pyc, dbt.columns[dbcols[name]])
96 if ret:
97 rc.append("Change column " + pyc.name)
98 rc.extend(indent(ret))
99 dbcols.pop(name)
100 else:
101 rc.append("Add column " + pyc.name)
102 for dbcol in dbcols:
103 rc.append("Remove column " + dbcol)
104 return rc
105
107 rc = []
108 pyt, dbt = pyc.type, dbc.type
109
110
111 if isinstance(pyt, Unicode):
112 pyt = String(pyt.length)
113 elif isinstance(pyt, UnicodeText):
114 pyt = Text(pyt.length)
115
116
117 if not isinstance(dbt, pyt.__class__):
118 rc.append('Change type to ' + pyt.__class__.__name__)
119
120
121 else:
122 if isinstance(pyt, String):
123 if pyt.length != dbt.length:
124 rc.append('Change length to ' + str(pyt.length))
125
126
127 if dbc.primary_key != pyc.primary_key:
128 rc.append(pyc.primary_key and 'Make primary key' or 'Remove primary key')
129
130
131
132
133 if (dbc.default is not None and pyc.default is not None
134 and dbc.default != pyc.default):
135 rc.append('Change default to ' + str(pyc.default.arg))
136
137
138 if dbc.index is not None and dbc.index != pyc.index:
139 rc.append(pyc.index and 'Add index' or 'Remove index')
140
141 return rc
142