Commit 8f6dff37 authored by Andrew Godwin's avatar Andrew Godwin
Browse files

Fixed #22485: Include all unmigrated apps in project state by default.

parent 24ec9538
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ class Command(BaseCommand):
        # (makemigrations doesn't look at the database state).
        # Also make sure the graph is built without unmigrated apps shoehorned in.
        loader = MigrationLoader(connections[DEFAULT_DB_ALIAS], load=False)
        loader.build_graph(ignore_unmigrated=True)
        loader.build_graph()

        # Before anything else, see if there's conflicting apps and drop out
        # hard if there are any and they don't want to merge
@@ -78,7 +78,7 @@ class Command(BaseCommand):

        # Set up autodetector
        autodetector = MigrationAutodetector(
            loader.graph.project_state(),
            loader.project_state(),
            ProjectState.from_apps(apps),
            InteractiveMigrationQuestioner(specified_apps=app_labels),
        )
+1 −1
Original line number Diff line number Diff line
@@ -135,7 +135,7 @@ class Command(BaseCommand):
                self.stdout.write("  No migrations needed.")
                # If there's changes that aren't in migrations yet, tell them how to fix it.
                autodetector = MigrationAutodetector(
                    executor.loader.graph.project_state(),
                    executor.loader.project_state(),
                    ProjectState.from_apps(apps),
                )
                changes = autodetector.changes(graph=executor.loader.graph)
+4 −3
Original line number Diff line number Diff line
@@ -50,17 +50,18 @@ class MigrationAutodetector(object):
        old_apps = self.from_state.render()
        new_apps = self.to_state.render()
        # Prepare lists of old/new model keys that we care about
        # (i.e. ignoring proxy ones)
        # (i.e. ignoring proxy ones and unmigrated ones)

        old_model_keys = []
        for al, mn in self.from_state.models.keys():
            model = old_apps.get_model(al, mn)
            if not model._meta.proxy and model._meta.managed:
            if not model._meta.proxy and model._meta.managed and al not in self.from_state.real_apps:
                old_model_keys.append((al, mn))

        new_model_keys = []
        for al, mn in self.to_state.models.keys():
            model = new_apps.get_model(al, mn)
            if not model._meta.proxy and model._meta.managed:
            if not model._meta.proxy and model._meta.managed and al not in self.to_state.real_apps:
                new_model_keys.append((al, mn))

        def _rel_agnostic_fields_def(fields):
+4 −4
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ class MigrationExecutor(object):
        statements = []
        for migration, backwards in plan:
            with self.connection.schema_editor(collect_sql=True) as schema_editor:
                project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
                project_state = self.loader.project_state((migration.app_label, migration.name), at_end=False)
                if not backwards:
                    migration.apply(project_state, schema_editor, collect_sql=True)
                else:
@@ -90,7 +90,7 @@ class MigrationExecutor(object):
            else:
                # Alright, do it normally
                with self.connection.schema_editor() as schema_editor:
                    project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
                    project_state = self.loader.project_state((migration.app_label, migration.name), at_end=False)
                    migration.apply(project_state, schema_editor)
        # For replacement migrations, record individual statuses
        if migration.replaces:
@@ -110,7 +110,7 @@ class MigrationExecutor(object):
            self.progress_callback("unapply_start", migration, fake)
        if not fake:
            with self.connection.schema_editor() as schema_editor:
                project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=False)
                project_state = self.loader.project_state((migration.app_label, migration.name), at_end=False)
                migration.unapply(project_state, schema_editor)
        # For replacement migrations, record individual statuses
        if migration.replaces:
@@ -128,7 +128,7 @@ class MigrationExecutor(object):
        tables it would create exist. This is intended only for use
        on initial migrations (as it only looks for CreateModel).
        """
        project_state = self.loader.graph.project_state((migration.app_label, migration.name), at_end=True)
        project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        apps = project_state.render()
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
+2 −2
Original line number Diff line number Diff line
@@ -121,7 +121,7 @@ class MigrationGraph(object):
    def __str__(self):
        return "Graph: %s nodes, %s edges" % (len(self.nodes), sum(len(x) for x in self.dependencies.values()))

    def project_state(self, nodes=None, at_end=True):
    def make_state(self, nodes=None, at_end=True, real_apps=None):
        """
        Given a migration node or nodes, returns a complete ProjectState for it.
        If at_end is False, returns the state before the migration has run.
@@ -140,7 +140,7 @@ class MigrationGraph(object):
                    if not at_end and migration in nodes:
                        continue
                    plan.append(migration)
        project_state = ProjectState()
        project_state = ProjectState(real_apps=real_apps)
        for node in plan:
            project_state = self.nodes[node].mutate_state(project_state)
        return project_state
Loading