Add a graph for managing dependencies between evolutions and migrations.

Review Request #11231 — Created Oct. 19, 2020 and submitted

Information

Django Evolution
master

Reviewers

This introduces two new classes: DependencyGraph and EvolutionGraph.

DependencyGraph is a generic class for managing dependencies between
nodes and returning an order using a topological sort. Each node
stores a key, an insertion order (the order in which it was added
to the graph), a dictionary of state, and lists of nodes that it depends
on or that depend on it.

Dependencies are lazily-added through the graph, and can be removed.
When all manipulations of the graph are complete, a caller will call
finalize() to record dependencies, validate the graph, and allow
fetching the sorted order of nodes.

The previously-mentioned insertion order is an important implementation
detail in this graph. These work as a sort of loose dependency between
nodes. If two nodes don't define explicit dependencies to specify their
order, the insertion order will be used. This helps keep the order
generally consistent with the evolution ordering that we've used in the
past.

EvolutionGraph specializes this to manage dependencies
between evolutions, migrations, and model creations. An entire list of
evolutions and model creations for an app can be added at once, or an
entire migration plan. The graph will take care of determining the
correct dependencies for these and apply them. It then allows the caller
to retrieve batches of evolutions/migrations/model creations, grouped by
type and in the order indicated by dependencies.

As of this change, the graph isn't used outside of unit tests. Upcoming
changes will switch Evolver over to using the graph to apply
evolutions and migrations in the correct order.

Unit tests pass on all supported versions of Python and Django.

Summary ID
Add a graph for managing dependencies between evolutions and migrations.
This introduces two new classes: `DependencyGraph` and `EvolutionGraph`. `DependencyGraph` is a generic class for managing dependencies between nodes and returning an order using a topological sort. Each node stores a key, an insertion order (the order in which it was added to the graph), a dictionary of state, and lists of nodes that it depends on or that depend on it. Dependencies are lazily-added through the graph, and can be removed. When all manipulations of the graph are complete, a caller will call `finalize()` to record dependencies, validate the graph, and allow fetching the sorted order of nodes. The previously-mentioned insertion order is an important implementation detail in this graph. These work as a sort of loose dependency between nodes. If two nodes don't define explicit dependencies to specify their order, the insertion order will be used. This helps keep the order generally consistent with the evolution ordering that we've used in the past. `EvolutionGraph` specializes this to manage dependencies between evolutions, migrations, and model creations. An entire list of evolutions and model creations for an app can be added at once, or an entire migration plan. The graph will take care of determining the correct dependencies for these and apply them. It then allows the caller to retrieve batches of evolutions/migrations/model creations, grouped by type and in the order indicated by dependencies. As of this change, the graph isn't used outside of unit tests. Upcoming changes will switch `Evolver` over to using the graph to apply evolutions and migrations in the correct order.
f5846d8506fefe49d5bce93adb91cb984ee445a5
Description From Last Updated

F841 local variable 'migrations_app' is assigned to but never used

reviewbotreviewbot

F841 local variable 'migrations_app2' is assigned to but never used

reviewbotreviewbot

F401 'collections.OrderedDict' imported but unused

reviewbotreviewbot

Maybe say set of Node here like you do for dependencies?

daviddavid
Checks run (1 failed, 1 succeeded)
flake8 failed.
JSHint passed.

flake8

chipx86
david
  1. 
      
  2. django_evolution/utils/graph.py (Diff revision 2)
     
     
    Show all issues

    Maybe say set of Node here like you do for dependencies?

  3. 
      
chipx86
Review request changed
Status:
Completed
Change Summary:
Pushed to master (0d14b38)