def schema(tg):
tables = {}<EOL>for tname, table in tg.tabledict.items():<EOL><INDENT>t = TableSpec.from_table_metadata(table)<EOL>tables[t.name] = t<EOL>for at in t.many_to_many.values():<EOL><INDENT>tables[at.name] = at<EOL><DEDENT><DEDENT>ordered = OrderedDict()<EOL>i = <NUM_LIT:0><EOL>while tables and i < <NUM_LIT:100>:<EOL><INDENT>i += <NUM_LIT:1><EOL>for table in list(tables.keys()):<EOL><INDENT>if all((ref[<NUM_LIT:1>] in ordered) or ref[<NUM_LIT:1>] == table for ref in tables[table].foreign_keys):<EOL><INDENT>ordered[table] = tables.pop(table)<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if tables:  <EOL><INDENT>raise ValueError('<STR_LIT>')<EOL><DEDENT>return list(ordered.values())<EOL>
Convert the table and column descriptions of a `TableGroup` into specifications for the
DB schema.

:param ds:
:return: A pair (tables, reference_tables).