Skip to content
This repository
executable file 559 lines (467 sloc) 18.515 kb
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
#!/usr/bin/env python
"""
.----.
===(_)== THIS WONT HURT A BIT...
// 6 6 \\ /
( 7 )
\ '--' /
\_ ._/
__) (__
/"`/`\`V/`\`\
/ \ `Y _/_ \
/ [DR]\_ |/ / /\
| ( \/ / / /
\ \ \ /
\ `-/` _.`
`=. `=./
`"`
"""
from collections import namedtuple
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


class UnknownFieldType(object):
    pass

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',
])

class ColumnInfo(object):
    """
Store metadata about a given column.
"""
    __slots__ = ['field_class', 'kwargs', 'is_pk', 'raw_column_type']

    def __init__(self, field_class, kwargs=None, is_pk=False,
                 raw_column_type=None):
        self.field_class = field_class
        self.kwargs = kwargs
        self.is_pk = is_pk
        self.raw_column_type = raw_column_type

ForeignKeyMapping = namedtuple('ForeignKeyMapping', ('column', 'table', 'pk'))

class Introspector(object):
    conn = None
    peewee_mapping = {}

    def get_conn_class(self):
        raise NotImplementedError

    def get_columns(self, table):
        """
Get the names, field type and attributes for the columns in the
given table.

Arguments:
table str: the name of the table to introspect

Returns:
A dictionary keyed by the column name, mapped to a ColumnInfo
object.
"""
        raise NotImplementedError

    def get_foreign_keys(self, table):
        """
Get the foreign keys from the given table to other tables/columns.

Arguments:
table str: the name of the table to introspect

Returns:
A list of `ForeignKeyMapping`s
"""
        raise NotImplementedError

    def get_tables(self):
        """Returns a list of table names."""
        return self.conn.get_tables()

    def connect(self, database, **connect):
        """
Open a connection to the given database, passing along any keyword
arguments.
"""
        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 PostgresqlIntrospector(Introspector):
    # select oid, typname from pg_type;
    mapping = {
        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
    }

    def get_conn_class(self):
        return PostgresqlDatabase

    def _get_field_class(self, oid):
        return self.mapping.get(oid, UnknownFieldType)

    def get_columns(self, table):
        # Get basic metadata about columns.
        curs = self.conn.execute_sql("""
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 curs.fetchall():
            name_to_info[row[0]] = row

        # Look up the actual column type for each column. TODO: combine with
        # query above?
        curs = self.conn.execute_sql('select * from "%s" limit 1' % table)

        # Store column metadata in dictionary keyed by column name.
        accum = {}
        for column_description in curs.description:
            field_class = self._get_field_class(column_description.type_code)
            name, nullable, data_type, max_length = name_to_info[
                column_description.name]

            kwargs = {}
            if nullable == 'YES':
                kwargs['null'] = True
            if field_class is CharField and max_length:
                kwargs['max_length'] = max_length

            accum[name] = ColumnInfo(
                field_class,
                kwargs,
                raw_column_type=data_type)

        # Look up the primary keys.
        curs = self.conn.execute_sql("""
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)
        pks = [x[0] for x in curs.fetchall()]
        for pk in pks:
            if accum[pk].field_class == IntegerField:
                accum[pk].field_class = PrimaryKeyField
            else:
                accum[pk].kwargs['primary_key'] = True

        return accum

    def get_foreign_keys(self, table, schema='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
'''
        result = self.conn.execute_sql(sql, (table, schema))
        return [ForeignKeyMapping(*row) for row in result]


class MySQLIntrospector(Introspector):
    if FIELD_TYPE is None:
        mapping = {}
    else:
        mapping = {
            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,
        }

    def get_conn_class(self):
        return MySQLDatabase

    def get_columns(self, table):
        pk_col = self.get_primary_key(table)

        # Get basic metadata about columns. Amazingly, this is the same query
        # as we use with postgresql.
        curs = self.conn.execute_sql("""
SELECT column_name, is_nullable, data_type, character_maximum_length
FROM information_schema.columns
WHERE table_name=%s""", (table,))
        columns = curs.fetchall()

        curs = self.conn.execute_sql('select * from `%s` limit 1' % table)
        accum = {}
        for idx, column in enumerate(curs.description):
            field_class = self.mapping.get(column[1], UnknownFieldType)
            name, nullable, data_type, max_length = columns[idx]
            kwargs = {}
            if name == pk_col and field_class is IntegerField:
                field_class = PrimaryKeyField
            elif name == pk_col:
                kwargs['primary_key'] = True

            if nullable == 'YES':
                kwargs['null'] = True
            if field_class is CharField and max_length:
                kwargs['max_length'] = max_length

            accum[name] = ColumnInfo(
                field_class,
                kwargs,
                raw_column_type=data_type)
        return accum

    def get_primary_key(self, table):
        curs = self.conn.execute_sql('SHOW INDEX FROM `%s`' % table)
        for row in curs.fetchall():
            if row[2] == 'PRIMARY':
                return row[4]

    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
'''
        cursor = self.conn.execute_sql(framing, (table,))
        return [ForeignKeyMapping(*row) for row in cursor]


class SqliteIntrospector(Introspector):
    mapping = {
        '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,
    }
    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 get_conn_class(self):
        return SqliteDatabase

    def map_col(self, column_type):
        column_type = column_type.lower()
        if column_type in self.mapping:
            column_class = self.mapping[column_type]
        elif re.search(self.re_varchar, column_type):
            column_class = CharField
        else:
            column_type = re.sub('\(.+\)', '', column_type)
            column_class = self.mapping.get(column_type, UnknownFieldType)
        return column_class, column_type

    def get_columns(self, table):
        curs = self.conn.execute_sql('pragma table_info("%s")' % table)
        accum = {}
        # cid, name, type, notnull, dflt_value, pk
        for (_, name, column_type, not_null, _, is_pk) in curs.fetchall():
            field_class, raw_column_type = self.map_col(column_type)
            kwargs = {}

            if is_pk and field_class == IntegerField:
                field_class = PrimaryKeyField
            elif is_pk:
                kwargs['primary_key'] = True

            if not not_null:
                kwargs['null'] = True

            accum[name] = ColumnInfo(
                field_class,
                kwargs,
                is_pk=is_pk,
                raw_column_type=raw_column_type)
        return accum

    def get_foreign_keys(self, table):
        curs = self.conn.execute_sql("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)
            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

            fk_column, rel_table, rel_pk = [s.strip('"') for s in match.groups()]
            fks.append(ForeignKeyMapping(fk_column, rel_table, rel_pk))

        return fks


TEMPLATE = '''from peewee import *

database = %s('%s', **%s)

class UnknownFieldType(object):
pass

class BaseModel(Model):
class Meta:
database = database
'''

ENGINE_MAPPING = {
    'postgresql': PostgresqlIntrospector,
    'postgres': PostgresqlIntrospector,
    'sqlite': SqliteIntrospector,
    'mysql': MySQLIntrospector,
}

def get_introspector(engine, database, **connect):
    if engine not in ENGINE_MAPPING:
        err('Unsupported engine: "%s"' % engine)
        sys.exit(1)

    introspector = ENGINE_MAPPING[engine]()
    schema = connect.pop('schema', None)
    introspector.connect(database, **connect)

    if schema:
        introspector.conn.set_search_path(*schema.split(','))
    return introspector

def introspect(db, schema=None):
    tables = db.get_tables()

    table_columns = {}
    table_to_model = {}
    table_fks = {}

    # first pass, just raw column names and peewee type
    for table in tables:
        table_columns[table] = db.get_columns(table)
        table_to_model[table] = tn(table)
        if schema:
            table_fks[table] = db.get_foreign_keys(table, schema)
        else:
            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
    column_metadata = {}
    for table in tables:
        column_metadata[table] = {}
        for column, rel_table, rel_pk in table_fks[table]:
            is_pk = table_columns[table][column].field_class is PrimaryKeyField
            table_columns[table][column].field_class = ForeignKeyField
            table_columns[rel_table][rel_pk].field_class = PrimaryKeyField
            if rel_table == table:
                ttm = "'self'"
            else:
                ttm = table_to_model[rel_table]
            column_metadata[table][column] = {'rel_model': ttm}
            if is_pk:
                column_metadata[table][column]['primary_key'] = True

        for col_name, column_info in table_columns[table].items():
            column_metadata[table].setdefault(col_name, {})
            column_metadata[table][col_name].update(column_info.kwargs)
            requires_db_column = any((
                column_info.field_class is ForeignKeyField,
                col_name != cn(col_name),
                cn(col_name) in RESERVED_WORDS))
            if requires_db_column:
                column_metadata[table][col_name]['db_column'] = "'%s'" % col_name

    return table_columns, table_to_model, table_fks, column_metadata

def print_models(engine, database, tables, **connect):
    schema = connect.get('schema')
    db = get_introspector(engine, database, **connect)

    models, table_to_model, table_fks, col_meta = introspect(db, schema)

    # write generated code to standard out
    print_(TEMPLATE % (db.get_conn_class().__name__, database, repr(connect)))
    pk_classes = (IntegerField, PrimaryKeyField)

    # print the models
    def print_model(model, seen, accum=None):
        accum = accum or []

        for _, rel_table, _ in table_fks[model]:
            if rel_table in accum and model not in accum:
                print_('# POSSIBLE REFERENCE CYCLE: %s' % table_to_model[rel_table])

            if rel_table not in seen and rel_table not in accum:
                seen.add(rel_table)
                if rel_table != model:
                    print_model(rel_table, seen, accum + [model])

        ttm = table_to_model[model]
        print_('class %s(BaseModel):' % ttm)
        cols = models[model]
        for column, column_info in ds(cols):
            if column == 'id' and column_info.field_class in pk_classes:
                continue

            field_params = ', '.join([
                '%s=%s' % (k, v) for k, v in col_meta[model][column].items()
            ])
            colname = cn(column)
            if colname in RESERVED_WORDS:
                print_(' # FIXME: "%s" is a reserved word, renamed.' % colname)
                colname = colname + '_'

            comments = ''
            if column_info.field_class is UnknownFieldType:
                comments = ' # %s' % column_info.raw_column_type

            print_(' %s = %s(%s)%s' % (
                colname,
                column_info.field_class.__name__,
                field_params,
                comments))

        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:
            if not tables or model in tables:
                print_model(model, seen)

# misc
tn = lambda t: re.sub('[^\w]+', '', t.title())
cn = lambda c: re.sub('_id$', '', c.lower()) or c.lower()
ds = lambda d: sorted(d.items(), key=lambda t:t[0])

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:
        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)

    if options.tables:
        tables = [x for x in options.tables.split(',') if x]
    else:
        tables = []
    print_models(options.engine, database, tables, **connect)
Something went wrong with that request. Please try again.