We have found it useful to be able to print out the schema in SQL to look 
at it, and to diff versions of it alongside versions of the orm. Printing 
it out is relatively straightforward, diffing slightly less; currently 
we're using something very close to the following:

def dump_schema(TheModelBase):
    out = StringIO()

    def dump(sql, *multiparams, **params):
        out.write(('%s' % sql.compile(dialect=engine.dialect)).strip()+';\n\n')

    engine = create_engine('postgres://', strategy='mock', executor=dump)
    # patch tables with consistent collections so things get output in a
    # consistent order
    # Note: This doesn't fully work. The topo sort yields sets of nodes from the
    # graph, these sets are not guaranteed to be consistent.
    tables = TheModelBase.metadata.tables
    for table in tables.values():
        table.indexes = SortedSet(table.indexes)
    TheModelBase.metadata.tables = OrderedDict(
        (k, tables[k]) for k in sorted(tables.keys())
    )
    TheModelBase.metadata.create_all(engine)
    TheModelBase.metadata.tables = tables

    # tweak the spacing
    text = out.getvalue().strip()
    text = text.replace('\t', '    ')
    text = re.sub(r' *$', '', text, flags=re.MULTILINE)
    # Make this and "PRIMARY KEY (" line up
    text = text.replace('FOREIGN KEY(', 'FOREIGN KEY (')

    # alphabetically sort certain statements within their groupings
    def sort_statement_groups(text, prefix):
        return '\n\n'.join(it.chain(*[
            sorted(statements) if prefixed else list(statements)
            for prefixed, statements in it.groupby(text.split('\n\n'), 
key=lambda x: x.startswith(prefix))
        ]))
    text = sort_statement_groups(text, 'CREATE TYPE')
    text = sort_statement_groups(text, 'CREATE INDEX')
    
    HEADER = '-- Autogenerated SQL, manual edits will be discarded\n\n\n'
    return HEADER + text + '\n'


I think this has been pretty useful, I'm wondering if it would be worth 
having upstream, or if the consensus is that just the first four lines of 
this function are "good enough" (it wasn't good enough for me).

~Cheers

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to