Commit 29582ad4 authored by Chris Beaven's avatar Chris Beaven
Browse files

[1.7.x] Fixed #22881 -- Better soft_applied migration detection

parent 72d1c4a6
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -132,11 +132,13 @@ class MigrationExecutor(object):
        """
        project_state = self.loader.project_state((migration.app_label, migration.name), at_end=True)
        apps = project_state.render()
        found_create_migration = False
        for operation in migration.operations:
            if isinstance(operation, migrations.CreateModel):
                model = apps.get_model(migration.app_label, operation.name)
                if model._meta.db_table not in self.connection.introspection.get_table_list(self.connection.cursor()):
                    return False
            else:
                return False
        return True
                found_create_migration = True
        # If we get this far and we found at least one CreateModel migration,
        # the migration is considered implicitly applied.
        return found_create_migration
+14 −0
Original line number Diff line number Diff line
@@ -155,6 +155,12 @@ class ExecutorTests(MigrationTestBase):
        self.assertTableNotExists("migrations_author")
        self.assertTableNotExists("migrations_tribble")
        # Run it normally
        self.assertEqual(
            executor.migration_plan([("migrations", "0001_initial")]),
            [
                (executor.loader.graph.nodes["migrations", "0001_initial"], False),
            ],
        )
        executor.migrate([("migrations", "0001_initial")])
        # Are the tables there now?
        self.assertTableExists("migrations_author")
@@ -171,9 +177,17 @@ class ExecutorTests(MigrationTestBase):
        # Make sure that was faked
        self.assertEqual(state["faked"], True)
        # Finally, migrate forwards; this should fake-apply our initial migration
        executor.loader.build_graph()
        self.assertEqual(
            executor.migration_plan([("migrations", "0001_initial")]),
            [
                (executor.loader.graph.nodes["migrations", "0001_initial"], False),
            ],
        )
        executor.migrate([("migrations", "0001_initial")])
        self.assertEqual(state["faked"], True)
        # And migrate back to clean up the database
        executor.loader.build_graph()
        executor.migrate([("migrations", None)])
        self.assertTableNotExists("migrations_author")
        self.assertTableNotExists("migrations_tribble")
+6 −1
Original line number Diff line number Diff line
@@ -25,6 +25,11 @@ class Migration(migrations.Migration):
                ("id", models.AutoField(primary_key=True)),
                ("fluffy", models.BooleanField(default=True)),
            ],
        )
        ),

        migrations.AlterUniqueTogether(
            name='author',
            unique_together=set([('name', 'slug')]),
        ),

    ]