def get_all_tables(source: Engine, tenant=None, public=True) -> list: tables = [] if public: publicMeta = MetaData() publicMeta.reflect(bind=source) tables = list(sort_tables_and_constraints(publicMeta.tables.values())) if tenant: tenantMeta = MetaData() tenantMeta.reflect(bind=source, schema=tenant) tables += list(sort_tables_and_constraints(tenantMeta.tables.values())) return tables
async def visit_metadata(self, metadata): if self.tables is not None: tables = self.tables else: tables = list(metadata.tables.values()) tables_create = [] for t in tables: if await self._can_create_table(t): tables_create.append(t) collection = sort_tables_and_constraints(tables_create) seq_coll = [] # noinspection PyProtectedMember for s in metadata._sequences.values(): if s.column is None and await self._can_create_sequence(s): seq_coll.append(s) event_collection = [t for (t, fks) in collection if t is not None] await _Async(metadata.dispatch.before_create)( metadata, self.connection, tables=event_collection, checkfirst=self.checkfirst, _ddl_runner=self, ) for seq in seq_coll: await self.traverse_single(seq, create_ok=True) for table, fkcs in collection: if table is not None: await self.traverse_single( table, create_ok=True, include_foreign_key_constraints=fkcs, _is_metadata_operation=True, ) else: for fkc in fkcs: await self.traverse_single(fkc) await _Async(metadata.dispatch.after_create)( metadata, self.connection, tables=event_collection, checkfirst=self.checkfirst, _ddl_runner=self, )
async def visit_metadata(self, metadata): if self.tables is not None: tables = self.tables else: tables = list(metadata.tables.values()) try: unsorted_tables = [] for t in tables: if await self._can_drop_table(t): unsorted_tables.append(t) collection = list( reversed( sort_tables_and_constraints( unsorted_tables, filter_fn=lambda constraint: False if not self.dialect. supports_alter or constraint.name is None else None, ))) except exc.CircularDependencyError as err2: if not self.dialect.supports_alter: util.warn( "Can't sort tables for DROP; an " "unresolvable foreign key " "dependency exists between tables: %s, and backend does " "not support ALTER. To restore at least a partial sort, " "apply use_alter=True to ForeignKey and " "ForeignKeyConstraint " "objects involved in the cycle to mark these as known " "cycles that will be ignored." % (", ".join(sorted([t.fullname for t in err2.cycles])))) collection = [(t, ()) for t in unsorted_tables] else: util.raise_from_cause( exc.CircularDependencyError( err2.args[0], err2.cycles, err2.edges, msg="Can't sort tables for DROP; an unresolvable " "foreign key dependency exists between tables: %s." " Please ensure that the ForeignKey and " "ForeignKeyConstraint objects involved in the " "cycle have names so that they can be dropped " "using DROP CONSTRAINT." % (", ".join(sorted([t.fullname for t in err2.cycles]))), )) seq_coll = [] for s in metadata._sequences.values(): if s.column is None and await self._can_drop_sequence(s): seq_coll.append(s) event_collection = [t for (t, fks) in collection if t is not None] await _Async(metadata.dispatch.before_drop)( metadata, self.connection, tables=event_collection, checkfirst=self.checkfirst, _ddl_runner=self, ) for table, fkcs in collection: if table is not None: await self.traverse_single(table, drop_ok=True, _is_metadata_operation=True) else: for fkc in fkcs: await self.traverse_single(fkc) for seq in seq_coll: await self.traverse_single(seq, drop_ok=True) await _Async(metadata.dispatch.after_drop)( metadata, self.connection, tables=event_collection, checkfirst=self.checkfirst, _ddl_runner=self, )
import os from sqlalchemy import create_engine, inspect, MetaData from sqlalchemy.sql.ddl import sort_tables_and_constraints if __name__ == '__main__': # pragma: no cover source = create_engine(os.environ['DATABASE_URL_ETOOLS'], echo=False) destination = create_engine(os.environ['DATABASE_URL_DATAMART']) # reset_database(destination) # syncronyze_extensions(source, destination) # sync_ddl(source, destination) # add_tenant(source, destination) # migrate(source, destination, 'public') metadata = MetaData() inspector = inspect(source) metadata.reflect(bind=source, schema='bolivia') tables = sort_tables_and_constraints(metadata.tables.values()) for tablename, deps in tables: # TODO: remove me print(111, "main.py:23", tablename, deps)