Ticket #22325: circular_dependency.patch

File circular_dependency.patch, 2.1 KB (added by nliberg, 10 years ago)

Fix circular dependency by building dependency graph in 2 stages

  • django/db/migrations/loader.py

    From 32e82c27231b114effe265bd5e1b78a60d131d4b Mon Sep 17 00:00:00 2001
    From: Nils Liberg <nils@nilsliberg.se>
    Date: Mon, 24 Mar 2014 16:36:17 +0100
    Subject: [PATCH] Fix incorrect CircularDependencyError
    
    In the build_graph method of the MigrationLoader class the self.graph.root_nodes(...) method was run on a dependency graph that was not yet fully constructed. This could cause the current node (key) to be incorrectly included among the returned root nodes and since the parent is drawn from this set one could get circular dependencies not present in the real dependency graph.
    
    Perhaps this change will fix the problems described in Django ticket #22325 and #22277.
    ---
     django/db/migrations/loader.py | 8 ++++++--
     1 file changed, 6 insertions(+), 2 deletions(-)
    
    diff --git a/django/db/migrations/loader.py b/django/db/migrations/loader.py
    index ee4f4c9..316ff95 100644
    a b def build_graph(self, ignore_unmigrated=False):  
    194194            self.graph.add_node(key, migration)
    195195        for key, migration in normal.items():
    196196            for parent in migration.dependencies:
     197                if parent is not None and parent[1] != "__first__":
     198                    self.graph.add_dependency(key, parent)
     199        for key, migration in normal.items():
     200            for parent in migration.dependencies:
    197201                # Special-case __first__, which means "the first migration" for
    198202                # migrated apps, and is ignored for unmigrated apps. It allows
    199203                # makemigrations to declare dependencies on apps before they
    def build_graph(self, ignore_unmigrated=False):  
    229233                        parent = list(self.graph.root_nodes(parent[0]))[0]
    230234                    else:
    231235                        raise ValueError("Dependency on unknown app %s" % parent[0])
    232                 if parent is not None:
    233                     self.graph.add_dependency(key, parent)
     236                    if parent is not None:
     237                        self.graph.add_dependency(key, parent)
    234238
    235239    def detect_conflicts(self):
    236240        """
Back to Top