forked from coleifer/peewee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpwiz.py
executable file
·359 lines (297 loc) · 10.5 KB
/
pwiz.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env python
# .----.
# ===(_)== THIS WONT HURT A BIT...
# // 6 6 \\ /
# ( 7 )
# \ '--' /
# \_ ._/
# __) (__
# /"`/`\`V/`\`\
# / \ `Y _/_ \
# / [DR]\_ |/ / /\
# | ( \/ / / /
# \ \ \ /
# \ `-/` _.`
# `=. `=./
# `"`
from optparse import OptionParser
import re
import sys
try:
from MySQLdb.constants import FIELD_TYPE
MYSQL_MAP = {
FIELD_TYPE.BLOB: 'TextField',
FIELD_TYPE.CHAR: 'CharField',
FIELD_TYPE.DECIMAL: 'DecimalField',
FIELD_TYPE.NEWDECIMAL: 'DecimalField',
FIELD_TYPE.DATE: 'DateField',
FIELD_TYPE.DATETIME: 'DateTimeField',
FIELD_TYPE.DOUBLE: 'FloatField',
FIELD_TYPE.FLOAT: 'FloatField',
FIELD_TYPE.INT24: 'IntegerField',
FIELD_TYPE.LONG: 'IntegerField',
FIELD_TYPE.LONGLONG: 'BigIntegerField',
FIELD_TYPE.SHORT: 'IntegerField',
FIELD_TYPE.STRING: 'CharField',
FIELD_TYPE.TIME: 'TimeField',
FIELD_TYPE.TIMESTAMP: 'DateTimeField',
FIELD_TYPE.TINY: 'IntegerField',
FIELD_TYPE.TINY_BLOB: 'TextField',
FIELD_TYPE.MEDIUM_BLOB: 'TextField',
FIELD_TYPE.LONG_BLOB: 'TextField',
FIELD_TYPE.VAR_STRING: 'CharField',
}
except ImportError:
MYSQL_MAP = {}
from peewee import *
class DB(object):
conn = None
def get_conn_class(self):
raise NotImplementedError
def get_columns(self, table):
"""
get_columns('some_table')
{
'name': 'CharField',
'age': 'IntegerField',
}
"""
raise NotImplementedError
def get_foreign_keys(self, table):
"""
get_foreign_keys('some_table')
[
# column, rel table, rel pk
('blog_id', 'blog', 'id'),
('user_id', 'users', 'id'),
]
"""
raise NotImplementedError
def get_tables(self):
return self.conn.get_tables()
def connect(self, database, **connect):
conn_class = self.get_conn_class()
self.conn = conn_class(database, **connect)
try:
self.conn.connect()
except:
err('error connecting to %s' % database)
raise
class PgDB(DB):
# thanks, django
reverse_mapping = {
16: 'BooleanField',
20: 'IntegerField',
21: 'IntegerField',
23: 'IntegerField',
25: 'TextField',
700: 'FloatField',
701: 'FloatField',
1042: 'CharField', # blank-padded CHAR
1043: 'CharField',
1082: 'DateField',
1114: 'DateTimeField',
1184: 'DateTimeField',
1083: 'TimeField',
1266: 'TimeField',
1700: 'DecimalField',
2950: 'TextField', # UUID
}
def get_conn_class(self):
return PostgresqlDatabase
def get_columns(self, table):
curs = self.conn.execute('select * from %s limit 1' % table)
return dict((c.name, self.reverse_mapping.get(c.type_code, 'UnknownFieldType')) for c in curs.description)
def get_foreign_keys(self, table):
framing = '''
SELECT
kcu.column_name, ccu.table_name, ccu.column_name
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE
tc.constraint_type = 'FOREIGN KEY' AND
tc.table_name = %s
'''
fks = []
for row in self.conn.execute(framing, (table,)):
fks.append(row)
return fks
class MySQLDB(DB):
# thanks, django
reverse_mapping = MYSQL_MAP
def get_conn_class(self):
return MySQLDatabase
def get_columns(self, table):
curs = self.conn.execute('select * from %s limit 1' % table)
return dict((r[0], self.reverse_mapping.get(r[1], 'UnknownFieldType')) for r in curs.description)
def get_foreign_keys(self, table):
framing = '''
SELECT column_name, referenced_table_name, referenced_column_name
FROM information_schema.key_column_usage
WHERE table_name = %s
AND table_schema = DATABASE()
AND referenced_table_name IS NOT NULL
AND referenced_column_name IS NOT NULL
'''
return [row for row in self.conn.execute(framing, (table,))]
class SqDB(DB):
# thanks, django
reverse_mapping = {
'bool': 'BooleanField',
'boolean': 'BooleanField',
'smallint': 'IntegerField',
'smallint unsigned': 'IntegerField',
'smallinteger': 'IntegerField',
'int': 'IntegerField',
'integer': 'IntegerField',
'bigint': 'BigIntegerField',
'integer unsigned': 'IntegerField',
'decimal': 'DecimalField',
'real': 'FloatField',
'text': 'TextField',
'char': 'CharField',
'date': 'DateField',
'datetime': 'DateTimeField',
'time': 'TimeField',
}
def get_conn_class(self):
return SqliteDatabase
def map_col(self, col):
col = col.lower()
if col in self.reverse_mapping:
return self.reverse_mapping[col]
elif re.search(r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$', col):
return 'CharField'
else:
return 'UnknownFieldType'
def get_columns(self, table):
curs = self.conn.execute('pragma table_info(%s)' % table)
col_dict = {}
for (_, name, col, not_null, _, is_pk) in curs.fetchall():
# cid, name, type, notnull, dflt_value, pk
if is_pk:
col_type = 'PrimaryKeyField'
else:
col_type = self.map_col(col)
col_dict[name] = col_type
return col_dict
def get_foreign_keys(self, table):
fks = []
curs = self.conn.execute("SELECT sql FROM sqlite_master WHERE tbl_name = ? AND type = ?", [table, "table"])
table_def = curs.fetchone()[0].strip()
try:
columns = re.search('\((.+)\)', table_def).groups()[0]
except AttributeError:
err('Unable to read table definition for "%s"' % table)
sys.exit(1)
for col_def in columns.split(','):
col_def = col_def.strip()
m = re.search('"?(.+?)"?\s+.+\s+references (.*) \(["|]?(.*)["|]?\)', col_def, re.I)
if not m:
continue
fk_column, rel_table, rel_pk = [s.strip('"') for s in m.groups()]
fks.append((fk_column, rel_table, rel_pk))
return fks
frame = '''from peewee import *
database = %s('%s', **%s)
class UnknownFieldType(object):
pass
class BaseModel(Model):
class Meta:
database = database
'''
engine_mapping = {
'postgresql': PgDB,
'sqlite': SqDB,
'mysql': MySQLDB,
}
def get_db(engine):
if engine not in engine_mapping:
err('Unsupported engine: "%s"' % engine)
sys.exit(1)
db_class = engine_mapping[engine]
return db_class()
def introspect(engine, database, **connect):
db = get_db(engine)
schema = connect.pop('schema', None)
db.connect(database, **connect)
if schema:
db.conn.set_search_path(*schema.split(','))
tables = db.get_tables()
models = {}
table_to_model = {}
table_fks = {}
# first pass, just raw column names and peewee type
for table in tables:
models[table] = db.get_columns(table)
table_to_model[table] = tn(table)
table_fks[table] = db.get_foreign_keys(table)
# second pass, convert foreign keys, assign primary keys, and mark
# explicit column names where they don't match the "pythonic" ones
col_meta = {}
for table in tables:
col_meta[table] = {}
for column, rel_table, rel_pk in table_fks[table]:
models[table][column] = 'ForeignKeyField'
models[rel_table][rel_pk] = 'PrimaryKeyField'
col_meta[table][column] = {'to': table_to_model[rel_table]}
for column in models[table]:
col_meta[table].setdefault(column, {})
if column != cn(column):
col_meta[table][column]['db_column'] = "'%s'" % column
# write generated code to standard out
print frame % (db.get_conn_class().__name__, database, repr(connect))
# print the models
def print_model(model, seen):
for _, rel_table, _ in table_fks[model]:
if rel_table not in seen:
seen.add(rel_table)
print_model(rel_table, seen)
ttm = table_to_model[model]
print 'class %s(BaseModel):' % ttm
cols = models[model]
for column, field_class in ds(cols):
if column == 'id' and field_class in ('IntegerField', 'PrimaryKeyField'):
continue
field_params = ', '.join([
'%s=%s' % (k, v) for k, v in col_meta[model][column].items()
])
print ' %s = %s(%s)' % (cn(column), field_class, field_params)
print
print ' class Meta:'
print ' db_table = \'%s\'' % model
print
seen.add(model)
seen = set()
for model, cols in ds(models):
if model not in seen:
print_model(model, seen)
# misc
tn = lambda t: t.title().replace('_', '')
cn = lambda c: re.sub('_id$', '', c.lower())
ds = lambda d: sorted(d.items(), key=lambda t:t[0])
def err(msg):
print '\033[91m%s\033[0m' % msg
if __name__ == '__main__':
parser = OptionParser(usage='usage: %prog [options] database_name')
ao = parser.add_option
ao('-H', '--host', dest='host')
ao('-p', '--port', dest='port', type='int')
ao('-u', '--user', dest='user')
ao('-P', '--password', dest='password')
ao('-e', '--engine', dest='engine', default='postgresql')
ao('-s', '--schema', dest='schema')
options, args = parser.parse_args()
ops = ('host', 'port', 'user', 'password', 'schema')
connect = dict((o, getattr(options, o)) for o in ops if getattr(options, o))
if len(args) < 1:
print 'error: missing required parameter "database"'
parser.print_help()
sys.exit(1)
database = args[-1]
if options.engine == 'mysql' and 'password' in connect:
connect['passwd'] = connect.pop('password', None)
introspect(options.engine, database, **connect)