Commit 2cee1d46 authored by Andrew Godwin's avatar Andrew Godwin
Browse files

Fixed #22861: Internal migrations done first so __first__ works

Thanks to Chris Beaven.
parent 09b63a7c
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ class MigrationGraph(object):
        for node in self.nodes:
            if not any(key[0] == node[0] for key in self.dependencies.get(node, set())) and (not app or app == node[0]):
                roots.add(node)
        return roots
        return sorted(roots)

    def leaf_nodes(self, app=None):
        """
@@ -88,7 +88,7 @@ class MigrationGraph(object):
        for node in self.nodes:
            if not any(key[0] == node[0] for key in self.dependents.get(node, set())) and (not app or app == node[0]):
                leaves.add(node)
        return leaves
        return sorted(leaves)

    def dfs(self, start, get_children):
        """
+11 −0
Original line number Diff line number Diff line
@@ -223,8 +223,19 @@ class MigrationLoader(object):
        self.graph = MigrationGraph()
        for key, migration in normal.items():
            self.graph.add_node(key, migration)
        # Add all internal dependencies first to ensure __first__ dependencies
        # find the correct root node.
        for key, migration in normal.items():
            for parent in migration.dependencies:
                if parent[0] != key[0] or parent[1] == '__first__':
                    # Ignore __first__ references to the same app (#22325)
                    continue
                self.graph.add_dependency(key, parent)
        for key, migration in normal.items():
            for parent in migration.dependencies:
                if parent[0] == key[0]:
                    # Internal dependencies already added.
                    continue
                parent = self.check_key(parent, key[0])
                if parent is not None:
                    self.graph.add_dependency(key, parent)
+4 −4
Original line number Diff line number Diff line
@@ -51,11 +51,11 @@ class GraphTests(TestCase):
        # Test roots and leaves
        self.assertEqual(
            graph.root_nodes(),
            set([('app_a', '0001'), ('app_b', '0001')]),
            [('app_a', '0001'), ('app_b', '0001')],
        )
        self.assertEqual(
            graph.leaf_nodes(),
            set([('app_a', '0004'), ('app_b', '0002')]),
            [('app_a', '0004'), ('app_b', '0002')],
        )

    def test_complex_graph(self):
@@ -105,11 +105,11 @@ class GraphTests(TestCase):
        # Test roots and leaves
        self.assertEqual(
            graph.root_nodes(),
            set([('app_a', '0001'), ('app_b', '0001'), ('app_c', '0001')]),
            [('app_a', '0001'), ('app_b', '0001'), ('app_c', '0001')],
        )
        self.assertEqual(
            graph.leaf_nodes(),
            set([('app_a', '0004'), ('app_b', '0002'), ('app_c', '0002')]),
            [('app_a', '0004'), ('app_b', '0002'), ('app_c', '0002')],
        )

    def test_circular_graph(self):
+20 −0
Original line number Diff line number Diff line
@@ -142,6 +142,26 @@ class LoaderTests(TestCase):
            ],
        )

    @override_settings(MIGRATION_MODULES={
        "migrations": "migrations.test_migrations_first",
        "migrations2": "migrations2.test_migrations_2_first",
    })
    @modify_settings(INSTALLED_APPS={'append': 'migrations2'})
    def test_first(self):
        """
        Makes sure the '__first__' migrations build correctly.
        """
        migration_loader = MigrationLoader(connection)
        self.assertEqual(
            migration_loader.graph.forwards_plan(("migrations", "second")),
            [
                ("migrations", "thefirst"),
                ("migrations2", "0001_initial"),
                ("migrations2", "0002_second"),
                ("migrations", "second"),
            ],
        )

    @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
    def test_name_match(self):
        "Tests prefix name matching"
+0 −0

Empty file added.

Loading