summaryrefslogtreecommitdiff
path: root/tests/migrations/test_executor.py
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2015-12-19 08:53:53 -0500
committerTim Graham <timograham@gmail.com>2015-12-19 13:37:24 -0500
commitfa9ce4e9a691e4f8c8b093784bac752e488dc60b (patch)
treebf3fd60e63220cb1b229d04962677e885d5018f0 /tests/migrations/test_executor.py
parentb26d147259202455ce356755b729c3dcb16ae9e6 (diff)
Fixed #25922 -- Fixed migrate --fake-initial detection of many-to-many tables.
Diffstat (limited to 'tests/migrations/test_executor.py')
-rw-r--r--tests/migrations/test_executor.py59
1 files changed, 59 insertions, 0 deletions
diff --git a/tests/migrations/test_executor.py b/tests/migrations/test_executor.py
index 32cdc5e09c..1bbed48e3c 100644
--- a/tests/migrations/test_executor.py
+++ b/tests/migrations/test_executor.py
@@ -302,6 +302,65 @@ class ExecutorTests(MigrationTestBase):
self.assertTableNotExists("migrations_tribble")
@override_settings(
+ MIGRATION_MODULES={
+ "migrations": "migrations.test_add_many_to_many_field_initial",
+ },
+ )
+ def test_detect_soft_applied_add_field_manytomanyfield(self):
+ """
+ executor.detect_soft_applied() detects ManyToManyField tables from an
+ AddField operation. This checks the case of AddField in a migration
+ with other operations (0001) and the case of AddField in its own
+ migration (0002).
+ """
+ tables = [
+ # from 0001
+ "migrations_project",
+ "migrations_task",
+ "migrations_project_tasks",
+ # from 0002
+ "migrations_task_projects",
+ ]
+ executor = MigrationExecutor(connection)
+ # Create the tables for 0001 but make it look like the migration hasn't
+ # been applied.
+ executor.migrate([("migrations", "0001_initial")])
+ executor.migrate([("migrations", None)], fake=True)
+ for table in tables[:3]:
+ self.assertTableExists(table)
+ # Table detection sees 0001 is applied but not 0002.
+ migration = executor.loader.get_migration("migrations", "0001_initial")
+ self.assertEqual(executor.detect_soft_applied(None, migration)[0], True)
+ migration = executor.loader.get_migration("migrations", "0002_initial")
+ self.assertEqual(executor.detect_soft_applied(None, migration)[0], False)
+
+ # Create the tables for both migrations but make it look like neither
+ # has been applied.
+ executor.loader.build_graph()
+ executor.migrate([("migrations", "0001_initial")], fake=True)
+ executor.migrate([("migrations", "0002_initial")])
+ executor.loader.build_graph()
+ executor.migrate([("migrations", None)], fake=True)
+ # Table detection sees 0002 is applied.
+ migration = executor.loader.get_migration("migrations", "0002_initial")
+ self.assertEqual(executor.detect_soft_applied(None, migration)[0], True)
+
+ # Leave the tables for 0001 except the many-to-many table. That missing
+ # table should cause detect_soft_applied() to return False.
+ with connection.schema_editor() as editor:
+ for table in tables[2:]:
+ editor.execute(editor.sql_delete_table % {"table": table})
+ migration = executor.loader.get_migration("migrations", "0001_initial")
+ self.assertEqual(executor.detect_soft_applied(None, migration)[0], False)
+
+ # Cleanup by removing the remaining tables.
+ with connection.schema_editor() as editor:
+ for table in tables[:2]:
+ editor.execute(editor.sql_delete_table % {"table": table})
+ for table in tables:
+ self.assertTableNotExists(table)
+
+ @override_settings(
INSTALLED_APPS=[
"migrations.migrations_test_apps.lookuperror_a",
"migrations.migrations_test_apps.lookuperror_b",