Example #1
0
def find_dependent_tables(table, metadata=None):
    '''
    Return an iterator with all tables that depend on table.  The
    tables are returned in the order that they depend on each
    other. For example you know that table[0] does not depend on
    tables[1].

    :param table: The tables who dependencies we want to find

    :param metadata: The :class:`sqlalchemy.engine.MetaData` object
      that holds the tables to search through.  If None then use
      bauble.db.metadata
    '''
    # NOTE: we can't use bauble.metadata.sorted_tables here because it
    # returns all the tables in the metadata even if they aren't
    # dependent on table at all
    from sqlalchemy.sql.util import sort_tables
    if metadata is None:
        import bauble.db as db
        metadata = db.metadata
    tables = []

    def _impl(t2):
        for tbl in metadata.sorted_tables:
            for fk in tbl.foreign_keys:
                if fk.column.table == t2 and tbl not in tables \
                        and tbl is not table:
                    tables.append(tbl)
                    _impl(tbl)

    _impl(table)
    return sort_tables(tables=tables)
Example #2
0
def find_dependent_tables(table, metadata=None):
    '''
    Return an iterator with all tables that depend on table.  The
    tables are returned in the order that they depend on each
    other. For example you know that table[0] does not depend on
    tables[1].

    :param table: The tables who dependencies we want to find

    :param metadata: The :class:`sqlalchemy.engine.MetaData` object
      that holds the tables to search through.  If None then use
      bauble.db.metadata
    '''
    # NOTE: we can't use bauble.metadata.sorted_tables here because it
    # returns all the tables in the metadata even if they aren't
    # dependent on table at all
    from sqlalchemy.sql.util import sort_tables
    if metadata is None:
        import bauble.db as db
        metadata = db.metadata
    tables = []

    def _impl(t2):
        for tbl in metadata.sorted_tables:
            for fk in tbl.foreign_keys:
                if fk.column.table == t2 and tbl not in tables \
                        and tbl is not table:
                    tables.append(tbl)
                    _impl(tbl)
    _impl(table)
    return sort_tables(tables=tables)
Example #3
0
    def visit_metadata(self, metadata):
        if self.tables:
            tables = self.tables
        else:
            tables = metadata.tables.values()
        collection = [t for t in sql_util.sort_tables(tables) 
                        if self._can_create_table(t)]
        seq_coll = [s for s in metadata._sequences.values() 
                        if s.column is None and self._can_create_sequence(s)]

        metadata.dispatch.before_create(metadata, self.connection,
                                    tables=collection,
                                    checkfirst=self.checkfirst,
                                            _ddl_runner=self)

        for seq in seq_coll:
            self.traverse_single(seq, create_ok=True)

        for table in collection:
            self.traverse_single(table, create_ok=True)

        metadata.dispatch.after_create(metadata, self.connection,
                                    tables=collection,
                                    checkfirst=self.checkfirst,
                                            _ddl_runner=self)
Example #4
0
    def visit_metadata(self, metadata):
        if self.tables:
            tables = self.tables
        else:
            tables = metadata.tables.values()
        collection = [
            t for t in sql_util.sort_tables(tables)
            if self._can_create_table(t)
        ]
        seq_coll = [
            s for s in metadata._sequences.values()
            if s.column is None and self._can_create_sequence(s)
        ]

        metadata.dispatch.before_create(metadata,
                                        self.connection,
                                        tables=collection,
                                        checkfirst=self.checkfirst)

        for seq in seq_coll:
            self.traverse_single(seq, create_ok=True)

        for table in collection:
            self.traverse_single(table, create_ok=True)

        metadata.dispatch.after_create(metadata,
                                       self.connection,
                                       tables=collection,
                                       checkfirst=self.checkfirst)
Example #5
0
    def visit_metadata(self, metadata):
        if self.tables:
            tables = self.tables
        else:
            tables = list(metadata.tables.values())
        collection = [t for t in reversed(sql_util.sort_tables(tables))
                                if self._can_drop_table(t)]
        seq_coll = [s for s in list(metadata._sequences.values())
                                if s.column is None and self._can_drop_sequence(s)]

        metadata.dispatch.before_drop(metadata, self.connection,
                                            tables=collection,
                                            checkfirst=self.checkfirst,
                                            _ddl_runner=self)

        for table in collection:
            self.traverse_single(table, drop_ok=True)

        for seq in seq_coll:
            self.traverse_single(seq, drop_ok=True)

        metadata.dispatch.after_drop(metadata, self.connection,
                                            tables=collection,
                                            checkfirst=self.checkfirst,
                                            _ddl_runner=self)
Example #6
0
 def visit_metadata(self, metadata):
     if self.tables:
         tables = self.tables
     else:
         tables = metadata.tables.values()
     collection = [t for t in reversed(sql_util.sort_tables(tables)) if self._can_drop(t)]
     if self.dialect.supports_alter:
         for alterable in self.find_alterables(collection):
             self.drop_foreignkey(alterable)
     for table in collection:
         self.traverse_single(table)
Example #7
0
 def visit_metadata(self, metadata):
     if self.tables:
         tables = self.tables
     else:
         tables = metadata.tables.values()
     collection = [t for t in sql_util.sort_tables(tables) if self._can_create(t)]
     for table in collection:
         self.traverse_single(table)
     if self.dialect.supports_alter:
         for alterable in self.find_alterables(collection):
             self.connection.execute(schema.AddForeignKey(alterable))
Example #8
0
File: ddl.py Project: dreamwave/rad
    def visit_metadata(self, metadata):
        if self.tables:
            tables = self.tables
        else:
            tables = metadata.tables.values()
        collection = [t for t in reversed(sql_util.sort_tables(tables)) if self._can_drop(t)]
        
        for listener in metadata.ddl_listeners['before-drop']:
            listener('before-drop', metadata, self.connection, tables=collection)
        
        for table in collection:
            self.traverse_single(table)

        for listener in metadata.ddl_listeners['after-drop']:
            listener('after-drop', metadata, self.connection, tables=collection)
Example #9
0
    def visit_metadata(self, metadata):
        if self.tables:
            tables = self.tables
        else:
            tables = metadata.tables.values()
        collection = [t for t in reversed(sql_util.sort_tables(tables)) if self._can_drop(t)]
        
        for listener in metadata.ddl_listeners['before-drop']:
            listener('before-drop', metadata, self.connection, tables=collection)
        
        for table in collection:
            self.traverse_single(table, drop_ok=True)

        for listener in metadata.ddl_listeners['after-drop']:
            listener('after-drop', metadata, self.connection, tables=collection)
Example #10
0
    def visit_metadata(self, metadata):
        if self.tables:
            tables = self.tables
        else:
            tables = metadata.tables.values()
        collection = [t for t in sql_util.sort_tables(tables) if self._can_create(t)]
        
        for listener in metadata.ddl_listeners['before-create']:
            listener('before-create', metadata, self.connection, tables=collection)
            
        for table in collection:
            self.traverse_single(table, create_ok=True)

        for listener in metadata.ddl_listeners['after-create']:
            listener('after-create', metadata, self.connection, tables=collection)
Example #11
0
def get_sorted_models(decl_base):
    """
    Returns an iterator over all declarative classes registered to the given declarative base
    Similar to `sqlalchemy_utils.functions.orm.get_class_by_table` and `Mapper._sorted_tables`

    Uses the ordered list of tables (ordered by sqla using topological sort) to infer model order

    :param base:
    :return:
    """
    table_to_mapper = {}
    for k, v in decl_base._decl_class_registry.items():
        if k == '_sa_module_registry':
            # Internal orm declarative stuff
            continue
        v = inspect(v)  # Get the mapper
        for mapper in v.base_mapper.self_and_descendants:  # Taken from Mapper._sorted_tables
            for table in mapper.tables:
                table_to_mapper.setdefault(table, mapper)

    extra_dependencies = []
    for table, mapper in table_to_mapper.items():
        super_ = mapper.inherits
        if super_:
            extra_dependencies.extend([
                (super_table, table)
                for super_table in super_.tables
            ])

    sorted_tables = sql_util.sort_tables(table_to_mapper, extra_dependencies=extra_dependencies)

    for table in sorted_tables:
        try:
            mapper = table_to_mapper[table]
        except KeyError:
            logging.warning('Table not mapped to %r: %r', decl_base.__name__, table)
        else:
            yield mapper.entity