Sort a collection of :class:`_schema.Table` objects based on dependency. This is a dependency-ordered sort which will emit :class:`_schema.Table` objects such that they will follow their dependent :class:`_schema.Table` objects. Tables are dependent on another based on the prese
(
tables: Iterable[TableClause],
skip_fn: Optional[Callable[[ForeignKeyConstraint], bool]] = None,
extra_dependencies: Optional[
typing_Sequence[Tuple[TableClause, TableClause]]
] = None,
)
| 1709 | |
| 1710 | |
| 1711 | def sort_tables( |
| 1712 | tables: Iterable[TableClause], |
| 1713 | skip_fn: Optional[Callable[[ForeignKeyConstraint], bool]] = None, |
| 1714 | extra_dependencies: Optional[ |
| 1715 | typing_Sequence[Tuple[TableClause, TableClause]] |
| 1716 | ] = None, |
| 1717 | ) -> List[Table]: |
| 1718 | """Sort a collection of :class:`_schema.Table` objects based on |
| 1719 | dependency. |
| 1720 | |
| 1721 | This is a dependency-ordered sort which will emit :class:`_schema.Table` |
| 1722 | objects such that they will follow their dependent :class:`_schema.Table` |
| 1723 | objects. |
| 1724 | Tables are dependent on another based on the presence of |
| 1725 | :class:`_schema.ForeignKeyConstraint` |
| 1726 | objects as well as explicit dependencies |
| 1727 | added by :meth:`_schema.Table.add_is_dependent_on`. |
| 1728 | |
| 1729 | .. warning:: |
| 1730 | |
| 1731 | The :func:`._schema.sort_tables` function cannot by itself |
| 1732 | accommodate automatic resolution of dependency cycles between |
| 1733 | tables, which are usually caused by mutually dependent foreign key |
| 1734 | constraints. When these cycles are detected, the foreign keys |
| 1735 | of these tables are omitted from consideration in the sort. |
| 1736 | A warning is emitted when this condition occurs, which will be an |
| 1737 | exception raise in a future release. Tables which are not part |
| 1738 | of the cycle will still be returned in dependency order. |
| 1739 | |
| 1740 | To resolve these cycles, the |
| 1741 | :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be |
| 1742 | applied to those constraints which create a cycle. Alternatively, |
| 1743 | the :func:`_schema.sort_tables_and_constraints` function will |
| 1744 | automatically return foreign key constraints in a separate |
| 1745 | collection when cycles are detected so that they may be applied |
| 1746 | to a schema separately. |
| 1747 | |
| 1748 | :param tables: a sequence of :class:`_schema.Table` objects. |
| 1749 | |
| 1750 | :param skip_fn: optional callable which will be passed a |
| 1751 | :class:`_schema.ForeignKeyConstraint` object; if it returns True, this |
| 1752 | constraint will not be considered as a dependency. Note this is |
| 1753 | **different** from the same parameter in |
| 1754 | :func:`.sort_tables_and_constraints`, which is |
| 1755 | instead passed the owning :class:`_schema.ForeignKeyConstraint` object. |
| 1756 | |
| 1757 | :param extra_dependencies: a sequence of 2-tuples of tables which will |
| 1758 | also be considered as dependent on each other. |
| 1759 | |
| 1760 | .. seealso:: |
| 1761 | |
| 1762 | :func:`.sort_tables_and_constraints` |
| 1763 | |
| 1764 | :attr:`_schema.MetaData.sorted_tables` - uses this function to sort |
| 1765 | |
| 1766 | |
| 1767 | """ |
| 1768 |
nothing calls this directly
no test coverage detected