def test_raise_on_cycle_two(self):

        # this condition was arising from ticket:362 and was not treated
        # properly by topological sort

        node1 = "node1"
        node2 = "node2"
        node3 = "node3"
        node4 = "node4"
        tuples = [
            (node1, node2),
            (node3, node1),
            (node2, node4),
            (node3, node2),
            (node2, node3),
        ]
        allitems = self._nodes_from_tuples(tuples)

        try:
            list(topological.sort(tuples, allitems))
            assert False
        except exc.CircularDependencyError as err:
            eq_(err.cycles, set(["node1", "node3", "node2"]))
            eq_(
                err.edges,
                set(
                    [
                        ("node3", "node1"),
                        ("node2", "node3"),
                        ("node3", "node2"),
                        ("node1", "node2"),
                        ("node2", "node4"),
                    ]
                ),
            )
Exemplo n.º 2
0
    def test_raise_on_cycle_one(self):
        node1 = 'node1'
        node2 = 'node2'
        node3 = 'node3'
        node4 = 'node4'
        node5 = 'node5'
        tuples = [
            (node4, node5),
            (node5, node4),
            (node1, node2),
            (node2, node3),
            (node3, node1),
            (node4, node1),
            ]
        allitems = self._nodes_from_tuples(tuples)

        try:
            list(topological.sort(tuples, allitems))
            assert False
        except exc.CircularDependencyError as err:
            eq_(err.cycles, set(['node1', 'node3', 'node2', 'node5',
                'node4']))
            eq_(err.edges, set([('node3', 'node1'), ('node4', 'node1'),
                ('node2', 'node3'), ('node1', 'node2'),
                ('node4', 'node5'), ('node5', 'node4')]))
Exemplo n.º 3
0
def sort_tables(tables, extra_dependencies=None):
    """sort a collection of Table objects in order of their foreign-key dependency."""

    tables = list(tables)
    tuples = []
    if extra_dependencies:
        tuples.extend(extra_dependencies)

    def visit_foreign_key(fkey):
        if fkey.use_alter:
            return
        parent_table = fkey.column.table
        if parent_table in tables:
            child_table = fkey.parent.table
            if parent_table is not child_table:
                tuples.append((parent_table, child_table))

    for table in tables:
        visitors.traverse(table,
                            {'schema_visitor': True},
                            {'foreign_key': visit_foreign_key})

        tuples.extend(
            [parent, table] for parent in table._extra_dependencies
        )

    return list(topological.sort(tuples, tables))
Exemplo n.º 4
0
    def get_table_names(self, schema=None, order_by=None):
        """Return all table names in `schema`.

        :param schema: Optional, retrieve names from a non-default schema.
        :param order_by: Optional, may be the string "foreign_key" to sort
                         the result on foreign key dependencies.

        This should probably not return view names or maybe it should return
        them with an indicator t or v.
        """

        if hasattr(self.dialect, 'get_table_names'):
            tnames = self.dialect.get_table_names(self.bind,
            schema, info_cache=self.info_cache)
        else:
            tnames = self.engine.table_names(schema)
        if order_by == 'foreign_key':
            import random
            random.shuffle(tnames)

            tuples = []
            for tname in tnames:
                for fkey in self.get_foreign_keys(tname, schema):
                    if tname != fkey['referred_table']:
                        tuples.append((tname, fkey['referred_table']))
            tnames = list(topological.sort(tuples, tnames))
        return tnames
Exemplo n.º 5
0
 def assert_sort(self, tuples, allitems=None):
     if allitems is None:
         allitems = self._nodes_from_tuples(tuples)
     else:
         allitems = self._nodes_from_tuples(tuples).union(allitems)
     result = list(topological.sort(tuples, allitems))
     assert conforms_partial_ordering(tuples, result)
Exemplo n.º 6
0
    def get_table_names(self, schema=None, order_by=None):
        """Return all table names in `schema`.

        :param schema: Optional, retrieve names from a non-default schema.
        :param order_by: Optional, may be the string "foreign_key" to sort
                         the result on foreign key dependencies.

        This should probably not return view names or maybe it should return
        them with an indicator t or v.
        """

        if hasattr(self.dialect, 'get_table_names'):
            tnames = self.dialect.get_table_names(self.bind,
                                                  schema,
                                                  info_cache=self.info_cache)
        else:
            tnames = self.engine.table_names(schema)
        if order_by == 'foreign_key':
            import random
            random.shuffle(tnames)

            tuples = []
            for tname in tnames:
                for fkey in self.get_foreign_keys(tname, schema):
                    if tname != fkey['referred_table']:
                        tuples.append((tname, fkey['referred_table']))
            tnames = list(topological.sort(tuples, tnames))
        return tnames
Exemplo n.º 7
0
    def _adjust_self_columns_for_partial_reordering(self):
        pairs = set()

        col_by_idx = list(self.columns)

        if self.partial_reordering:
            for tuple_ in self.partial_reordering:
                for index, elem in enumerate(tuple_):
                    if index > 0:
                        pairs.add((tuple_[index - 1], elem))
        else:
            for index, elem in enumerate(self.existing_ordering):
                if index > 0:
                    pairs.add((col_by_idx[index - 1], elem))

        pairs.update(self.add_col_ordering)

        # this can happen if some columns were dropped and not removed
        # from existing_ordering.  this should be prevented already, but
        # conservatively making sure this didn't happen
        pairs = [p for p in pairs if p[0] != p[1]]

        sorted_ = list(
            topological.sort(pairs, col_by_idx, deterministic_order=True))
        self.columns = OrderedDict((k, self.columns[k]) for k in sorted_)
        self.column_transfers = OrderedDict(
            (k, self.column_transfers[k]) for k in sorted_)
Exemplo n.º 8
0
 def assert_sort(self, tuples, allitems=None):
     if allitems is None:
         allitems = self._nodes_from_tuples(tuples)
     else:
         allitems = self._nodes_from_tuples(tuples).union(allitems)
     result = list(topological.sort(tuples, allitems))
     assert conforms_partial_ordering(tuples, result)
    def test_raise_on_cycle_one(self):
        node1 = "node1"
        node2 = "node2"
        node3 = "node3"
        node4 = "node4"
        node5 = "node5"
        tuples = [
            (node4, node5),
            (node5, node4),
            (node1, node2),
            (node2, node3),
            (node3, node1),
            (node4, node1),
        ]
        allitems = self._nodes_from_tuples(tuples)

        try:
            list(topological.sort(tuples, allitems))
            assert False
        except exc.CircularDependencyError as err:
            eq_(err.cycles, set(["node1", "node3", "node2", "node5", "node4"]))
            eq_(
                err.edges,
                set(
                    [
                        ("node3", "node1"),
                        ("node4", "node1"),
                        ("node2", "node3"),
                        ("node1", "node2"),
                        ("node4", "node5"),
                        ("node5", "node4"),
                    ]
                ),
            )
Exemplo n.º 10
0
    def test_raise_on_cycle_one(self):
        node1 = 'node1'
        node2 = 'node2'
        node3 = 'node3'
        node4 = 'node4'
        node5 = 'node5'
        tuples = [
            (node4, node5),
            (node5, node4),
            (node1, node2),
            (node2, node3),
            (node3, node1),
            (node4, node1),
        ]
        allitems = self._nodes_from_tuples(tuples)

        try:
            list(topological.sort(tuples, allitems))
            assert False
        except exc.CircularDependencyError, err:
            eq_(err.cycles, set(['node1', 'node3', 'node2', 'node5', 'node4']))
            eq_(
                err.edges,
                set([('node3', 'node1'), ('node4', 'node1'),
                     ('node2', 'node3'), ('node1', 'node2'),
                     ('node4', 'node5'), ('node5', 'node4')]))
Exemplo n.º 11
0
 def test_raise_on_cycle_three(self):
     question, issue, providerservice, answer, provider = \
         'Question', 'Issue', 'ProviderService', 'Answer', 'Provider'
     tuples = [
         (question, issue),
         (providerservice, issue),
         (provider, question),
         (question, provider),
         (providerservice, question),
         (provider, providerservice),
         (question, answer),
         (issue, question),
         ]
     allitems = self._nodes_from_tuples(tuples)
     assert_raises(exc.CircularDependencyError, list,
                   topological.sort(tuples, allitems))
Exemplo n.º 12
0
 def test_raise_on_cycle_three(self):
     question, issue, providerservice, answer, provider = \
         'Question', 'Issue', 'ProviderService', 'Answer', 'Provider'
     tuples = [
         (question, issue),
         (providerservice, issue),
         (provider, question),
         (question, provider),
         (providerservice, question),
         (provider, providerservice),
         (question, answer),
         (issue, question),
     ]
     allitems = self._nodes_from_tuples(tuples)
     assert_raises(exc.CircularDependencyError, list,
                   topological.sort(tuples, allitems))
Exemplo n.º 13
0
    def execute(self):
        postsort_actions = self._generate_actions()

        #sort = topological.sort(self.dependencies, postsort_actions)
        #print "--------------"
        #print self.dependencies
        #print list(sort)
        #print "COUNT OF POSTSORT ACTIONS", len(postsort_actions)

        # execute
        if self.cycles:
            for set_ in topological.sort_as_subsets(self.dependencies,
                                                    postsort_actions):
                while set_:
                    n = set_.pop()
                    n.execute_aggregate(self, set_)
        else:
            for rec in topological.sort(self.dependencies, postsort_actions):
                rec.execute(self)
Exemplo n.º 14
0
    def execute(self):
        postsort_actions = self._generate_actions()

        #sort = topological.sort(self.dependencies, postsort_actions)
        #print "--------------"
        #print "\ndependencies:", self.dependencies
        #print "\ncycles:", self.cycles
        #print "\nsort:", list(sort)
        #print "\nCOUNT OF POSTSORT ACTIONS", len(postsort_actions)

        # execute
        if self.cycles:
            for set_ in topological.sort_as_subsets(self.dependencies,
                                                    postsort_actions):
                while set_:
                    n = set_.pop()
                    n.execute_aggregate(self, set_)
        else:
            for rec in topological.sort(self.dependencies, postsort_actions):
                rec.execute(self)
Exemplo n.º 15
0
    def test_raise_on_cycle_two(self):

        # this condition was arising from ticket:362 and was not treated
        # properly by topological sort

        node1 = 'node1'
        node2 = 'node2'
        node3 = 'node3'
        node4 = 'node4'
        tuples = [(node1, node2), (node3, node1), (node2, node4),
                  (node3, node2), (node2, node3)]
        allitems = self._nodes_from_tuples(tuples)

        try:
            list(topological.sort(tuples, allitems))
            assert False
        except exc.CircularDependencyError as err:
            eq_(err.cycles, set(['node1', 'node3', 'node2']))
            eq_(err.edges, set([('node3', 'node1'), ('node2', 'node3'),
                ('node3', 'node2'), ('node1', 'node2'),
                ('node2', 'node4')]))
Exemplo n.º 16
0
    def test_raise_on_cycle_two(self):

        # this condition was arising from ticket:362 and was not treated
        # properly by topological sort

        node1 = 'node1'
        node2 = 'node2'
        node3 = 'node3'
        node4 = 'node4'
        tuples = [(node1, node2), (node3, node1), (node2, node4),
                  (node3, node2), (node2, node3)]
        allitems = self._nodes_from_tuples(tuples)

        try:
            list(topological.sort(tuples, allitems))
            assert False
        except exc.CircularDependencyError, err:
            eq_(err.cycles, set(['node1', 'node3', 'node2']))
            eq_(err.edges, set([('node3', 'node1'), ('node2', 'node3'),
                ('node3', 'node2'), ('node1', 'node2'),
                ('node2','node4')]))
Exemplo n.º 17
0
def sort_tables(tables):
    """sort a collection of Table objects in order of their foreign-key dependency."""

    tables = list(tables)
    tuples = []

    def visit_foreign_key(fkey):
        if fkey.use_alter:
            return
        parent_table = fkey.column.table
        if parent_table in tables:
            child_table = fkey.parent.table
            if parent_table is not child_table:
                tuples.append((parent_table, child_table))

    for table in tables:
        visitors.traverse(table, {'schema_visitor': True},
                          {'foreign_key': visit_foreign_key})

        tuples.extend([parent, table] for parent in table._extra_dependencies)

    return list(topological.sort(tuples, tables))
Exemplo n.º 18
0
 def assert_sort_deterministic(self, tuples, allitems, expected):
     result = list(topological.sort(tuples,
                                    allitems,
                                    deterministic_order=True))
     assert conforms_partial_ordering(tuples, result)
     assert result == expected
Exemplo n.º 19
0
 def assert_sort_deterministic(self, tuples, allitems, expected):
     result = list(
         topological.sort(tuples, allitems, deterministic_order=True))
     assert conforms_partial_ordering(tuples, result)
     assert result == expected