-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathpwiz.py
executable file
·618 lines (507 loc) · 20.4 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
#!/usr/bin/env python
from optparse import OptionParser
import re
import sys
from peewee import *
from peewee import print_
try:
from MySQLdb.constants import FIELD_TYPE
except ImportError:
try:
from pymysql.constants import FIELD_TYPE
except ImportError:
FIELD_TYPE = None
RESERVED_WORDS = set([
'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif',
'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import',
'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try',
'while', 'with', 'yield',
])
TEMPLATE = """from peewee import *
database = %s('%s', **%s)
class UnknownField(object):
pass
class BaseModel(Model):
class Meta:
database = database
"""
class UnknownField(object):
pass
class Column(object):
"""
Store metadata about a database column.
"""
primary_key_types = (IntegerField, PrimaryKeyField)
def __init__(self, name, field_class, raw_column_type, nullable,
primary_key=False, max_length=None, db_column=None):
self.name = name
self.field_class = field_class
self.raw_column_type = raw_column_type
self.nullable = nullable
self.primary_key = primary_key
self.max_length = max_length
self.db_column = db_column
def __repr__(self):
attrs = [
'field_class',
'raw_column_type',
'nullable',
'primary_key',
'max_length',
'db_column']
keyword_args = ', '.join(
'%s=%s' % (attr, getattr(self, attr))
for attr in attrs)
return 'Column(%s, %s)' % (self.name, keyword_args)
def get_field_parameters(self):
params = {}
# Set up default attributes.
if self.nullable:
params['null'] = True
if self.field_class is CharField and self.max_length:
params['max_length'] = self.max_length
if self.field_class is ForeignKeyField or self.name != self.db_column:
params['db_column'] = "'%s'" % self.db_column
if self.primary_key and not self.field_class is PrimaryKeyField:
params['primary_key'] = True
# Handle ForeignKeyField-specific attributes.
if self.field_class is ForeignKeyField:
params['rel_model'] = self.rel_model
return params
def is_primary_key(self):
return self.field_class is PrimaryKeyField or self.primary_key
def set_foreign_key(self, foreign_key, model_names):
self.field_class = ForeignKeyField
if foreign_key.dest_table == foreign_key.table:
self.rel_model = "'self'"
else:
self.rel_model = model_names[foreign_key.dest_table]
self.to_field = foreign_key.dest_column
def get_field(self):
# Generate the field definition for this column.
field_params = self.get_field_parameters()
param_str = ', '.join('%s=%s' % (k, v)
for k, v in sorted(field_params.items()))
field = '%s = %s(%s)' % (
self.name,
self.field_class.__name__,
param_str)
if self.field_class is UnknownField:
field = '%s # %s' % (field, self.raw_column_type)
return field
class ForeignKeyMapping(object):
def __init__(self, table, column, dest_table, dest_column):
self.table = table
self.column = column
self.dest_table = dest_table
self.dest_column = dest_column
def __repr__(self):
return 'ForeignKeyMapping(%s.%s -> %s.%s)' % (
self.table,
self.column,
self.dest_table,
self.dest_column)
class Metadata(object):
column_map = {}
database_class = None
def __init__(self, database, **kwargs):
self._conn = self.connect(database, **kwargs)
self.database = database
self.database_kwargs = kwargs
def execute(self, sql, *params):
return self._conn.execute_sql(sql, params)
def set_search_path(self, *path):
self._conn.set_search_path(*path)
def connect(self, database, **kwargs):
return self.database_class(database, **kwargs)
def get_tables(self):
"""Returns a list of table names."""
return self._conn.get_tables()
def get_columns(self, table):
pass
def get_foreign_keys(self, table, schema=None):
pass
class PostgresqlMetadata(Metadata):
# select oid, typname from pg_type;
column_map = {
16: BooleanField,
17: BlobField,
20: BigIntegerField,
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
}
database_class = PostgresqlDatabase
def get_columns(self, table):
# Get basic metadata about columns.
cursor = self.execute("""
SELECT
column_name, is_nullable, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name=%s""", table)
name_to_info = {}
for row in cursor.fetchall():
name_to_info[row[0]] = {
'db_column': row[0],
'nullable': row[1] == 'YES',
'raw_column_type': row[2],
'max_length': row[3],
'primary_key': False,
}
# Look up the actual column type for each column.
cursor = self.execute('SELECT * FROM "%s" LIMIT 1' % table)
# Store column metadata in dictionary keyed by column name.
for column_description in cursor.description:
field_class = self.column_map.get(
column_description.type_code,
UnknownField)
column = column_description.name
name_to_info[column]['field_class'] = field_class
# Look up the primary keys.
cursor = self.execute("""
SELECT pg_attribute.attname
FROM pg_index, pg_class, pg_attribute
WHERE
pg_class.oid = '%s'::regclass AND
indrelid = pg_class.oid AND
pg_attribute.attrelid = pg_class.oid AND
pg_attribute.attnum = any(pg_index.indkey)
AND indisprimary;""" % table)
pk_names = [row[0] for row in cursor.fetchall()]
for pk_name in pk_names:
name_to_info[pk_name]['primary_key'] = True
if name_to_info[pk_name]['field_class'] is IntegerField:
name_to_info[pk_name]['field_class'] = PrimaryKeyField
columns = {}
for name, column_info in name_to_info.items():
columns[name] = Column(
name,
field_class=column_info['field_class'],
raw_column_type=column_info['raw_column_type'],
nullable=column_info['nullable'],
primary_key=column_info['primary_key'],
max_length=column_info['max_length'],
db_column=name)
return columns
def get_foreign_keys(self, table, schema=None):
schema = schema or 'public'
sql = """
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 AND
tc.constraint_schema = kcu.constraint_schema)
JOIN information_schema.constraint_column_usage AS ccu
ON (ccu.constraint_name = tc.constraint_name AND
ccu.constraint_schema = tc.constraint_schema)
WHERE
tc.constraint_type = 'FOREIGN KEY' AND
tc.table_name = %s AND
tc.table_schema = %s"""
cursor = self.execute(sql, table, schema)
return [
ForeignKeyMapping(table, column, dest_table, dest_column)
for column, dest_table, dest_column in cursor]
class MySQLMetadata(Metadata):
if FIELD_TYPE is None:
column_map = {}
else:
column_map = {
FIELD_TYPE.BLOB: TextField,
FIELD_TYPE.CHAR: CharField,
FIELD_TYPE.DATE: DateField,
FIELD_TYPE.DATETIME: DateTimeField,
FIELD_TYPE.DECIMAL: DecimalField,
FIELD_TYPE.DOUBLE: FloatField,
FIELD_TYPE.FLOAT: FloatField,
FIELD_TYPE.INT24: IntegerField,
FIELD_TYPE.LONG_BLOB: TextField,
FIELD_TYPE.LONG: IntegerField,
FIELD_TYPE.LONGLONG: BigIntegerField,
FIELD_TYPE.MEDIUM_BLOB: TextField,
FIELD_TYPE.NEWDECIMAL: DecimalField,
FIELD_TYPE.SHORT: IntegerField,
FIELD_TYPE.STRING: CharField,
FIELD_TYPE.TIMESTAMP: DateTimeField,
FIELD_TYPE.TIME: TimeField,
FIELD_TYPE.TINY_BLOB: TextField,
FIELD_TYPE.TINY: IntegerField,
FIELD_TYPE.VAR_STRING: CharField,
}
database_class = MySQLDatabase
def __init__(self, database, **kwargs):
if 'password' in kwargs:
kwargs['passwd'] = kwargs.pop('password')
super(MySQLMetadata, self).__init__(database, **kwargs)
def get_columns(self, table):
pk_name = self.get_primary_key(table)
# Get basic metadata about columns.
cursor = self.execute("""
SELECT
column_name, is_nullable, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name=%s AND table_schema=DATABASE()""", table)
name_to_info = {}
for row in cursor.fetchall():
name_to_info[row[0]] = {
'db_column': row[0],
'nullable': row[1] == 'YES',
'raw_column_type': row[2],
'max_length': row[3],
'primary_key': False,
}
# Look up the actual column type for each column.
cursor = self.execute('SELECT * FROM `%s` LIMIT 1' % table)
# Store column metadata in dictionary keyed by column name.
for column_description in cursor.description:
name, type_code = column_description[:2]
field_class = self.column_map.get(type_code, UnknownField)
if name == pk_name:
name_to_info[name]['primary_key'] = True
if field_class is IntegerField:
field_class = PrimaryKeyField
name_to_info[name]['field_class'] = field_class
columns = {}
for name, column_info in name_to_info.items():
columns[name] = Column(
name,
field_class=column_info['field_class'],
raw_column_type=column_info['raw_column_type'],
nullable=column_info['nullable'],
primary_key=column_info['primary_key'],
max_length=column_info['max_length'],
db_column=name)
return columns
def get_primary_key(self, table):
cursor = self.execute('SHOW INDEX FROM `%s`' % table)
for row in cursor.fetchall():
if row[2] == 'PRIMARY':
return row[4]
def get_foreign_keys(self, table, schema=None):
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
"""
cursor = self.execute(framing, table)
return [
ForeignKeyMapping(table, column, dest_table, dest_column)
for column, dest_table, dest_column in cursor]
class SqliteMetadata(Metadata):
column_map = {
'bigint': BigIntegerField,
'blob': BlobField,
'bool': BooleanField,
'boolean': BooleanField,
'char': CharField,
'date': DateField,
'datetime': DateTimeField,
'decimal': DecimalField,
'integer': IntegerField,
'integer unsigned': IntegerField,
'int': IntegerField,
'long': BigIntegerField,
'real': FloatField,
'smallinteger': IntegerField,
'smallint': IntegerField,
'smallint unsigned': IntegerField,
'text': TextField,
'time': TimeField,
}
database_class = SqliteDatabase
begin = '(?:["\[\(]+)?'
end = '(?:["\]\)]+)?'
re_foreign_key = (
'(?:FOREIGN KEY\s*)?'
'{begin}(.+?){end}\s+(?:.+\s+)?'
'references\s+{begin}(.+?){end}'
'\s*\(["|\[]?(.+?)["|\]]?\)').format(begin=begin, end=end)
re_varchar = r'^\s*(?:var)?char\s*\(\s*(\d+)\s*\)\s*$'
def _map_col(self, column_type):
raw_column_type = column_type.lower()
if raw_column_type in self.column_map:
field_class = self.column_map[raw_column_type]
elif re.search(self.re_varchar, raw_column_type):
field_class = CharField
else:
column_type = re.sub('\(.+\)', '', raw_column_type)
field_class = self.column_map.get(column_type, UnknownField)
return field_class, raw_column_type
def get_columns(self, table):
columns = {}
# Column ID, Name, Column Type, Not Null?, Default, Is Primary Key?
cursor = self.execute('PRAGMA table_info("%s")' % table)
for (_, name, column_type, not_null, _, is_pk) in cursor.fetchall():
field_class, raw_column_type = self._map_col(column_type)
if is_pk and field_class == IntegerField:
field_class = PrimaryKeyField
max_length = None
if field_class is CharField:
match = re.match('\w+\((\d+)\)', column_type)
if match:
max_length, = match.groups()
columns[name] = Column(
name,
field_class=field_class,
raw_column_type=raw_column_type,
nullable=not not_null,
primary_key=is_pk,
max_length=max_length,
db_column=name)
return columns
def get_foreign_keys(self, table, schema=None):
query = """
SELECT sql
FROM sqlite_master
WHERE (tbl_name = ? AND type = ?)"""
cursor = self.execute(query, table, 'table')
table_definition = cursor.fetchone()[0].strip()
try:
columns = re.search('\((.+)\)', table_definition).groups()[0]
except AttributeError:
print_('Unable to read table definition for "%s"' % table)
return []
fks = []
for column_def in columns.split(','):
column_def = column_def.strip()
match = re.search(self.re_foreign_key, column_def, re.I)
if not match:
continue
column, dest_table, dest_column = [
s.strip('"') for s in match.groups()]
fks.append(ForeignKeyMapping(
table=table,
column=column,
dest_table=dest_table,
dest_column=dest_column))
return fks
DATABASE_ALIASES = {
SqliteMetadata: ['sqlite', 'sqlite3'],
MySQLMetadata: ['mysql', 'mysqldb'],
PostgresqlMetadata: ['postgres', 'postgresql'],
}
DATABASE_MAP = dict((value, key)
for key in DATABASE_ALIASES
for value in DATABASE_ALIASES[key])
def make_introspector(database_type, database, **kwargs):
if database_type not in DATABASE_MAP:
err('Unrecognized database, must be one of: %s' %
', '.join(DATABASE_MAP.keys()))
sys.exit(1)
schema = kwargs.pop('schema', None)
metadata = DATABASE_MAP[database_type](database, **kwargs)
if schema:
metadata.set_search_path(*schema.split(','))
return Introspector(metadata, schema=schema)
class Introspector(object):
pk_classes = [PrimaryKeyField, IntegerField]
def __init__(self, metadata, schema=None):
self.metadata = metadata
self.schema = schema
def make_model_name(self, table):
model = re.sub('[^\w]+', '', table)
return ''.join(sub.title() for sub in model.split('_'))
def make_column_name(self, column):
column = re.sub('_id$', '', column.lower()) or column.lower()
if column in RESERVED_WORDS:
column += '_'
return column
def introspect(self):
# Retrieve all the tables in the database.
tables = self.metadata.get_tables()
# Store a mapping of table name -> dictionary of columns.
columns = {}
# Store a mapping of table -> foreign keys.
foreign_keys = {}
# Store a mapping of table name -> model name.
model_names = {}
# Gather the columns for each table.
for table in tables:
columns[table] = self.metadata.get_columns(table)
foreign_keys[table] = self.metadata.get_foreign_keys(
table, self.schema)
model_names[table] = self.make_model_name(table)
# On the second pass convert all foreign keys.
for table in tables:
for foreign_key in foreign_keys[table]:
src = columns[foreign_key.table][foreign_key.column]
src.set_foreign_key(foreign_key, model_names)
for column_name, column in columns[table].items():
column.name = self.make_column_name(column_name)
return columns, foreign_keys, model_names
def print_models(self, tables=None):
columns, foreign_keys, model_names = self.introspect()
print_(TEMPLATE % (
self.metadata.database_class.__name__,
self.metadata.database,
repr(self.metadata.database_kwargs)))
def _print_table(table, seen, accum=None):
accum = accum or []
for foreign_key in foreign_keys[table]:
dest = foreign_key.dest_table
# In the event the destination table has already been pushed
# for printing, then we have a reference cycle.
if dest in accum and table not in accum:
print_('# Possible reference cycle: %s' % foreign_key)
# If this is not a self-referential foreign key, and we have
# not already processed the destination table, do so now.
if dest not in seen and dest not in accum:
seen.add(dest)
if dest != table:
_print_table(dest, seen, accum + [table])
print_('class %s(BaseModel):' % model_names[table])
for name, column in sorted(columns[table].items()):
if name == 'id' and column.field_class in self.pk_classes:
continue
print_(' %s' % column.get_field())
print_('')
print_(' class Meta:')
print_(' db_table = \'%s\'' % table)
print_('')
seen.add(table)
seen = set()
for table in sorted(model_names.keys()):
if table not in seen:
if not tables or table in tables:
_print_table(table, seen)
def err(msg):
sys.stderr.write('\033[91m%s\033[0m\n' % msg)
sys.stderr.flush()
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')
ao('-t', '--tables', dest='tables')
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:
err('Missing required parameter "database"')
parser.print_help()
sys.exit(1)
database = args[-1]
tables = None
if options.tables:
tables = [x for x in options.tables.split(',') if x]
introspector = make_introspector(options.engine, database, **connect)
introspector.print_models(tables)