1. from unittest import mock
    
  2. 
    
  3. from django.apps.registry import apps as global_apps
    
  4. from django.db import DatabaseError, connection, migrations, models
    
  5. from django.db.migrations.exceptions import InvalidMigrationPlan
    
  6. from django.db.migrations.executor import MigrationExecutor
    
  7. from django.db.migrations.graph import MigrationGraph
    
  8. from django.db.migrations.recorder import MigrationRecorder
    
  9. from django.db.migrations.state import ProjectState
    
  10. from django.test import (
    
  11.     SimpleTestCase,
    
  12.     modify_settings,
    
  13.     override_settings,
    
  14.     skipUnlessDBFeature,
    
  15. )
    
  16. from django.test.utils import isolate_lru_cache
    
  17. 
    
  18. from .test_base import MigrationTestBase
    
  19. 
    
  20. 
    
  21. @modify_settings(INSTALLED_APPS={"append": "migrations2"})
    
  22. class ExecutorTests(MigrationTestBase):
    
  23.     """
    
  24.     Tests the migration executor (full end-to-end running).
    
  25. 
    
  26.     Bear in mind that if these are failing you should fix the other
    
  27.     test failures first, as they may be propagating into here.
    
  28.     """
    
  29. 
    
  30.     available_apps = [
    
  31.         "migrations",
    
  32.         "migrations2",
    
  33.         "django.contrib.auth",
    
  34.         "django.contrib.contenttypes",
    
  35.     ]
    
  36. 
    
  37.     @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
    
  38.     def test_run(self):
    
  39.         """
    
  40.         Tests running a simple set of migrations.
    
  41.         """
    
  42.         executor = MigrationExecutor(connection)
    
  43.         # Let's look at the plan first and make sure it's up to scratch
    
  44.         plan = executor.migration_plan([("migrations", "0002_second")])
    
  45.         self.assertEqual(
    
  46.             plan,
    
  47.             [
    
  48.                 (executor.loader.graph.nodes["migrations", "0001_initial"], False),
    
  49.                 (executor.loader.graph.nodes["migrations", "0002_second"], False),
    
  50.             ],
    
  51.         )
    
  52.         # Were the tables there before?
    
  53.         self.assertTableNotExists("migrations_author")
    
  54.         self.assertTableNotExists("migrations_book")
    
  55.         # Alright, let's try running it
    
  56.         executor.migrate([("migrations", "0002_second")])
    
  57.         # Are the tables there now?
    
  58.         self.assertTableExists("migrations_author")
    
  59.         self.assertTableExists("migrations_book")
    
  60.         # Rebuild the graph to reflect the new DB state
    
  61.         executor.loader.build_graph()
    
  62.         # Alright, let's undo what we did
    
  63.         plan = executor.migration_plan([("migrations", None)])
    
  64.         self.assertEqual(
    
  65.             plan,
    
  66.             [
    
  67.                 (executor.loader.graph.nodes["migrations", "0002_second"], True),
    
  68.                 (executor.loader.graph.nodes["migrations", "0001_initial"], True),
    
  69.             ],
    
  70.         )
    
  71.         executor.migrate([("migrations", None)])
    
  72.         # Are the tables gone?
    
  73.         self.assertTableNotExists("migrations_author")
    
  74.         self.assertTableNotExists("migrations_book")
    
  75. 
    
  76.     @override_settings(
    
  77.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
    
  78.     )
    
  79.     def test_run_with_squashed(self):
    
  80.         """
    
  81.         Tests running a squashed migration from zero (should ignore what it replaces)
    
  82.         """
    
  83.         executor = MigrationExecutor(connection)
    
  84.         # Check our leaf node is the squashed one
    
  85.         leaves = [
    
  86.             key for key in executor.loader.graph.leaf_nodes() if key[0] == "migrations"
    
  87.         ]
    
  88.         self.assertEqual(leaves, [("migrations", "0001_squashed_0002")])
    
  89.         # Check the plan
    
  90.         plan = executor.migration_plan([("migrations", "0001_squashed_0002")])
    
  91.         self.assertEqual(
    
  92.             plan,
    
  93.             [
    
  94.                 (
    
  95.                     executor.loader.graph.nodes["migrations", "0001_squashed_0002"],
    
  96.                     False,
    
  97.                 ),
    
  98.             ],
    
  99.         )
    
  100.         # Were the tables there before?
    
  101.         self.assertTableNotExists("migrations_author")
    
  102.         self.assertTableNotExists("migrations_book")
    
  103.         # Alright, let's try running it
    
  104.         executor.migrate([("migrations", "0001_squashed_0002")])
    
  105.         # Are the tables there now?
    
  106.         self.assertTableExists("migrations_author")
    
  107.         self.assertTableExists("migrations_book")
    
  108.         # Rebuild the graph to reflect the new DB state
    
  109.         executor.loader.build_graph()
    
  110.         # Alright, let's undo what we did. Should also just use squashed.
    
  111.         plan = executor.migration_plan([("migrations", None)])
    
  112.         self.assertEqual(
    
  113.             plan,
    
  114.             [
    
  115.                 (executor.loader.graph.nodes["migrations", "0001_squashed_0002"], True),
    
  116.             ],
    
  117.         )
    
  118.         executor.migrate([("migrations", None)])
    
  119.         # Are the tables gone?
    
  120.         self.assertTableNotExists("migrations_author")
    
  121.         self.assertTableNotExists("migrations_book")
    
  122. 
    
  123.     @override_settings(
    
  124.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"},
    
  125.     )
    
  126.     def test_migrate_backward_to_squashed_migration(self):
    
  127.         executor = MigrationExecutor(connection)
    
  128.         try:
    
  129.             self.assertTableNotExists("migrations_author")
    
  130.             self.assertTableNotExists("migrations_book")
    
  131.             executor.migrate([("migrations", "0001_squashed_0002")])
    
  132.             self.assertTableExists("migrations_author")
    
  133.             self.assertTableExists("migrations_book")
    
  134.             executor.loader.build_graph()
    
  135.             # Migrate backward to a squashed migration.
    
  136.             executor.migrate([("migrations", "0001_initial")])
    
  137.             self.assertTableExists("migrations_author")
    
  138.             self.assertTableNotExists("migrations_book")
    
  139.         finally:
    
  140.             # Unmigrate everything.
    
  141.             executor = MigrationExecutor(connection)
    
  142.             executor.migrate([("migrations", None)])
    
  143.             self.assertTableNotExists("migrations_author")
    
  144.             self.assertTableNotExists("migrations_book")
    
  145. 
    
  146.     @override_settings(
    
  147.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_non_atomic"}
    
  148.     )
    
  149.     def test_non_atomic_migration(self):
    
  150.         """
    
  151.         Applying a non-atomic migration works as expected.
    
  152.         """
    
  153.         executor = MigrationExecutor(connection)
    
  154.         with self.assertRaisesMessage(RuntimeError, "Abort migration"):
    
  155.             executor.migrate([("migrations", "0001_initial")])
    
  156.         self.assertTableExists("migrations_publisher")
    
  157.         migrations_apps = executor.loader.project_state(
    
  158.             ("migrations", "0001_initial")
    
  159.         ).apps
    
  160.         Publisher = migrations_apps.get_model("migrations", "Publisher")
    
  161.         self.assertTrue(Publisher.objects.exists())
    
  162.         self.assertTableNotExists("migrations_book")
    
  163. 
    
  164.     @override_settings(
    
  165.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_atomic_operation"}
    
  166.     )
    
  167.     def test_atomic_operation_in_non_atomic_migration(self):
    
  168.         """
    
  169.         An atomic operation is properly rolled back inside a non-atomic
    
  170.         migration.
    
  171.         """
    
  172.         executor = MigrationExecutor(connection)
    
  173.         with self.assertRaisesMessage(RuntimeError, "Abort migration"):
    
  174.             executor.migrate([("migrations", "0001_initial")])
    
  175.         migrations_apps = executor.loader.project_state(
    
  176.             ("migrations", "0001_initial")
    
  177.         ).apps
    
  178.         Editor = migrations_apps.get_model("migrations", "Editor")
    
  179.         self.assertFalse(Editor.objects.exists())
    
  180.         # Record previous migration as successful.
    
  181.         executor.migrate([("migrations", "0001_initial")], fake=True)
    
  182.         # Rebuild the graph to reflect the new DB state.
    
  183.         executor.loader.build_graph()
    
  184.         # Migrating backwards is also atomic.
    
  185.         with self.assertRaisesMessage(RuntimeError, "Abort migration"):
    
  186.             executor.migrate([("migrations", None)])
    
  187.         self.assertFalse(Editor.objects.exists())
    
  188. 
    
  189.     @override_settings(
    
  190.         MIGRATION_MODULES={
    
  191.             "migrations": "migrations.test_migrations",
    
  192.             "migrations2": "migrations2.test_migrations_2",
    
  193.         }
    
  194.     )
    
  195.     def test_empty_plan(self):
    
  196.         """
    
  197.         Re-planning a full migration of a fully-migrated set doesn't
    
  198.         perform spurious unmigrations and remigrations.
    
  199. 
    
  200.         There was previously a bug where the executor just always performed the
    
  201.         backwards plan for applied migrations - which even for the most recent
    
  202.         migration in an app, might include other, dependent apps, and these
    
  203.         were being unmigrated.
    
  204.         """
    
  205.         # Make the initial plan, check it
    
  206.         executor = MigrationExecutor(connection)
    
  207.         plan = executor.migration_plan(
    
  208.             [
    
  209.                 ("migrations", "0002_second"),
    
  210.                 ("migrations2", "0001_initial"),
    
  211.             ]
    
  212.         )
    
  213.         self.assertEqual(
    
  214.             plan,
    
  215.             [
    
  216.                 (executor.loader.graph.nodes["migrations", "0001_initial"], False),
    
  217.                 (executor.loader.graph.nodes["migrations", "0002_second"], False),
    
  218.                 (executor.loader.graph.nodes["migrations2", "0001_initial"], False),
    
  219.             ],
    
  220.         )
    
  221.         # Fake-apply all migrations
    
  222.         executor.migrate(
    
  223.             [("migrations", "0002_second"), ("migrations2", "0001_initial")], fake=True
    
  224.         )
    
  225.         # Rebuild the graph to reflect the new DB state
    
  226.         executor.loader.build_graph()
    
  227.         # Now plan a second time and make sure it's empty
    
  228.         plan = executor.migration_plan(
    
  229.             [
    
  230.                 ("migrations", "0002_second"),
    
  231.                 ("migrations2", "0001_initial"),
    
  232.             ]
    
  233.         )
    
  234.         self.assertEqual(plan, [])
    
  235.         # The resulting state should include applied migrations.
    
  236.         state = executor.migrate(
    
  237.             [
    
  238.                 ("migrations", "0002_second"),
    
  239.                 ("migrations2", "0001_initial"),
    
  240.             ]
    
  241.         )
    
  242.         self.assertIn(("migrations", "book"), state.models)
    
  243.         self.assertIn(("migrations", "author"), state.models)
    
  244.         self.assertIn(("migrations2", "otherauthor"), state.models)
    
  245.         # Erase all the fake records
    
  246.         executor.recorder.record_unapplied("migrations2", "0001_initial")
    
  247.         executor.recorder.record_unapplied("migrations", "0002_second")
    
  248.         executor.recorder.record_unapplied("migrations", "0001_initial")
    
  249. 
    
  250.     @override_settings(
    
  251.         MIGRATION_MODULES={
    
  252.             "migrations": "migrations.test_migrations",
    
  253.             "migrations2": "migrations2.test_migrations_2_no_deps",
    
  254.         }
    
  255.     )
    
  256.     def test_mixed_plan_not_supported(self):
    
  257.         """
    
  258.         Although the MigrationExecutor interfaces allows for mixed migration
    
  259.         plans (combined forwards and backwards migrations) this is not
    
  260.         supported.
    
  261.         """
    
  262.         # Prepare for mixed plan
    
  263.         executor = MigrationExecutor(connection)
    
  264.         plan = executor.migration_plan([("migrations", "0002_second")])
    
  265.         self.assertEqual(
    
  266.             plan,
    
  267.             [
    
  268.                 (executor.loader.graph.nodes["migrations", "0001_initial"], False),
    
  269.                 (executor.loader.graph.nodes["migrations", "0002_second"], False),
    
  270.             ],
    
  271.         )
    
  272.         executor.migrate(None, plan)
    
  273.         # Rebuild the graph to reflect the new DB state
    
  274.         executor.loader.build_graph()
    
  275.         self.assertIn(
    
  276.             ("migrations", "0001_initial"), executor.loader.applied_migrations
    
  277.         )
    
  278.         self.assertIn(("migrations", "0002_second"), executor.loader.applied_migrations)
    
  279.         self.assertNotIn(
    
  280.             ("migrations2", "0001_initial"), executor.loader.applied_migrations
    
  281.         )
    
  282. 
    
  283.         # Generate mixed plan
    
  284.         plan = executor.migration_plan(
    
  285.             [
    
  286.                 ("migrations", None),
    
  287.                 ("migrations2", "0001_initial"),
    
  288.             ]
    
  289.         )
    
  290.         msg = (
    
  291.             "Migration plans with both forwards and backwards migrations are "
    
  292.             "not supported. Please split your migration process into separate "
    
  293.             "plans of only forwards OR backwards migrations."
    
  294.         )
    
  295.         with self.assertRaisesMessage(InvalidMigrationPlan, msg) as cm:
    
  296.             executor.migrate(None, plan)
    
  297.         self.assertEqual(
    
  298.             cm.exception.args[1],
    
  299.             [
    
  300.                 (executor.loader.graph.nodes["migrations", "0002_second"], True),
    
  301.                 (executor.loader.graph.nodes["migrations", "0001_initial"], True),
    
  302.                 (executor.loader.graph.nodes["migrations2", "0001_initial"], False),
    
  303.             ],
    
  304.         )
    
  305.         # Rebuild the graph to reflect the new DB state
    
  306.         executor.loader.build_graph()
    
  307.         executor.migrate(
    
  308.             [
    
  309.                 ("migrations", None),
    
  310.                 ("migrations2", None),
    
  311.             ]
    
  312.         )
    
  313.         # Are the tables gone?
    
  314.         self.assertTableNotExists("migrations_author")
    
  315.         self.assertTableNotExists("migrations_book")
    
  316.         self.assertTableNotExists("migrations2_otherauthor")
    
  317. 
    
  318.     @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
    
  319.     def test_soft_apply(self):
    
  320.         """
    
  321.         Tests detection of initial migrations already having been applied.
    
  322.         """
    
  323.         state = {"faked": None}
    
  324. 
    
  325.         def fake_storer(phase, migration=None, fake=None):
    
  326.             state["faked"] = fake
    
  327. 
    
  328.         executor = MigrationExecutor(connection, progress_callback=fake_storer)
    
  329.         # Were the tables there before?
    
  330.         self.assertTableNotExists("migrations_author")
    
  331.         self.assertTableNotExists("migrations_tribble")
    
  332.         # Run it normally
    
  333.         self.assertEqual(
    
  334.             executor.migration_plan([("migrations", "0001_initial")]),
    
  335.             [
    
  336.                 (executor.loader.graph.nodes["migrations", "0001_initial"], False),
    
  337.             ],
    
  338.         )
    
  339.         executor.migrate([("migrations", "0001_initial")])
    
  340.         # Are the tables there now?
    
  341.         self.assertTableExists("migrations_author")
    
  342.         self.assertTableExists("migrations_tribble")
    
  343.         # We shouldn't have faked that one
    
  344.         self.assertIs(state["faked"], False)
    
  345.         # Rebuild the graph to reflect the new DB state
    
  346.         executor.loader.build_graph()
    
  347.         # Fake-reverse that
    
  348.         executor.migrate([("migrations", None)], fake=True)
    
  349.         # Are the tables still there?
    
  350.         self.assertTableExists("migrations_author")
    
  351.         self.assertTableExists("migrations_tribble")
    
  352.         # Make sure that was faked
    
  353.         self.assertIs(state["faked"], True)
    
  354.         # Finally, migrate forwards; this should fake-apply our initial migration
    
  355.         executor.loader.build_graph()
    
  356.         self.assertEqual(
    
  357.             executor.migration_plan([("migrations", "0001_initial")]),
    
  358.             [
    
  359.                 (executor.loader.graph.nodes["migrations", "0001_initial"], False),
    
  360.             ],
    
  361.         )
    
  362.         # Applying the migration should raise a database level error
    
  363.         # because we haven't given the --fake-initial option
    
  364.         with self.assertRaises(DatabaseError):
    
  365.             executor.migrate([("migrations", "0001_initial")])
    
  366.         # Reset the faked state
    
  367.         state = {"faked": None}
    
  368.         # Allow faking of initial CreateModel operations
    
  369.         executor.migrate([("migrations", "0001_initial")], fake_initial=True)
    
  370.         self.assertIs(state["faked"], True)
    
  371.         # And migrate back to clean up the database
    
  372.         executor.loader.build_graph()
    
  373.         executor.migrate([("migrations", None)])
    
  374.         self.assertTableNotExists("migrations_author")
    
  375.         self.assertTableNotExists("migrations_tribble")
    
  376. 
    
  377.     @override_settings(
    
  378.         MIGRATION_MODULES={
    
  379.             "migrations": "migrations.test_migrations_custom_user",
    
  380.             "django.contrib.auth": "django.contrib.auth.migrations",
    
  381.         },
    
  382.         AUTH_USER_MODEL="migrations.Author",
    
  383.     )
    
  384.     def test_custom_user(self):
    
  385.         """
    
  386.         Regression test for #22325 - references to a custom user model defined in the
    
  387.         same app are not resolved correctly.
    
  388.         """
    
  389.         with isolate_lru_cache(global_apps.get_swappable_settings_name):
    
  390.             executor = MigrationExecutor(connection)
    
  391.             self.assertTableNotExists("migrations_author")
    
  392.             self.assertTableNotExists("migrations_tribble")
    
  393.             # Migrate forwards
    
  394.             executor.migrate([("migrations", "0001_initial")])
    
  395.             self.assertTableExists("migrations_author")
    
  396.             self.assertTableExists("migrations_tribble")
    
  397.             # The soft-application detection works.
    
  398.             # Change table_names to not return auth_user during this as it
    
  399.             # wouldn't be there in a normal run, and ensure migrations.Author
    
  400.             # exists in the global app registry temporarily.
    
  401.             old_table_names = connection.introspection.table_names
    
  402.             connection.introspection.table_names = lambda c: [
    
  403.                 x for x in old_table_names(c) if x != "auth_user"
    
  404.             ]
    
  405.             migrations_apps = executor.loader.project_state(
    
  406.                 ("migrations", "0001_initial"),
    
  407.             ).apps
    
  408.             global_apps.get_app_config("migrations").models[
    
  409.                 "author"
    
  410.             ] = migrations_apps.get_model("migrations", "author")
    
  411.             try:
    
  412.                 migration = executor.loader.get_migration("auth", "0001_initial")
    
  413.                 self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
    
  414.             finally:
    
  415.                 connection.introspection.table_names = old_table_names
    
  416.                 del global_apps.get_app_config("migrations").models["author"]
    
  417.                 # Migrate back to clean up the database.
    
  418.                 executor.loader.build_graph()
    
  419.                 executor.migrate([("migrations", None)])
    
  420.                 self.assertTableNotExists("migrations_author")
    
  421.                 self.assertTableNotExists("migrations_tribble")
    
  422. 
    
  423.     @override_settings(
    
  424.         MIGRATION_MODULES={
    
  425.             "migrations": "migrations.test_add_many_to_many_field_initial",
    
  426.         },
    
  427.     )
    
  428.     def test_detect_soft_applied_add_field_manytomanyfield(self):
    
  429.         """
    
  430.         executor.detect_soft_applied() detects ManyToManyField tables from an
    
  431.         AddField operation. This checks the case of AddField in a migration
    
  432.         with other operations (0001) and the case of AddField in its own
    
  433.         migration (0002).
    
  434.         """
    
  435.         tables = [
    
  436.             # from 0001
    
  437.             "migrations_project",
    
  438.             "migrations_task",
    
  439.             "migrations_project_tasks",
    
  440.             # from 0002
    
  441.             "migrations_task_projects",
    
  442.         ]
    
  443.         executor = MigrationExecutor(connection)
    
  444.         # Create the tables for 0001 but make it look like the migration hasn't
    
  445.         # been applied.
    
  446.         executor.migrate([("migrations", "0001_initial")])
    
  447.         executor.migrate([("migrations", None)], fake=True)
    
  448.         for table in tables[:3]:
    
  449.             self.assertTableExists(table)
    
  450.         # Table detection sees 0001 is applied but not 0002.
    
  451.         migration = executor.loader.get_migration("migrations", "0001_initial")
    
  452.         self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
    
  453.         migration = executor.loader.get_migration("migrations", "0002_initial")
    
  454.         self.assertIs(executor.detect_soft_applied(None, migration)[0], False)
    
  455. 
    
  456.         # Create the tables for both migrations but make it look like neither
    
  457.         # has been applied.
    
  458.         executor.loader.build_graph()
    
  459.         executor.migrate([("migrations", "0001_initial")], fake=True)
    
  460.         executor.migrate([("migrations", "0002_initial")])
    
  461.         executor.loader.build_graph()
    
  462.         executor.migrate([("migrations", None)], fake=True)
    
  463.         # Table detection sees 0002 is applied.
    
  464.         migration = executor.loader.get_migration("migrations", "0002_initial")
    
  465.         self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
    
  466. 
    
  467.         # Leave the tables for 0001 except the many-to-many table. That missing
    
  468.         # table should cause detect_soft_applied() to return False.
    
  469.         with connection.schema_editor() as editor:
    
  470.             for table in tables[2:]:
    
  471.                 editor.execute(editor.sql_delete_table % {"table": table})
    
  472.         migration = executor.loader.get_migration("migrations", "0001_initial")
    
  473.         self.assertIs(executor.detect_soft_applied(None, migration)[0], False)
    
  474. 
    
  475.         # Cleanup by removing the remaining tables.
    
  476.         with connection.schema_editor() as editor:
    
  477.             for table in tables[:2]:
    
  478.                 editor.execute(editor.sql_delete_table % {"table": table})
    
  479.         for table in tables:
    
  480.             self.assertTableNotExists(table)
    
  481. 
    
  482.     @override_settings(
    
  483.         INSTALLED_APPS=[
    
  484.             "migrations.migrations_test_apps.lookuperror_a",
    
  485.             "migrations.migrations_test_apps.lookuperror_b",
    
  486.             "migrations.migrations_test_apps.lookuperror_c",
    
  487.         ]
    
  488.     )
    
  489.     def test_unrelated_model_lookups_forwards(self):
    
  490.         """
    
  491.         #24123 - All models of apps already applied which are
    
  492.         unrelated to the first app being applied are part of the initial model
    
  493.         state.
    
  494.         """
    
  495.         try:
    
  496.             executor = MigrationExecutor(connection)
    
  497.             self.assertTableNotExists("lookuperror_a_a1")
    
  498.             self.assertTableNotExists("lookuperror_b_b1")
    
  499.             self.assertTableNotExists("lookuperror_c_c1")
    
  500.             executor.migrate([("lookuperror_b", "0003_b3")])
    
  501.             self.assertTableExists("lookuperror_b_b3")
    
  502.             # Rebuild the graph to reflect the new DB state
    
  503.             executor.loader.build_graph()
    
  504. 
    
  505.             # Migrate forwards -- This led to a lookup LookupErrors because
    
  506.             # lookuperror_b.B2 is already applied
    
  507.             executor.migrate(
    
  508.                 [
    
  509.                     ("lookuperror_a", "0004_a4"),
    
  510.                     ("lookuperror_c", "0003_c3"),
    
  511.                 ]
    
  512.             )
    
  513.             self.assertTableExists("lookuperror_a_a4")
    
  514.             self.assertTableExists("lookuperror_c_c3")
    
  515. 
    
  516.             # Rebuild the graph to reflect the new DB state
    
  517.             executor.loader.build_graph()
    
  518.         finally:
    
  519.             # Cleanup
    
  520.             executor.migrate(
    
  521.                 [
    
  522.                     ("lookuperror_a", None),
    
  523.                     ("lookuperror_b", None),
    
  524.                     ("lookuperror_c", None),
    
  525.                 ]
    
  526.             )
    
  527.             self.assertTableNotExists("lookuperror_a_a1")
    
  528.             self.assertTableNotExists("lookuperror_b_b1")
    
  529.             self.assertTableNotExists("lookuperror_c_c1")
    
  530. 
    
  531.     @override_settings(
    
  532.         INSTALLED_APPS=[
    
  533.             "migrations.migrations_test_apps.lookuperror_a",
    
  534.             "migrations.migrations_test_apps.lookuperror_b",
    
  535.             "migrations.migrations_test_apps.lookuperror_c",
    
  536.         ]
    
  537.     )
    
  538.     def test_unrelated_model_lookups_backwards(self):
    
  539.         """
    
  540.         #24123 - All models of apps being unapplied which are
    
  541.         unrelated to the first app being unapplied are part of the initial
    
  542.         model state.
    
  543.         """
    
  544.         try:
    
  545.             executor = MigrationExecutor(connection)
    
  546.             self.assertTableNotExists("lookuperror_a_a1")
    
  547.             self.assertTableNotExists("lookuperror_b_b1")
    
  548.             self.assertTableNotExists("lookuperror_c_c1")
    
  549.             executor.migrate(
    
  550.                 [
    
  551.                     ("lookuperror_a", "0004_a4"),
    
  552.                     ("lookuperror_b", "0003_b3"),
    
  553.                     ("lookuperror_c", "0003_c3"),
    
  554.                 ]
    
  555.             )
    
  556.             self.assertTableExists("lookuperror_b_b3")
    
  557.             self.assertTableExists("lookuperror_a_a4")
    
  558.             self.assertTableExists("lookuperror_c_c3")
    
  559.             # Rebuild the graph to reflect the new DB state
    
  560.             executor.loader.build_graph()
    
  561. 
    
  562.             # Migrate backwards -- This led to a lookup LookupErrors because
    
  563.             # lookuperror_b.B2 is not in the initial state (unrelated to app c)
    
  564.             executor.migrate([("lookuperror_a", None)])
    
  565. 
    
  566.             # Rebuild the graph to reflect the new DB state
    
  567.             executor.loader.build_graph()
    
  568.         finally:
    
  569.             # Cleanup
    
  570.             executor.migrate([("lookuperror_b", None), ("lookuperror_c", None)])
    
  571.             self.assertTableNotExists("lookuperror_a_a1")
    
  572.             self.assertTableNotExists("lookuperror_b_b1")
    
  573.             self.assertTableNotExists("lookuperror_c_c1")
    
  574. 
    
  575.     @override_settings(
    
  576.         INSTALLED_APPS=[
    
  577.             "migrations.migrations_test_apps.mutate_state_a",
    
  578.             "migrations.migrations_test_apps.mutate_state_b",
    
  579.         ]
    
  580.     )
    
  581.     def test_unrelated_applied_migrations_mutate_state(self):
    
  582.         """
    
  583.         #26647 - Unrelated applied migrations should be part of the final
    
  584.         state in both directions.
    
  585.         """
    
  586.         executor = MigrationExecutor(connection)
    
  587.         executor.migrate(
    
  588.             [
    
  589.                 ("mutate_state_b", "0002_add_field"),
    
  590.             ]
    
  591.         )
    
  592.         # Migrate forward.
    
  593.         executor.loader.build_graph()
    
  594.         state = executor.migrate(
    
  595.             [
    
  596.                 ("mutate_state_a", "0001_initial"),
    
  597.             ]
    
  598.         )
    
  599.         self.assertIn("added", state.models["mutate_state_b", "b"].fields)
    
  600.         executor.loader.build_graph()
    
  601.         # Migrate backward.
    
  602.         state = executor.migrate(
    
  603.             [
    
  604.                 ("mutate_state_a", None),
    
  605.             ]
    
  606.         )
    
  607.         self.assertIn("added", state.models["mutate_state_b", "b"].fields)
    
  608.         executor.migrate(
    
  609.             [
    
  610.                 ("mutate_state_b", None),
    
  611.             ]
    
  612.         )
    
  613. 
    
  614.     @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
    
  615.     def test_process_callback(self):
    
  616.         """
    
  617.         #24129 - Tests callback process
    
  618.         """
    
  619.         call_args_list = []
    
  620. 
    
  621.         def callback(*args):
    
  622.             call_args_list.append(args)
    
  623. 
    
  624.         executor = MigrationExecutor(connection, progress_callback=callback)
    
  625.         # Were the tables there before?
    
  626.         self.assertTableNotExists("migrations_author")
    
  627.         self.assertTableNotExists("migrations_tribble")
    
  628.         executor.migrate(
    
  629.             [
    
  630.                 ("migrations", "0001_initial"),
    
  631.                 ("migrations", "0002_second"),
    
  632.             ]
    
  633.         )
    
  634.         # Rebuild the graph to reflect the new DB state
    
  635.         executor.loader.build_graph()
    
  636. 
    
  637.         executor.migrate(
    
  638.             [
    
  639.                 ("migrations", None),
    
  640.                 ("migrations", None),
    
  641.             ]
    
  642.         )
    
  643.         self.assertTableNotExists("migrations_author")
    
  644.         self.assertTableNotExists("migrations_tribble")
    
  645. 
    
  646.         migrations = executor.loader.graph.nodes
    
  647.         expected = [
    
  648.             ("render_start",),
    
  649.             ("render_success",),
    
  650.             ("apply_start", migrations["migrations", "0001_initial"], False),
    
  651.             ("apply_success", migrations["migrations", "0001_initial"], False),
    
  652.             ("apply_start", migrations["migrations", "0002_second"], False),
    
  653.             ("apply_success", migrations["migrations", "0002_second"], False),
    
  654.             ("render_start",),
    
  655.             ("render_success",),
    
  656.             ("unapply_start", migrations["migrations", "0002_second"], False),
    
  657.             ("unapply_success", migrations["migrations", "0002_second"], False),
    
  658.             ("unapply_start", migrations["migrations", "0001_initial"], False),
    
  659.             ("unapply_success", migrations["migrations", "0001_initial"], False),
    
  660.         ]
    
  661.         self.assertEqual(call_args_list, expected)
    
  662. 
    
  663.     @override_settings(
    
  664.         INSTALLED_APPS=[
    
  665.             "migrations.migrations_test_apps.alter_fk.author_app",
    
  666.             "migrations.migrations_test_apps.alter_fk.book_app",
    
  667.         ]
    
  668.     )
    
  669.     def test_alter_id_type_with_fk(self):
    
  670.         try:
    
  671.             executor = MigrationExecutor(connection)
    
  672.             self.assertTableNotExists("author_app_author")
    
  673.             self.assertTableNotExists("book_app_book")
    
  674.             # Apply initial migrations
    
  675.             executor.migrate(
    
  676.                 [
    
  677.                     ("author_app", "0001_initial"),
    
  678.                     ("book_app", "0001_initial"),
    
  679.                 ]
    
  680.             )
    
  681.             self.assertTableExists("author_app_author")
    
  682.             self.assertTableExists("book_app_book")
    
  683.             # Rebuild the graph to reflect the new DB state
    
  684.             executor.loader.build_graph()
    
  685. 
    
  686.             # Apply PK type alteration
    
  687.             executor.migrate([("author_app", "0002_alter_id")])
    
  688. 
    
  689.             # Rebuild the graph to reflect the new DB state
    
  690.             executor.loader.build_graph()
    
  691.         finally:
    
  692.             # We can't simply unapply the migrations here because there is no
    
  693.             # implicit cast from VARCHAR to INT on the database level.
    
  694.             with connection.schema_editor() as editor:
    
  695.                 editor.execute(editor.sql_delete_table % {"table": "book_app_book"})
    
  696.                 editor.execute(editor.sql_delete_table % {"table": "author_app_author"})
    
  697.             self.assertTableNotExists("author_app_author")
    
  698.             self.assertTableNotExists("book_app_book")
    
  699.             executor.migrate([("author_app", None)], fake=True)
    
  700. 
    
  701.     @override_settings(
    
  702.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
    
  703.     )
    
  704.     def test_apply_all_replaced_marks_replacement_as_applied(self):
    
  705.         """
    
  706.         Applying all replaced migrations marks replacement as applied (#24628).
    
  707.         """
    
  708.         recorder = MigrationRecorder(connection)
    
  709.         # Place the database in a state where the replaced migrations are
    
  710.         # partially applied: 0001 is applied, 0002 is not.
    
  711.         recorder.record_applied("migrations", "0001_initial")
    
  712.         executor = MigrationExecutor(connection)
    
  713.         # Use fake because we don't actually have the first migration
    
  714.         # applied, so the second will fail. And there's no need to actually
    
  715.         # create/modify tables here, we're just testing the
    
  716.         # MigrationRecord, which works the same with or without fake.
    
  717.         executor.migrate([("migrations", "0002_second")], fake=True)
    
  718. 
    
  719.         # Because we've now applied 0001 and 0002 both, their squashed
    
  720.         # replacement should be marked as applied.
    
  721.         self.assertIn(
    
  722.             ("migrations", "0001_squashed_0002"),
    
  723.             recorder.applied_migrations(),
    
  724.         )
    
  725. 
    
  726.     @override_settings(
    
  727.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
    
  728.     )
    
  729.     def test_migrate_marks_replacement_applied_even_if_it_did_nothing(self):
    
  730.         """
    
  731.         A new squash migration will be marked as applied even if all its
    
  732.         replaced migrations were previously already applied (#24628).
    
  733.         """
    
  734.         recorder = MigrationRecorder(connection)
    
  735.         # Record all replaced migrations as applied
    
  736.         recorder.record_applied("migrations", "0001_initial")
    
  737.         recorder.record_applied("migrations", "0002_second")
    
  738.         executor = MigrationExecutor(connection)
    
  739.         executor.migrate([("migrations", "0001_squashed_0002")])
    
  740. 
    
  741.         # Because 0001 and 0002 are both applied, even though this migrate run
    
  742.         # didn't apply anything new, their squashed replacement should be
    
  743.         # marked as applied.
    
  744.         self.assertIn(
    
  745.             ("migrations", "0001_squashed_0002"),
    
  746.             recorder.applied_migrations(),
    
  747.         )
    
  748. 
    
  749.     @override_settings(
    
  750.         MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
    
  751.     )
    
  752.     def test_migrate_marks_replacement_unapplied(self):
    
  753.         executor = MigrationExecutor(connection)
    
  754.         executor.migrate([("migrations", "0001_squashed_0002")])
    
  755.         try:
    
  756.             self.assertIn(
    
  757.                 ("migrations", "0001_squashed_0002"),
    
  758.                 executor.recorder.applied_migrations(),
    
  759.             )
    
  760.         finally:
    
  761.             executor.loader.build_graph()
    
  762.             executor.migrate([("migrations", None)])
    
  763.             self.assertNotIn(
    
  764.                 ("migrations", "0001_squashed_0002"),
    
  765.                 executor.recorder.applied_migrations(),
    
  766.             )
    
  767. 
    
  768.     # When the feature is False, the operation and the record won't be
    
  769.     # performed in a transaction and the test will systematically pass.
    
  770.     @skipUnlessDBFeature("can_rollback_ddl")
    
  771.     def test_migrations_applied_and_recorded_atomically(self):
    
  772.         """Migrations are applied and recorded atomically."""
    
  773. 
    
  774.         class Migration(migrations.Migration):
    
  775.             operations = [
    
  776.                 migrations.CreateModel(
    
  777.                     "model",
    
  778.                     [
    
  779.                         ("id", models.AutoField(primary_key=True)),
    
  780.                     ],
    
  781.                 ),
    
  782.             ]
    
  783. 
    
  784.         executor = MigrationExecutor(connection)
    
  785.         with mock.patch(
    
  786.             "django.db.migrations.executor.MigrationExecutor.record_migration"
    
  787.         ) as record_migration:
    
  788.             record_migration.side_effect = RuntimeError("Recording migration failed.")
    
  789.             with self.assertRaisesMessage(RuntimeError, "Recording migration failed."):
    
  790.                 executor.apply_migration(
    
  791.                     ProjectState(),
    
  792.                     Migration("0001_initial", "record_migration"),
    
  793.                 )
    
  794.                 executor.migrate([("migrations", "0001_initial")])
    
  795.         # The migration isn't recorded as applied since it failed.
    
  796.         migration_recorder = MigrationRecorder(connection)
    
  797.         self.assertIs(
    
  798.             migration_recorder.migration_qs.filter(
    
  799.                 app="record_migration",
    
  800.                 name="0001_initial",
    
  801.             ).exists(),
    
  802.             False,
    
  803.         )
    
  804.         self.assertTableNotExists("record_migration_model")
    
  805. 
    
  806.     def test_migrations_not_applied_on_deferred_sql_failure(self):
    
  807.         """Migrations are not recorded if deferred SQL application fails."""
    
  808. 
    
  809.         class DeferredSQL:
    
  810.             def __str__(self):
    
  811.                 raise DatabaseError("Failed to apply deferred SQL")
    
  812. 
    
  813.         class Migration(migrations.Migration):
    
  814.             atomic = False
    
  815. 
    
  816.             def apply(self, project_state, schema_editor, collect_sql=False):
    
  817.                 schema_editor.deferred_sql.append(DeferredSQL())
    
  818. 
    
  819.         executor = MigrationExecutor(connection)
    
  820.         with self.assertRaisesMessage(DatabaseError, "Failed to apply deferred SQL"):
    
  821.             executor.apply_migration(
    
  822.                 ProjectState(),
    
  823.                 Migration("0001_initial", "deferred_sql"),
    
  824.             )
    
  825.         # The migration isn't recorded as applied since it failed.
    
  826.         migration_recorder = MigrationRecorder(connection)
    
  827.         self.assertIs(
    
  828.             migration_recorder.migration_qs.filter(
    
  829.                 app="deferred_sql",
    
  830.                 name="0001_initial",
    
  831.             ).exists(),
    
  832.             False,
    
  833.         )
    
  834. 
    
  835.     @mock.patch.object(MigrationRecorder, "has_table", return_value=False)
    
  836.     def test_migrate_skips_schema_creation(self, mocked_has_table):
    
  837.         """
    
  838.         The django_migrations table is not created if there are no migrations
    
  839.         to record.
    
  840.         """
    
  841.         executor = MigrationExecutor(connection)
    
  842.         # 0 queries, since the query for has_table is being mocked.
    
  843.         with self.assertNumQueries(0):
    
  844.             executor.migrate([], plan=[])
    
  845. 
    
  846. 
    
  847. class FakeLoader:
    
  848.     def __init__(self, graph, applied):
    
  849.         self.graph = graph
    
  850.         self.applied_migrations = applied
    
  851.         self.replace_migrations = True
    
  852. 
    
  853. 
    
  854. class FakeMigration:
    
  855.     """Really all we need is any object with a debug-useful repr."""
    
  856. 
    
  857.     def __init__(self, name):
    
  858.         self.name = name
    
  859. 
    
  860.     def __repr__(self):
    
  861.         return "M<%s>" % self.name
    
  862. 
    
  863. 
    
  864. class ExecutorUnitTests(SimpleTestCase):
    
  865.     """(More) isolated unit tests for executor methods."""
    
  866. 
    
  867.     def test_minimize_rollbacks(self):
    
  868.         """
    
  869.         Minimize unnecessary rollbacks in connected apps.
    
  870. 
    
  871.         When you say "./manage.py migrate appA 0001", rather than migrating to
    
  872.         just after appA-0001 in the linearized migration plan (which could roll
    
  873.         back migrations in other apps that depend on appA 0001, but don't need
    
  874.         to be rolled back since we're not rolling back appA 0001), we migrate
    
  875.         to just before appA-0002.
    
  876.         """
    
  877.         a1_impl = FakeMigration("a1")
    
  878.         a1 = ("a", "1")
    
  879.         a2_impl = FakeMigration("a2")
    
  880.         a2 = ("a", "2")
    
  881.         b1_impl = FakeMigration("b1")
    
  882.         b1 = ("b", "1")
    
  883.         graph = MigrationGraph()
    
  884.         graph.add_node(a1, a1_impl)
    
  885.         graph.add_node(a2, a2_impl)
    
  886.         graph.add_node(b1, b1_impl)
    
  887.         graph.add_dependency(None, b1, a1)
    
  888.         graph.add_dependency(None, a2, a1)
    
  889. 
    
  890.         executor = MigrationExecutor(None)
    
  891.         executor.loader = FakeLoader(
    
  892.             graph,
    
  893.             {
    
  894.                 a1: a1_impl,
    
  895.                 b1: b1_impl,
    
  896.                 a2: a2_impl,
    
  897.             },
    
  898.         )
    
  899. 
    
  900.         plan = executor.migration_plan({a1})
    
  901. 
    
  902.         self.assertEqual(plan, [(a2_impl, True)])
    
  903. 
    
  904.     def test_minimize_rollbacks_branchy(self):
    
  905.         r"""
    
  906.         Minimize rollbacks when target has multiple in-app children.
    
  907. 
    
  908.         a: 1 <---- 3 <--\
    
  909.               \ \- 2 <--- 4
    
  910.                \       \
    
  911.         b:      \- 1 <--- 2
    
  912.         """
    
  913.         a1_impl = FakeMigration("a1")
    
  914.         a1 = ("a", "1")
    
  915.         a2_impl = FakeMigration("a2")
    
  916.         a2 = ("a", "2")
    
  917.         a3_impl = FakeMigration("a3")
    
  918.         a3 = ("a", "3")
    
  919.         a4_impl = FakeMigration("a4")
    
  920.         a4 = ("a", "4")
    
  921.         b1_impl = FakeMigration("b1")
    
  922.         b1 = ("b", "1")
    
  923.         b2_impl = FakeMigration("b2")
    
  924.         b2 = ("b", "2")
    
  925.         graph = MigrationGraph()
    
  926.         graph.add_node(a1, a1_impl)
    
  927.         graph.add_node(a2, a2_impl)
    
  928.         graph.add_node(a3, a3_impl)
    
  929.         graph.add_node(a4, a4_impl)
    
  930.         graph.add_node(b1, b1_impl)
    
  931.         graph.add_node(b2, b2_impl)
    
  932.         graph.add_dependency(None, a2, a1)
    
  933.         graph.add_dependency(None, a3, a1)
    
  934.         graph.add_dependency(None, a4, a2)
    
  935.         graph.add_dependency(None, a4, a3)
    
  936.         graph.add_dependency(None, b2, b1)
    
  937.         graph.add_dependency(None, b1, a1)
    
  938.         graph.add_dependency(None, b2, a2)
    
  939. 
    
  940.         executor = MigrationExecutor(None)
    
  941.         executor.loader = FakeLoader(
    
  942.             graph,
    
  943.             {
    
  944.                 a1: a1_impl,
    
  945.                 b1: b1_impl,
    
  946.                 a2: a2_impl,
    
  947.                 b2: b2_impl,
    
  948.                 a3: a3_impl,
    
  949.                 a4: a4_impl,
    
  950.             },
    
  951.         )
    
  952. 
    
  953.         plan = executor.migration_plan({a1})
    
  954. 
    
  955.         should_be_rolled_back = [b2_impl, a4_impl, a2_impl, a3_impl]
    
  956.         exp = [(m, True) for m in should_be_rolled_back]
    
  957.         self.assertEqual(plan, exp)
    
  958. 
    
  959.     def test_backwards_nothing_to_do(self):
    
  960.         r"""
    
  961.         If the current state satisfies the given target, do nothing.
    
  962. 
    
  963.         a: 1 <--- 2
    
  964.         b:    \- 1
    
  965.         c:     \- 1
    
  966. 
    
  967.         If a1 is applied already and a2 is not, and we're asked to migrate to
    
  968.         a1, don't apply or unapply b1 or c1, regardless of their current state.
    
  969.         """
    
  970.         a1_impl = FakeMigration("a1")
    
  971.         a1 = ("a", "1")
    
  972.         a2_impl = FakeMigration("a2")
    
  973.         a2 = ("a", "2")
    
  974.         b1_impl = FakeMigration("b1")
    
  975.         b1 = ("b", "1")
    
  976.         c1_impl = FakeMigration("c1")
    
  977.         c1 = ("c", "1")
    
  978.         graph = MigrationGraph()
    
  979.         graph.add_node(a1, a1_impl)
    
  980.         graph.add_node(a2, a2_impl)
    
  981.         graph.add_node(b1, b1_impl)
    
  982.         graph.add_node(c1, c1_impl)
    
  983.         graph.add_dependency(None, a2, a1)
    
  984.         graph.add_dependency(None, b1, a1)
    
  985.         graph.add_dependency(None, c1, a1)
    
  986. 
    
  987.         executor = MigrationExecutor(None)
    
  988.         executor.loader = FakeLoader(
    
  989.             graph,
    
  990.             {
    
  991.                 a1: a1_impl,
    
  992.                 b1: b1_impl,
    
  993.             },
    
  994.         )
    
  995. 
    
  996.         plan = executor.migration_plan({a1})
    
  997. 
    
  998.         self.assertEqual(plan, [])