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

This commit is contained in:
Chris Beaven 2014-06-23 13:29:27 +12:00
parent 72d1c4a6dd
commit 29582ad4a3
3 changed files with 25 additions and 4 deletions

View File

@ -132,11 +132,13 @@ class MigrationExecutor(object):
""" """
project_state = self.loader.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() apps = project_state.render()
found_create_migration = False
for operation in migration.operations: for operation in migration.operations:
if isinstance(operation, migrations.CreateModel): if isinstance(operation, migrations.CreateModel):
model = apps.get_model(migration.app_label, operation.name) 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()): if model._meta.db_table not in self.connection.introspection.get_table_list(self.connection.cursor()):
return False return False
else: found_create_migration = True
return False # If we get this far and we found at least one CreateModel migration,
return True # the migration is considered implicitly applied.
return found_create_migration

View File

@ -155,6 +155,12 @@ class ExecutorTests(MigrationTestBase):
self.assertTableNotExists("migrations_author") self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble") self.assertTableNotExists("migrations_tribble")
# Run it normally # Run it normally
self.assertEqual(
executor.migration_plan([("migrations", "0001_initial")]),
[
(executor.loader.graph.nodes["migrations", "0001_initial"], False),
],
)
executor.migrate([("migrations", "0001_initial")]) executor.migrate([("migrations", "0001_initial")])
# Are the tables there now? # Are the tables there now?
self.assertTableExists("migrations_author") self.assertTableExists("migrations_author")
@ -171,9 +177,17 @@ class ExecutorTests(MigrationTestBase):
# Make sure that was faked # Make sure that was faked
self.assertEqual(state["faked"], True) self.assertEqual(state["faked"], True)
# Finally, migrate forwards; this should fake-apply our initial migration # 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")]) executor.migrate([("migrations", "0001_initial")])
self.assertEqual(state["faked"], True) self.assertEqual(state["faked"], True)
# And migrate back to clean up the database # And migrate back to clean up the database
executor.loader.build_graph()
executor.migrate([("migrations", None)]) executor.migrate([("migrations", None)])
self.assertTableNotExists("migrations_author") self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble") self.assertTableNotExists("migrations_tribble")

View File

@ -25,6 +25,11 @@ class Migration(migrations.Migration):
("id", models.AutoField(primary_key=True)), ("id", models.AutoField(primary_key=True)),
("fluffy", models.BooleanField(default=True)), ("fluffy", models.BooleanField(default=True)),
], ],
) ),
migrations.AlterUniqueTogether(
name='author',
unique_together=set([('name', 'slug')]),
),
] ]