1. from django.core.exceptions import FieldDoesNotExist
    
  2. from django.db import IntegrityError, connection, migrations, models, transaction
    
  3. from django.db.migrations.migration import Migration
    
  4. from django.db.migrations.operations.fields import FieldOperation
    
  5. from django.db.migrations.state import ModelState, ProjectState
    
  6. from django.db.models.functions import Abs
    
  7. from django.db.transaction import atomic
    
  8. from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
    
  9. from django.test.utils import CaptureQueriesContext
    
  10. 
    
  11. from .models import FoodManager, FoodQuerySet, UnicodeModel
    
  12. from .test_base import OperationTestBase
    
  13. 
    
  14. 
    
  15. class Mixin:
    
  16.     pass
    
  17. 
    
  18. 
    
  19. class OperationTests(OperationTestBase):
    
  20.     """
    
  21.     Tests running the operations and making sure they do what they say they do.
    
  22.     Each test looks at their state changing, and then their database operation -
    
  23.     both forwards and backwards.
    
  24.     """
    
  25. 
    
  26.     def test_create_model(self):
    
  27.         """
    
  28.         Tests the CreateModel operation.
    
  29.         Most other tests use this operation as part of setup, so check failures
    
  30.         here first.
    
  31.         """
    
  32.         operation = migrations.CreateModel(
    
  33.             "Pony",
    
  34.             [
    
  35.                 ("id", models.AutoField(primary_key=True)),
    
  36.                 ("pink", models.IntegerField(default=1)),
    
  37.             ],
    
  38.         )
    
  39.         self.assertEqual(operation.describe(), "Create model Pony")
    
  40.         self.assertEqual(operation.migration_name_fragment, "pony")
    
  41.         # Test the state alteration
    
  42.         project_state = ProjectState()
    
  43.         new_state = project_state.clone()
    
  44.         operation.state_forwards("test_crmo", new_state)
    
  45.         self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
    
  46.         self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
    
  47.         # Test the database alteration
    
  48.         self.assertTableNotExists("test_crmo_pony")
    
  49.         with connection.schema_editor() as editor:
    
  50.             operation.database_forwards("test_crmo", editor, project_state, new_state)
    
  51.         self.assertTableExists("test_crmo_pony")
    
  52.         # And test reversal
    
  53.         with connection.schema_editor() as editor:
    
  54.             operation.database_backwards("test_crmo", editor, new_state, project_state)
    
  55.         self.assertTableNotExists("test_crmo_pony")
    
  56.         # And deconstruction
    
  57.         definition = operation.deconstruct()
    
  58.         self.assertEqual(definition[0], "CreateModel")
    
  59.         self.assertEqual(definition[1], [])
    
  60.         self.assertEqual(sorted(definition[2]), ["fields", "name"])
    
  61.         # And default manager not in set
    
  62.         operation = migrations.CreateModel(
    
  63.             "Foo", fields=[], managers=[("objects", models.Manager())]
    
  64.         )
    
  65.         definition = operation.deconstruct()
    
  66.         self.assertNotIn("managers", definition[2])
    
  67. 
    
  68.     def test_create_model_with_duplicate_field_name(self):
    
  69.         with self.assertRaisesMessage(
    
  70.             ValueError, "Found duplicate value pink in CreateModel fields argument."
    
  71.         ):
    
  72.             migrations.CreateModel(
    
  73.                 "Pony",
    
  74.                 [
    
  75.                     ("id", models.AutoField(primary_key=True)),
    
  76.                     ("pink", models.TextField()),
    
  77.                     ("pink", models.IntegerField(default=1)),
    
  78.                 ],
    
  79.             )
    
  80. 
    
  81.     def test_create_model_with_duplicate_base(self):
    
  82.         message = "Found duplicate value test_crmo.pony in CreateModel bases argument."
    
  83.         with self.assertRaisesMessage(ValueError, message):
    
  84.             migrations.CreateModel(
    
  85.                 "Pony",
    
  86.                 fields=[],
    
  87.                 bases=(
    
  88.                     "test_crmo.Pony",
    
  89.                     "test_crmo.Pony",
    
  90.                 ),
    
  91.             )
    
  92.         with self.assertRaisesMessage(ValueError, message):
    
  93.             migrations.CreateModel(
    
  94.                 "Pony",
    
  95.                 fields=[],
    
  96.                 bases=(
    
  97.                     "test_crmo.Pony",
    
  98.                     "test_crmo.pony",
    
  99.                 ),
    
  100.             )
    
  101.         message = (
    
  102.             "Found duplicate value migrations.unicodemodel in CreateModel bases "
    
  103.             "argument."
    
  104.         )
    
  105.         with self.assertRaisesMessage(ValueError, message):
    
  106.             migrations.CreateModel(
    
  107.                 "Pony",
    
  108.                 fields=[],
    
  109.                 bases=(
    
  110.                     UnicodeModel,
    
  111.                     UnicodeModel,
    
  112.                 ),
    
  113.             )
    
  114.         with self.assertRaisesMessage(ValueError, message):
    
  115.             migrations.CreateModel(
    
  116.                 "Pony",
    
  117.                 fields=[],
    
  118.                 bases=(
    
  119.                     UnicodeModel,
    
  120.                     "migrations.unicodemodel",
    
  121.                 ),
    
  122.             )
    
  123.         with self.assertRaisesMessage(ValueError, message):
    
  124.             migrations.CreateModel(
    
  125.                 "Pony",
    
  126.                 fields=[],
    
  127.                 bases=(
    
  128.                     UnicodeModel,
    
  129.                     "migrations.UnicodeModel",
    
  130.                 ),
    
  131.             )
    
  132.         message = (
    
  133.             "Found duplicate value <class 'django.db.models.base.Model'> in "
    
  134.             "CreateModel bases argument."
    
  135.         )
    
  136.         with self.assertRaisesMessage(ValueError, message):
    
  137.             migrations.CreateModel(
    
  138.                 "Pony",
    
  139.                 fields=[],
    
  140.                 bases=(
    
  141.                     models.Model,
    
  142.                     models.Model,
    
  143.                 ),
    
  144.             )
    
  145.         message = (
    
  146.             "Found duplicate value <class 'migrations.test_operations.Mixin'> in "
    
  147.             "CreateModel bases argument."
    
  148.         )
    
  149.         with self.assertRaisesMessage(ValueError, message):
    
  150.             migrations.CreateModel(
    
  151.                 "Pony",
    
  152.                 fields=[],
    
  153.                 bases=(
    
  154.                     Mixin,
    
  155.                     Mixin,
    
  156.                 ),
    
  157.             )
    
  158. 
    
  159.     def test_create_model_with_duplicate_manager_name(self):
    
  160.         with self.assertRaisesMessage(
    
  161.             ValueError,
    
  162.             "Found duplicate value objects in CreateModel managers argument.",
    
  163.         ):
    
  164.             migrations.CreateModel(
    
  165.                 "Pony",
    
  166.                 fields=[],
    
  167.                 managers=[
    
  168.                     ("objects", models.Manager()),
    
  169.                     ("objects", models.Manager()),
    
  170.                 ],
    
  171.             )
    
  172. 
    
  173.     def test_create_model_with_unique_after(self):
    
  174.         """
    
  175.         Tests the CreateModel operation directly followed by an
    
  176.         AlterUniqueTogether (bug #22844 - sqlite remake issues)
    
  177.         """
    
  178.         operation1 = migrations.CreateModel(
    
  179.             "Pony",
    
  180.             [
    
  181.                 ("id", models.AutoField(primary_key=True)),
    
  182.                 ("pink", models.IntegerField(default=1)),
    
  183.             ],
    
  184.         )
    
  185.         operation2 = migrations.CreateModel(
    
  186.             "Rider",
    
  187.             [
    
  188.                 ("id", models.AutoField(primary_key=True)),
    
  189.                 ("number", models.IntegerField(default=1)),
    
  190.                 ("pony", models.ForeignKey("test_crmoua.Pony", models.CASCADE)),
    
  191.             ],
    
  192.         )
    
  193.         operation3 = migrations.AlterUniqueTogether(
    
  194.             "Rider",
    
  195.             [
    
  196.                 ("number", "pony"),
    
  197.             ],
    
  198.         )
    
  199.         # Test the database alteration
    
  200.         project_state = ProjectState()
    
  201.         self.assertTableNotExists("test_crmoua_pony")
    
  202.         self.assertTableNotExists("test_crmoua_rider")
    
  203.         with connection.schema_editor() as editor:
    
  204.             new_state = project_state.clone()
    
  205.             operation1.state_forwards("test_crmoua", new_state)
    
  206.             operation1.database_forwards(
    
  207.                 "test_crmoua", editor, project_state, new_state
    
  208.             )
    
  209.             project_state, new_state = new_state, new_state.clone()
    
  210.             operation2.state_forwards("test_crmoua", new_state)
    
  211.             operation2.database_forwards(
    
  212.                 "test_crmoua", editor, project_state, new_state
    
  213.             )
    
  214.             project_state, new_state = new_state, new_state.clone()
    
  215.             operation3.state_forwards("test_crmoua", new_state)
    
  216.             operation3.database_forwards(
    
  217.                 "test_crmoua", editor, project_state, new_state
    
  218.             )
    
  219.         self.assertTableExists("test_crmoua_pony")
    
  220.         self.assertTableExists("test_crmoua_rider")
    
  221. 
    
  222.     def test_create_model_m2m(self):
    
  223.         """
    
  224.         Test the creation of a model with a ManyToMany field and the
    
  225.         auto-created "through" model.
    
  226.         """
    
  227.         project_state = self.set_up_test_model("test_crmomm")
    
  228.         operation = migrations.CreateModel(
    
  229.             "Stable",
    
  230.             [
    
  231.                 ("id", models.AutoField(primary_key=True)),
    
  232.                 ("ponies", models.ManyToManyField("Pony", related_name="stables")),
    
  233.             ],
    
  234.         )
    
  235.         # Test the state alteration
    
  236.         new_state = project_state.clone()
    
  237.         operation.state_forwards("test_crmomm", new_state)
    
  238.         # Test the database alteration
    
  239.         self.assertTableNotExists("test_crmomm_stable_ponies")
    
  240.         with connection.schema_editor() as editor:
    
  241.             operation.database_forwards("test_crmomm", editor, project_state, new_state)
    
  242.         self.assertTableExists("test_crmomm_stable")
    
  243.         self.assertTableExists("test_crmomm_stable_ponies")
    
  244.         self.assertColumnNotExists("test_crmomm_stable", "ponies")
    
  245.         # Make sure the M2M field actually works
    
  246.         with atomic():
    
  247.             Pony = new_state.apps.get_model("test_crmomm", "Pony")
    
  248.             Stable = new_state.apps.get_model("test_crmomm", "Stable")
    
  249.             stable = Stable.objects.create()
    
  250.             p1 = Pony.objects.create(pink=False, weight=4.55)
    
  251.             p2 = Pony.objects.create(pink=True, weight=5.43)
    
  252.             stable.ponies.add(p1, p2)
    
  253.             self.assertEqual(stable.ponies.count(), 2)
    
  254.             stable.ponies.all().delete()
    
  255.         # And test reversal
    
  256.         with connection.schema_editor() as editor:
    
  257.             operation.database_backwards(
    
  258.                 "test_crmomm", editor, new_state, project_state
    
  259.             )
    
  260.         self.assertTableNotExists("test_crmomm_stable")
    
  261.         self.assertTableNotExists("test_crmomm_stable_ponies")
    
  262. 
    
  263.     @skipUnlessDBFeature("supports_collation_on_charfield", "supports_foreign_keys")
    
  264.     def test_create_fk_models_to_pk_field_db_collation(self):
    
  265.         """Creation of models with a FK to a PK with db_collation."""
    
  266.         collation = connection.features.test_collations.get("non_default")
    
  267.         if not collation:
    
  268.             self.skipTest("Language collations are not supported.")
    
  269. 
    
  270.         app_label = "test_cfkmtopkfdbc"
    
  271.         operations = [
    
  272.             migrations.CreateModel(
    
  273.                 "Pony",
    
  274.                 [
    
  275.                     (
    
  276.                         "id",
    
  277.                         models.CharField(
    
  278.                             primary_key=True,
    
  279.                             max_length=10,
    
  280.                             db_collation=collation,
    
  281.                         ),
    
  282.                     ),
    
  283.                 ],
    
  284.             )
    
  285.         ]
    
  286.         project_state = self.apply_operations(app_label, ProjectState(), operations)
    
  287.         # ForeignKey.
    
  288.         new_state = project_state.clone()
    
  289.         operation = migrations.CreateModel(
    
  290.             "Rider",
    
  291.             [
    
  292.                 ("id", models.AutoField(primary_key=True)),
    
  293.                 ("pony", models.ForeignKey("Pony", models.CASCADE)),
    
  294.             ],
    
  295.         )
    
  296.         operation.state_forwards(app_label, new_state)
    
  297.         with connection.schema_editor() as editor:
    
  298.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  299.         self.assertColumnCollation(f"{app_label}_rider", "pony_id", collation)
    
  300.         # Reversal.
    
  301.         with connection.schema_editor() as editor:
    
  302.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  303.         # OneToOneField.
    
  304.         new_state = project_state.clone()
    
  305.         operation = migrations.CreateModel(
    
  306.             "ShetlandPony",
    
  307.             [
    
  308.                 (
    
  309.                     "pony",
    
  310.                     models.OneToOneField("Pony", models.CASCADE, primary_key=True),
    
  311.                 ),
    
  312.                 ("cuteness", models.IntegerField(default=1)),
    
  313.             ],
    
  314.         )
    
  315.         operation.state_forwards(app_label, new_state)
    
  316.         with connection.schema_editor() as editor:
    
  317.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  318.         self.assertColumnCollation(f"{app_label}_shetlandpony", "pony_id", collation)
    
  319.         # Reversal.
    
  320.         with connection.schema_editor() as editor:
    
  321.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  322. 
    
  323.     def test_create_model_inheritance(self):
    
  324.         """
    
  325.         Tests the CreateModel operation on a multi-table inheritance setup.
    
  326.         """
    
  327.         project_state = self.set_up_test_model("test_crmoih")
    
  328.         # Test the state alteration
    
  329.         operation = migrations.CreateModel(
    
  330.             "ShetlandPony",
    
  331.             [
    
  332.                 (
    
  333.                     "pony_ptr",
    
  334.                     models.OneToOneField(
    
  335.                         "test_crmoih.Pony",
    
  336.                         models.CASCADE,
    
  337.                         auto_created=True,
    
  338.                         primary_key=True,
    
  339.                         to_field="id",
    
  340.                         serialize=False,
    
  341.                     ),
    
  342.                 ),
    
  343.                 ("cuteness", models.IntegerField(default=1)),
    
  344.             ],
    
  345.         )
    
  346.         new_state = project_state.clone()
    
  347.         operation.state_forwards("test_crmoih", new_state)
    
  348.         self.assertIn(("test_crmoih", "shetlandpony"), new_state.models)
    
  349.         # Test the database alteration
    
  350.         self.assertTableNotExists("test_crmoih_shetlandpony")
    
  351.         with connection.schema_editor() as editor:
    
  352.             operation.database_forwards("test_crmoih", editor, project_state, new_state)
    
  353.         self.assertTableExists("test_crmoih_shetlandpony")
    
  354.         # And test reversal
    
  355.         with connection.schema_editor() as editor:
    
  356.             operation.database_backwards(
    
  357.                 "test_crmoih", editor, new_state, project_state
    
  358.             )
    
  359.         self.assertTableNotExists("test_crmoih_shetlandpony")
    
  360. 
    
  361.     def test_create_proxy_model(self):
    
  362.         """
    
  363.         CreateModel ignores proxy models.
    
  364.         """
    
  365.         project_state = self.set_up_test_model("test_crprmo")
    
  366.         # Test the state alteration
    
  367.         operation = migrations.CreateModel(
    
  368.             "ProxyPony",
    
  369.             [],
    
  370.             options={"proxy": True},
    
  371.             bases=("test_crprmo.Pony",),
    
  372.         )
    
  373.         self.assertEqual(operation.describe(), "Create proxy model ProxyPony")
    
  374.         new_state = project_state.clone()
    
  375.         operation.state_forwards("test_crprmo", new_state)
    
  376.         self.assertIn(("test_crprmo", "proxypony"), new_state.models)
    
  377.         # Test the database alteration
    
  378.         self.assertTableNotExists("test_crprmo_proxypony")
    
  379.         self.assertTableExists("test_crprmo_pony")
    
  380.         with connection.schema_editor() as editor:
    
  381.             operation.database_forwards("test_crprmo", editor, project_state, new_state)
    
  382.         self.assertTableNotExists("test_crprmo_proxypony")
    
  383.         self.assertTableExists("test_crprmo_pony")
    
  384.         # And test reversal
    
  385.         with connection.schema_editor() as editor:
    
  386.             operation.database_backwards(
    
  387.                 "test_crprmo", editor, new_state, project_state
    
  388.             )
    
  389.         self.assertTableNotExists("test_crprmo_proxypony")
    
  390.         self.assertTableExists("test_crprmo_pony")
    
  391.         # And deconstruction
    
  392.         definition = operation.deconstruct()
    
  393.         self.assertEqual(definition[0], "CreateModel")
    
  394.         self.assertEqual(definition[1], [])
    
  395.         self.assertEqual(sorted(definition[2]), ["bases", "fields", "name", "options"])
    
  396. 
    
  397.     def test_create_unmanaged_model(self):
    
  398.         """
    
  399.         CreateModel ignores unmanaged models.
    
  400.         """
    
  401.         project_state = self.set_up_test_model("test_crummo")
    
  402.         # Test the state alteration
    
  403.         operation = migrations.CreateModel(
    
  404.             "UnmanagedPony",
    
  405.             [],
    
  406.             options={"proxy": True},
    
  407.             bases=("test_crummo.Pony",),
    
  408.         )
    
  409.         self.assertEqual(operation.describe(), "Create proxy model UnmanagedPony")
    
  410.         new_state = project_state.clone()
    
  411.         operation.state_forwards("test_crummo", new_state)
    
  412.         self.assertIn(("test_crummo", "unmanagedpony"), new_state.models)
    
  413.         # Test the database alteration
    
  414.         self.assertTableNotExists("test_crummo_unmanagedpony")
    
  415.         self.assertTableExists("test_crummo_pony")
    
  416.         with connection.schema_editor() as editor:
    
  417.             operation.database_forwards("test_crummo", editor, project_state, new_state)
    
  418.         self.assertTableNotExists("test_crummo_unmanagedpony")
    
  419.         self.assertTableExists("test_crummo_pony")
    
  420.         # And test reversal
    
  421.         with connection.schema_editor() as editor:
    
  422.             operation.database_backwards(
    
  423.                 "test_crummo", editor, new_state, project_state
    
  424.             )
    
  425.         self.assertTableNotExists("test_crummo_unmanagedpony")
    
  426.         self.assertTableExists("test_crummo_pony")
    
  427. 
    
  428.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  429.     def test_create_model_with_constraint(self):
    
  430.         where = models.Q(pink__gt=2)
    
  431.         check_constraint = models.CheckConstraint(
    
  432.             check=where, name="test_constraint_pony_pink_gt_2"
    
  433.         )
    
  434.         operation = migrations.CreateModel(
    
  435.             "Pony",
    
  436.             [
    
  437.                 ("id", models.AutoField(primary_key=True)),
    
  438.                 ("pink", models.IntegerField(default=3)),
    
  439.             ],
    
  440.             options={"constraints": [check_constraint]},
    
  441.         )
    
  442. 
    
  443.         # Test the state alteration
    
  444.         project_state = ProjectState()
    
  445.         new_state = project_state.clone()
    
  446.         operation.state_forwards("test_crmo", new_state)
    
  447.         self.assertEqual(
    
  448.             len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
    
  449.         )
    
  450. 
    
  451.         # Test database alteration
    
  452.         self.assertTableNotExists("test_crmo_pony")
    
  453.         with connection.schema_editor() as editor:
    
  454.             operation.database_forwards("test_crmo", editor, project_state, new_state)
    
  455.         self.assertTableExists("test_crmo_pony")
    
  456.         with connection.cursor() as cursor:
    
  457.             with self.assertRaises(IntegrityError):
    
  458.                 cursor.execute("INSERT INTO test_crmo_pony (id, pink) VALUES (1, 1)")
    
  459. 
    
  460.         # Test reversal
    
  461.         with connection.schema_editor() as editor:
    
  462.             operation.database_backwards("test_crmo", editor, new_state, project_state)
    
  463.         self.assertTableNotExists("test_crmo_pony")
    
  464. 
    
  465.         # Test deconstruction
    
  466.         definition = operation.deconstruct()
    
  467.         self.assertEqual(definition[0], "CreateModel")
    
  468.         self.assertEqual(definition[1], [])
    
  469.         self.assertEqual(definition[2]["options"]["constraints"], [check_constraint])
    
  470. 
    
  471.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  472.     def test_create_model_with_boolean_expression_in_check_constraint(self):
    
  473.         app_label = "test_crmobechc"
    
  474.         rawsql_constraint = models.CheckConstraint(
    
  475.             check=models.expressions.RawSQL(
    
  476.                 "price < %s", (1000,), output_field=models.BooleanField()
    
  477.             ),
    
  478.             name=f"{app_label}_price_lt_1000_raw",
    
  479.         )
    
  480.         wrapper_constraint = models.CheckConstraint(
    
  481.             check=models.expressions.ExpressionWrapper(
    
  482.                 models.Q(price__gt=500) | models.Q(price__lt=500),
    
  483.                 output_field=models.BooleanField(),
    
  484.             ),
    
  485.             name=f"{app_label}_price_neq_500_wrap",
    
  486.         )
    
  487.         operation = migrations.CreateModel(
    
  488.             "Product",
    
  489.             [
    
  490.                 ("id", models.AutoField(primary_key=True)),
    
  491.                 ("price", models.IntegerField(null=True)),
    
  492.             ],
    
  493.             options={"constraints": [rawsql_constraint, wrapper_constraint]},
    
  494.         )
    
  495. 
    
  496.         project_state = ProjectState()
    
  497.         new_state = project_state.clone()
    
  498.         operation.state_forwards(app_label, new_state)
    
  499.         # Add table.
    
  500.         self.assertTableNotExists(app_label)
    
  501.         with connection.schema_editor() as editor:
    
  502.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  503.         self.assertTableExists(f"{app_label}_product")
    
  504.         insert_sql = f"INSERT INTO {app_label}_product (id, price) VALUES (%d, %d)"
    
  505.         with connection.cursor() as cursor:
    
  506.             with self.assertRaises(IntegrityError):
    
  507.                 cursor.execute(insert_sql % (1, 1000))
    
  508.             cursor.execute(insert_sql % (1, 999))
    
  509.             with self.assertRaises(IntegrityError):
    
  510.                 cursor.execute(insert_sql % (2, 500))
    
  511.             cursor.execute(insert_sql % (2, 499))
    
  512. 
    
  513.     def test_create_model_with_partial_unique_constraint(self):
    
  514.         partial_unique_constraint = models.UniqueConstraint(
    
  515.             fields=["pink"],
    
  516.             condition=models.Q(weight__gt=5),
    
  517.             name="test_constraint_pony_pink_for_weight_gt_5_uniq",
    
  518.         )
    
  519.         operation = migrations.CreateModel(
    
  520.             "Pony",
    
  521.             [
    
  522.                 ("id", models.AutoField(primary_key=True)),
    
  523.                 ("pink", models.IntegerField(default=3)),
    
  524.                 ("weight", models.FloatField()),
    
  525.             ],
    
  526.             options={"constraints": [partial_unique_constraint]},
    
  527.         )
    
  528.         # Test the state alteration
    
  529.         project_state = ProjectState()
    
  530.         new_state = project_state.clone()
    
  531.         operation.state_forwards("test_crmo", new_state)
    
  532.         self.assertEqual(
    
  533.             len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
    
  534.         )
    
  535.         # Test database alteration
    
  536.         self.assertTableNotExists("test_crmo_pony")
    
  537.         with connection.schema_editor() as editor:
    
  538.             operation.database_forwards("test_crmo", editor, project_state, new_state)
    
  539.         self.assertTableExists("test_crmo_pony")
    
  540.         # Test constraint works
    
  541.         Pony = new_state.apps.get_model("test_crmo", "Pony")
    
  542.         Pony.objects.create(pink=1, weight=4.0)
    
  543.         Pony.objects.create(pink=1, weight=4.0)
    
  544.         Pony.objects.create(pink=1, weight=6.0)
    
  545.         if connection.features.supports_partial_indexes:
    
  546.             with self.assertRaises(IntegrityError):
    
  547.                 Pony.objects.create(pink=1, weight=7.0)
    
  548.         else:
    
  549.             Pony.objects.create(pink=1, weight=7.0)
    
  550.         # Test reversal
    
  551.         with connection.schema_editor() as editor:
    
  552.             operation.database_backwards("test_crmo", editor, new_state, project_state)
    
  553.         self.assertTableNotExists("test_crmo_pony")
    
  554.         # Test deconstruction
    
  555.         definition = operation.deconstruct()
    
  556.         self.assertEqual(definition[0], "CreateModel")
    
  557.         self.assertEqual(definition[1], [])
    
  558.         self.assertEqual(
    
  559.             definition[2]["options"]["constraints"], [partial_unique_constraint]
    
  560.         )
    
  561. 
    
  562.     def test_create_model_with_deferred_unique_constraint(self):
    
  563.         deferred_unique_constraint = models.UniqueConstraint(
    
  564.             fields=["pink"],
    
  565.             name="deferrable_pink_constraint",
    
  566.             deferrable=models.Deferrable.DEFERRED,
    
  567.         )
    
  568.         operation = migrations.CreateModel(
    
  569.             "Pony",
    
  570.             [
    
  571.                 ("id", models.AutoField(primary_key=True)),
    
  572.                 ("pink", models.IntegerField(default=3)),
    
  573.             ],
    
  574.             options={"constraints": [deferred_unique_constraint]},
    
  575.         )
    
  576.         project_state = ProjectState()
    
  577.         new_state = project_state.clone()
    
  578.         operation.state_forwards("test_crmo", new_state)
    
  579.         self.assertEqual(
    
  580.             len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
    
  581.         )
    
  582.         self.assertTableNotExists("test_crmo_pony")
    
  583.         # Create table.
    
  584.         with connection.schema_editor() as editor:
    
  585.             operation.database_forwards("test_crmo", editor, project_state, new_state)
    
  586.         self.assertTableExists("test_crmo_pony")
    
  587.         Pony = new_state.apps.get_model("test_crmo", "Pony")
    
  588.         Pony.objects.create(pink=1)
    
  589.         if connection.features.supports_deferrable_unique_constraints:
    
  590.             # Unique constraint is deferred.
    
  591.             with transaction.atomic():
    
  592.                 obj = Pony.objects.create(pink=1)
    
  593.                 obj.pink = 2
    
  594.                 obj.save()
    
  595.             # Constraint behavior can be changed with SET CONSTRAINTS.
    
  596.             with self.assertRaises(IntegrityError):
    
  597.                 with transaction.atomic(), connection.cursor() as cursor:
    
  598.                     quoted_name = connection.ops.quote_name(
    
  599.                         deferred_unique_constraint.name
    
  600.                     )
    
  601.                     cursor.execute("SET CONSTRAINTS %s IMMEDIATE" % quoted_name)
    
  602.                     obj = Pony.objects.create(pink=1)
    
  603.                     obj.pink = 3
    
  604.                     obj.save()
    
  605.         else:
    
  606.             Pony.objects.create(pink=1)
    
  607.         # Reversal.
    
  608.         with connection.schema_editor() as editor:
    
  609.             operation.database_backwards("test_crmo", editor, new_state, project_state)
    
  610.         self.assertTableNotExists("test_crmo_pony")
    
  611.         # Deconstruction.
    
  612.         definition = operation.deconstruct()
    
  613.         self.assertEqual(definition[0], "CreateModel")
    
  614.         self.assertEqual(definition[1], [])
    
  615.         self.assertEqual(
    
  616.             definition[2]["options"]["constraints"],
    
  617.             [deferred_unique_constraint],
    
  618.         )
    
  619. 
    
  620.     @skipUnlessDBFeature("supports_covering_indexes")
    
  621.     def test_create_model_with_covering_unique_constraint(self):
    
  622.         covering_unique_constraint = models.UniqueConstraint(
    
  623.             fields=["pink"],
    
  624.             include=["weight"],
    
  625.             name="test_constraint_pony_pink_covering_weight",
    
  626.         )
    
  627.         operation = migrations.CreateModel(
    
  628.             "Pony",
    
  629.             [
    
  630.                 ("id", models.AutoField(primary_key=True)),
    
  631.                 ("pink", models.IntegerField(default=3)),
    
  632.                 ("weight", models.FloatField()),
    
  633.             ],
    
  634.             options={"constraints": [covering_unique_constraint]},
    
  635.         )
    
  636.         project_state = ProjectState()
    
  637.         new_state = project_state.clone()
    
  638.         operation.state_forwards("test_crmo", new_state)
    
  639.         self.assertEqual(
    
  640.             len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
    
  641.         )
    
  642.         self.assertTableNotExists("test_crmo_pony")
    
  643.         # Create table.
    
  644.         with connection.schema_editor() as editor:
    
  645.             operation.database_forwards("test_crmo", editor, project_state, new_state)
    
  646.         self.assertTableExists("test_crmo_pony")
    
  647.         Pony = new_state.apps.get_model("test_crmo", "Pony")
    
  648.         Pony.objects.create(pink=1, weight=4.0)
    
  649.         with self.assertRaises(IntegrityError):
    
  650.             Pony.objects.create(pink=1, weight=7.0)
    
  651.         # Reversal.
    
  652.         with connection.schema_editor() as editor:
    
  653.             operation.database_backwards("test_crmo", editor, new_state, project_state)
    
  654.         self.assertTableNotExists("test_crmo_pony")
    
  655.         # Deconstruction.
    
  656.         definition = operation.deconstruct()
    
  657.         self.assertEqual(definition[0], "CreateModel")
    
  658.         self.assertEqual(definition[1], [])
    
  659.         self.assertEqual(
    
  660.             definition[2]["options"]["constraints"],
    
  661.             [covering_unique_constraint],
    
  662.         )
    
  663. 
    
  664.     def test_create_model_managers(self):
    
  665.         """
    
  666.         The managers on a model are set.
    
  667.         """
    
  668.         project_state = self.set_up_test_model("test_cmoma")
    
  669.         # Test the state alteration
    
  670.         operation = migrations.CreateModel(
    
  671.             "Food",
    
  672.             fields=[
    
  673.                 ("id", models.AutoField(primary_key=True)),
    
  674.             ],
    
  675.             managers=[
    
  676.                 ("food_qs", FoodQuerySet.as_manager()),
    
  677.                 ("food_mgr", FoodManager("a", "b")),
    
  678.                 ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
    
  679.             ],
    
  680.         )
    
  681.         self.assertEqual(operation.describe(), "Create model Food")
    
  682.         new_state = project_state.clone()
    
  683.         operation.state_forwards("test_cmoma", new_state)
    
  684.         self.assertIn(("test_cmoma", "food"), new_state.models)
    
  685.         managers = new_state.models["test_cmoma", "food"].managers
    
  686.         self.assertEqual(managers[0][0], "food_qs")
    
  687.         self.assertIsInstance(managers[0][1], models.Manager)
    
  688.         self.assertEqual(managers[1][0], "food_mgr")
    
  689.         self.assertIsInstance(managers[1][1], FoodManager)
    
  690.         self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
    
  691.         self.assertEqual(managers[2][0], "food_mgr_kwargs")
    
  692.         self.assertIsInstance(managers[2][1], FoodManager)
    
  693.         self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
    
  694. 
    
  695.     def test_delete_model(self):
    
  696.         """
    
  697.         Tests the DeleteModel operation.
    
  698.         """
    
  699.         project_state = self.set_up_test_model("test_dlmo")
    
  700.         # Test the state alteration
    
  701.         operation = migrations.DeleteModel("Pony")
    
  702.         self.assertEqual(operation.describe(), "Delete model Pony")
    
  703.         self.assertEqual(operation.migration_name_fragment, "delete_pony")
    
  704.         new_state = project_state.clone()
    
  705.         operation.state_forwards("test_dlmo", new_state)
    
  706.         self.assertNotIn(("test_dlmo", "pony"), new_state.models)
    
  707.         # Test the database alteration
    
  708.         self.assertTableExists("test_dlmo_pony")
    
  709.         with connection.schema_editor() as editor:
    
  710.             operation.database_forwards("test_dlmo", editor, project_state, new_state)
    
  711.         self.assertTableNotExists("test_dlmo_pony")
    
  712.         # And test reversal
    
  713.         with connection.schema_editor() as editor:
    
  714.             operation.database_backwards("test_dlmo", editor, new_state, project_state)
    
  715.         self.assertTableExists("test_dlmo_pony")
    
  716.         # And deconstruction
    
  717.         definition = operation.deconstruct()
    
  718.         self.assertEqual(definition[0], "DeleteModel")
    
  719.         self.assertEqual(definition[1], [])
    
  720.         self.assertEqual(list(definition[2]), ["name"])
    
  721. 
    
  722.     def test_delete_proxy_model(self):
    
  723.         """
    
  724.         Tests the DeleteModel operation ignores proxy models.
    
  725.         """
    
  726.         project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
    
  727.         # Test the state alteration
    
  728.         operation = migrations.DeleteModel("ProxyPony")
    
  729.         new_state = project_state.clone()
    
  730.         operation.state_forwards("test_dlprmo", new_state)
    
  731.         self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
    
  732.         self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
    
  733.         # Test the database alteration
    
  734.         self.assertTableExists("test_dlprmo_pony")
    
  735.         self.assertTableNotExists("test_dlprmo_proxypony")
    
  736.         with connection.schema_editor() as editor:
    
  737.             operation.database_forwards("test_dlprmo", editor, project_state, new_state)
    
  738.         self.assertTableExists("test_dlprmo_pony")
    
  739.         self.assertTableNotExists("test_dlprmo_proxypony")
    
  740.         # And test reversal
    
  741.         with connection.schema_editor() as editor:
    
  742.             operation.database_backwards(
    
  743.                 "test_dlprmo", editor, new_state, project_state
    
  744.             )
    
  745.         self.assertTableExists("test_dlprmo_pony")
    
  746.         self.assertTableNotExists("test_dlprmo_proxypony")
    
  747. 
    
  748.     def test_delete_mti_model(self):
    
  749.         project_state = self.set_up_test_model("test_dlmtimo", mti_model=True)
    
  750.         # Test the state alteration
    
  751.         operation = migrations.DeleteModel("ShetlandPony")
    
  752.         new_state = project_state.clone()
    
  753.         operation.state_forwards("test_dlmtimo", new_state)
    
  754.         self.assertIn(("test_dlmtimo", "shetlandpony"), project_state.models)
    
  755.         self.assertNotIn(("test_dlmtimo", "shetlandpony"), new_state.models)
    
  756.         # Test the database alteration
    
  757.         self.assertTableExists("test_dlmtimo_pony")
    
  758.         self.assertTableExists("test_dlmtimo_shetlandpony")
    
  759.         self.assertColumnExists("test_dlmtimo_shetlandpony", "pony_ptr_id")
    
  760.         with connection.schema_editor() as editor:
    
  761.             operation.database_forwards(
    
  762.                 "test_dlmtimo", editor, project_state, new_state
    
  763.             )
    
  764.         self.assertTableExists("test_dlmtimo_pony")
    
  765.         self.assertTableNotExists("test_dlmtimo_shetlandpony")
    
  766.         # And test reversal
    
  767.         with connection.schema_editor() as editor:
    
  768.             operation.database_backwards(
    
  769.                 "test_dlmtimo", editor, new_state, project_state
    
  770.             )
    
  771.         self.assertTableExists("test_dlmtimo_pony")
    
  772.         self.assertTableExists("test_dlmtimo_shetlandpony")
    
  773.         self.assertColumnExists("test_dlmtimo_shetlandpony", "pony_ptr_id")
    
  774. 
    
  775.     def test_rename_model(self):
    
  776.         """
    
  777.         Tests the RenameModel operation.
    
  778.         """
    
  779.         project_state = self.set_up_test_model("test_rnmo", related_model=True)
    
  780.         # Test the state alteration
    
  781.         operation = migrations.RenameModel("Pony", "Horse")
    
  782.         self.assertEqual(operation.describe(), "Rename model Pony to Horse")
    
  783.         self.assertEqual(operation.migration_name_fragment, "rename_pony_horse")
    
  784.         # Test initial state and database
    
  785.         self.assertIn(("test_rnmo", "pony"), project_state.models)
    
  786.         self.assertNotIn(("test_rnmo", "horse"), project_state.models)
    
  787.         self.assertTableExists("test_rnmo_pony")
    
  788.         self.assertTableNotExists("test_rnmo_horse")
    
  789.         if connection.features.supports_foreign_keys:
    
  790.             self.assertFKExists(
    
  791.                 "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
    
  792.             )
    
  793.             self.assertFKNotExists(
    
  794.                 "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
    
  795.             )
    
  796.         # Migrate forwards
    
  797.         new_state = project_state.clone()
    
  798.         atomic_rename = connection.features.supports_atomic_references_rename
    
  799.         new_state = self.apply_operations(
    
  800.             "test_rnmo", new_state, [operation], atomic=atomic_rename
    
  801.         )
    
  802.         # Test new state and database
    
  803.         self.assertNotIn(("test_rnmo", "pony"), new_state.models)
    
  804.         self.assertIn(("test_rnmo", "horse"), new_state.models)
    
  805.         # RenameModel also repoints all incoming FKs and M2Ms
    
  806.         self.assertEqual(
    
  807.             new_state.models["test_rnmo", "rider"].fields["pony"].remote_field.model,
    
  808.             "test_rnmo.Horse",
    
  809.         )
    
  810.         self.assertTableNotExists("test_rnmo_pony")
    
  811.         self.assertTableExists("test_rnmo_horse")
    
  812.         if connection.features.supports_foreign_keys:
    
  813.             self.assertFKNotExists(
    
  814.                 "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
    
  815.             )
    
  816.             self.assertFKExists(
    
  817.                 "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
    
  818.             )
    
  819.         # Migrate backwards
    
  820.         original_state = self.unapply_operations(
    
  821.             "test_rnmo", project_state, [operation], atomic=atomic_rename
    
  822.         )
    
  823.         # Test original state and database
    
  824.         self.assertIn(("test_rnmo", "pony"), original_state.models)
    
  825.         self.assertNotIn(("test_rnmo", "horse"), original_state.models)
    
  826.         self.assertEqual(
    
  827.             original_state.models["test_rnmo", "rider"]
    
  828.             .fields["pony"]
    
  829.             .remote_field.model,
    
  830.             "Pony",
    
  831.         )
    
  832.         self.assertTableExists("test_rnmo_pony")
    
  833.         self.assertTableNotExists("test_rnmo_horse")
    
  834.         if connection.features.supports_foreign_keys:
    
  835.             self.assertFKExists(
    
  836.                 "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
    
  837.             )
    
  838.             self.assertFKNotExists(
    
  839.                 "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
    
  840.             )
    
  841.         # And deconstruction
    
  842.         definition = operation.deconstruct()
    
  843.         self.assertEqual(definition[0], "RenameModel")
    
  844.         self.assertEqual(definition[1], [])
    
  845.         self.assertEqual(definition[2], {"old_name": "Pony", "new_name": "Horse"})
    
  846. 
    
  847.     def test_rename_model_state_forwards(self):
    
  848.         """
    
  849.         RenameModel operations shouldn't trigger the caching of rendered apps
    
  850.         on state without prior apps.
    
  851.         """
    
  852.         state = ProjectState()
    
  853.         state.add_model(ModelState("migrations", "Foo", []))
    
  854.         operation = migrations.RenameModel("Foo", "Bar")
    
  855.         operation.state_forwards("migrations", state)
    
  856.         self.assertNotIn("apps", state.__dict__)
    
  857.         self.assertNotIn(("migrations", "foo"), state.models)
    
  858.         self.assertIn(("migrations", "bar"), state.models)
    
  859.         # Now with apps cached.
    
  860.         apps = state.apps
    
  861.         operation = migrations.RenameModel("Bar", "Foo")
    
  862.         operation.state_forwards("migrations", state)
    
  863.         self.assertIs(state.apps, apps)
    
  864.         self.assertNotIn(("migrations", "bar"), state.models)
    
  865.         self.assertIn(("migrations", "foo"), state.models)
    
  866. 
    
  867.     def test_rename_model_with_self_referential_fk(self):
    
  868.         """
    
  869.         Tests the RenameModel operation on model with self referential FK.
    
  870.         """
    
  871.         project_state = self.set_up_test_model("test_rmwsrf", related_model=True)
    
  872.         # Test the state alteration
    
  873.         operation = migrations.RenameModel("Rider", "HorseRider")
    
  874.         self.assertEqual(operation.describe(), "Rename model Rider to HorseRider")
    
  875.         new_state = project_state.clone()
    
  876.         operation.state_forwards("test_rmwsrf", new_state)
    
  877.         self.assertNotIn(("test_rmwsrf", "rider"), new_state.models)
    
  878.         self.assertIn(("test_rmwsrf", "horserider"), new_state.models)
    
  879.         # Remember, RenameModel also repoints all incoming FKs and M2Ms
    
  880.         self.assertEqual(
    
  881.             "self",
    
  882.             new_state.models["test_rmwsrf", "horserider"]
    
  883.             .fields["friend"]
    
  884.             .remote_field.model,
    
  885.         )
    
  886.         HorseRider = new_state.apps.get_model("test_rmwsrf", "horserider")
    
  887.         self.assertIs(
    
  888.             HorseRider._meta.get_field("horserider").remote_field.model, HorseRider
    
  889.         )
    
  890.         # Test the database alteration
    
  891.         self.assertTableExists("test_rmwsrf_rider")
    
  892.         self.assertTableNotExists("test_rmwsrf_horserider")
    
  893.         if connection.features.supports_foreign_keys:
    
  894.             self.assertFKExists(
    
  895.                 "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id")
    
  896.             )
    
  897.             self.assertFKNotExists(
    
  898.                 "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id")
    
  899.             )
    
  900.         atomic_rename = connection.features.supports_atomic_references_rename
    
  901.         with connection.schema_editor(atomic=atomic_rename) as editor:
    
  902.             operation.database_forwards("test_rmwsrf", editor, project_state, new_state)
    
  903.         self.assertTableNotExists("test_rmwsrf_rider")
    
  904.         self.assertTableExists("test_rmwsrf_horserider")
    
  905.         if connection.features.supports_foreign_keys:
    
  906.             self.assertFKNotExists(
    
  907.                 "test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_rider", "id")
    
  908.             )
    
  909.             self.assertFKExists(
    
  910.                 "test_rmwsrf_horserider",
    
  911.                 ["friend_id"],
    
  912.                 ("test_rmwsrf_horserider", "id"),
    
  913.             )
    
  914.         # And test reversal
    
  915.         with connection.schema_editor(atomic=atomic_rename) as editor:
    
  916.             operation.database_backwards(
    
  917.                 "test_rmwsrf", editor, new_state, project_state
    
  918.             )
    
  919.         self.assertTableExists("test_rmwsrf_rider")
    
  920.         self.assertTableNotExists("test_rmwsrf_horserider")
    
  921.         if connection.features.supports_foreign_keys:
    
  922.             self.assertFKExists(
    
  923.                 "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id")
    
  924.             )
    
  925.             self.assertFKNotExists(
    
  926.                 "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id")
    
  927.             )
    
  928. 
    
  929.     def test_rename_model_with_superclass_fk(self):
    
  930.         """
    
  931.         Tests the RenameModel operation on a model which has a superclass that
    
  932.         has a foreign key.
    
  933.         """
    
  934.         project_state = self.set_up_test_model(
    
  935.             "test_rmwsc", related_model=True, mti_model=True
    
  936.         )
    
  937.         # Test the state alteration
    
  938.         operation = migrations.RenameModel("ShetlandPony", "LittleHorse")
    
  939.         self.assertEqual(
    
  940.             operation.describe(), "Rename model ShetlandPony to LittleHorse"
    
  941.         )
    
  942.         new_state = project_state.clone()
    
  943.         operation.state_forwards("test_rmwsc", new_state)
    
  944.         self.assertNotIn(("test_rmwsc", "shetlandpony"), new_state.models)
    
  945.         self.assertIn(("test_rmwsc", "littlehorse"), new_state.models)
    
  946.         # RenameModel shouldn't repoint the superclass's relations, only local ones
    
  947.         self.assertEqual(
    
  948.             project_state.models["test_rmwsc", "rider"]
    
  949.             .fields["pony"]
    
  950.             .remote_field.model,
    
  951.             new_state.models["test_rmwsc", "rider"].fields["pony"].remote_field.model,
    
  952.         )
    
  953.         # Before running the migration we have a table for Shetland Pony, not
    
  954.         # Little Horse.
    
  955.         self.assertTableExists("test_rmwsc_shetlandpony")
    
  956.         self.assertTableNotExists("test_rmwsc_littlehorse")
    
  957.         if connection.features.supports_foreign_keys:
    
  958.             # and the foreign key on rider points to pony, not shetland pony
    
  959.             self.assertFKExists(
    
  960.                 "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id")
    
  961.             )
    
  962.             self.assertFKNotExists(
    
  963.                 "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_shetlandpony", "id")
    
  964.             )
    
  965.         with connection.schema_editor(
    
  966.             atomic=connection.features.supports_atomic_references_rename
    
  967.         ) as editor:
    
  968.             operation.database_forwards("test_rmwsc", editor, project_state, new_state)
    
  969.         # Now we have a little horse table, not shetland pony
    
  970.         self.assertTableNotExists("test_rmwsc_shetlandpony")
    
  971.         self.assertTableExists("test_rmwsc_littlehorse")
    
  972.         if connection.features.supports_foreign_keys:
    
  973.             # but the Foreign keys still point at pony, not little horse
    
  974.             self.assertFKExists(
    
  975.                 "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id")
    
  976.             )
    
  977.             self.assertFKNotExists(
    
  978.                 "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_littlehorse", "id")
    
  979.             )
    
  980. 
    
  981.     def test_rename_model_with_self_referential_m2m(self):
    
  982.         app_label = "test_rename_model_with_self_referential_m2m"
    
  983. 
    
  984.         project_state = self.apply_operations(
    
  985.             app_label,
    
  986.             ProjectState(),
    
  987.             operations=[
    
  988.                 migrations.CreateModel(
    
  989.                     "ReflexivePony",
    
  990.                     fields=[
    
  991.                         ("id", models.AutoField(primary_key=True)),
    
  992.                         ("ponies", models.ManyToManyField("self")),
    
  993.                     ],
    
  994.                 ),
    
  995.             ],
    
  996.         )
    
  997.         project_state = self.apply_operations(
    
  998.             app_label,
    
  999.             project_state,
    
  1000.             operations=[
    
  1001.                 migrations.RenameModel("ReflexivePony", "ReflexivePony2"),
    
  1002.             ],
    
  1003.             atomic=connection.features.supports_atomic_references_rename,
    
  1004.         )
    
  1005.         Pony = project_state.apps.get_model(app_label, "ReflexivePony2")
    
  1006.         pony = Pony.objects.create()
    
  1007.         pony.ponies.add(pony)
    
  1008. 
    
  1009.     def test_rename_model_with_m2m(self):
    
  1010.         app_label = "test_rename_model_with_m2m"
    
  1011.         project_state = self.apply_operations(
    
  1012.             app_label,
    
  1013.             ProjectState(),
    
  1014.             operations=[
    
  1015.                 migrations.CreateModel(
    
  1016.                     "Rider",
    
  1017.                     fields=[
    
  1018.                         ("id", models.AutoField(primary_key=True)),
    
  1019.                     ],
    
  1020.                 ),
    
  1021.                 migrations.CreateModel(
    
  1022.                     "Pony",
    
  1023.                     fields=[
    
  1024.                         ("id", models.AutoField(primary_key=True)),
    
  1025.                         ("riders", models.ManyToManyField("Rider")),
    
  1026.                     ],
    
  1027.                 ),
    
  1028.             ],
    
  1029.         )
    
  1030.         Pony = project_state.apps.get_model(app_label, "Pony")
    
  1031.         Rider = project_state.apps.get_model(app_label, "Rider")
    
  1032.         pony = Pony.objects.create()
    
  1033.         rider = Rider.objects.create()
    
  1034.         pony.riders.add(rider)
    
  1035. 
    
  1036.         project_state = self.apply_operations(
    
  1037.             app_label,
    
  1038.             project_state,
    
  1039.             operations=[
    
  1040.                 migrations.RenameModel("Pony", "Pony2"),
    
  1041.             ],
    
  1042.             atomic=connection.features.supports_atomic_references_rename,
    
  1043.         )
    
  1044.         Pony = project_state.apps.get_model(app_label, "Pony2")
    
  1045.         Rider = project_state.apps.get_model(app_label, "Rider")
    
  1046.         pony = Pony.objects.create()
    
  1047.         rider = Rider.objects.create()
    
  1048.         pony.riders.add(rider)
    
  1049.         self.assertEqual(Pony.objects.count(), 2)
    
  1050.         self.assertEqual(Rider.objects.count(), 2)
    
  1051.         self.assertEqual(
    
  1052.             Pony._meta.get_field("riders").remote_field.through.objects.count(), 2
    
  1053.         )
    
  1054. 
    
  1055.     def test_rename_model_with_db_table_rename_m2m(self):
    
  1056.         app_label = "test_rmwdbrm2m"
    
  1057.         project_state = self.apply_operations(
    
  1058.             app_label,
    
  1059.             ProjectState(),
    
  1060.             operations=[
    
  1061.                 migrations.CreateModel(
    
  1062.                     "Rider",
    
  1063.                     fields=[
    
  1064.                         ("id", models.AutoField(primary_key=True)),
    
  1065.                     ],
    
  1066.                 ),
    
  1067.                 migrations.CreateModel(
    
  1068.                     "Pony",
    
  1069.                     fields=[
    
  1070.                         ("id", models.AutoField(primary_key=True)),
    
  1071.                         ("riders", models.ManyToManyField("Rider")),
    
  1072.                     ],
    
  1073.                     options={"db_table": "pony"},
    
  1074.                 ),
    
  1075.             ],
    
  1076.         )
    
  1077.         new_state = self.apply_operations(
    
  1078.             app_label,
    
  1079.             project_state,
    
  1080.             operations=[migrations.RenameModel("Pony", "PinkPony")],
    
  1081.             atomic=connection.features.supports_atomic_references_rename,
    
  1082.         )
    
  1083.         Pony = new_state.apps.get_model(app_label, "PinkPony")
    
  1084.         Rider = new_state.apps.get_model(app_label, "Rider")
    
  1085.         pony = Pony.objects.create()
    
  1086.         rider = Rider.objects.create()
    
  1087.         pony.riders.add(rider)
    
  1088. 
    
  1089.     def test_rename_m2m_target_model(self):
    
  1090.         app_label = "test_rename_m2m_target_model"
    
  1091.         project_state = self.apply_operations(
    
  1092.             app_label,
    
  1093.             ProjectState(),
    
  1094.             operations=[
    
  1095.                 migrations.CreateModel(
    
  1096.                     "Rider",
    
  1097.                     fields=[
    
  1098.                         ("id", models.AutoField(primary_key=True)),
    
  1099.                     ],
    
  1100.                 ),
    
  1101.                 migrations.CreateModel(
    
  1102.                     "Pony",
    
  1103.                     fields=[
    
  1104.                         ("id", models.AutoField(primary_key=True)),
    
  1105.                         ("riders", models.ManyToManyField("Rider")),
    
  1106.                     ],
    
  1107.                 ),
    
  1108.             ],
    
  1109.         )
    
  1110.         Pony = project_state.apps.get_model(app_label, "Pony")
    
  1111.         Rider = project_state.apps.get_model(app_label, "Rider")
    
  1112.         pony = Pony.objects.create()
    
  1113.         rider = Rider.objects.create()
    
  1114.         pony.riders.add(rider)
    
  1115. 
    
  1116.         project_state = self.apply_operations(
    
  1117.             app_label,
    
  1118.             project_state,
    
  1119.             operations=[
    
  1120.                 migrations.RenameModel("Rider", "Rider2"),
    
  1121.             ],
    
  1122.             atomic=connection.features.supports_atomic_references_rename,
    
  1123.         )
    
  1124.         Pony = project_state.apps.get_model(app_label, "Pony")
    
  1125.         Rider = project_state.apps.get_model(app_label, "Rider2")
    
  1126.         pony = Pony.objects.create()
    
  1127.         rider = Rider.objects.create()
    
  1128.         pony.riders.add(rider)
    
  1129.         self.assertEqual(Pony.objects.count(), 2)
    
  1130.         self.assertEqual(Rider.objects.count(), 2)
    
  1131.         self.assertEqual(
    
  1132.             Pony._meta.get_field("riders").remote_field.through.objects.count(), 2
    
  1133.         )
    
  1134. 
    
  1135.     def test_rename_m2m_through_model(self):
    
  1136.         app_label = "test_rename_through"
    
  1137.         project_state = self.apply_operations(
    
  1138.             app_label,
    
  1139.             ProjectState(),
    
  1140.             operations=[
    
  1141.                 migrations.CreateModel(
    
  1142.                     "Rider",
    
  1143.                     fields=[
    
  1144.                         ("id", models.AutoField(primary_key=True)),
    
  1145.                     ],
    
  1146.                 ),
    
  1147.                 migrations.CreateModel(
    
  1148.                     "Pony",
    
  1149.                     fields=[
    
  1150.                         ("id", models.AutoField(primary_key=True)),
    
  1151.                     ],
    
  1152.                 ),
    
  1153.                 migrations.CreateModel(
    
  1154.                     "PonyRider",
    
  1155.                     fields=[
    
  1156.                         ("id", models.AutoField(primary_key=True)),
    
  1157.                         (
    
  1158.                             "rider",
    
  1159.                             models.ForeignKey(
    
  1160.                                 "test_rename_through.Rider", models.CASCADE
    
  1161.                             ),
    
  1162.                         ),
    
  1163.                         (
    
  1164.                             "pony",
    
  1165.                             models.ForeignKey(
    
  1166.                                 "test_rename_through.Pony", models.CASCADE
    
  1167.                             ),
    
  1168.                         ),
    
  1169.                     ],
    
  1170.                 ),
    
  1171.                 migrations.AddField(
    
  1172.                     "Pony",
    
  1173.                     "riders",
    
  1174.                     models.ManyToManyField(
    
  1175.                         "test_rename_through.Rider",
    
  1176.                         through="test_rename_through.PonyRider",
    
  1177.                     ),
    
  1178.                 ),
    
  1179.             ],
    
  1180.         )
    
  1181.         Pony = project_state.apps.get_model(app_label, "Pony")
    
  1182.         Rider = project_state.apps.get_model(app_label, "Rider")
    
  1183.         PonyRider = project_state.apps.get_model(app_label, "PonyRider")
    
  1184.         pony = Pony.objects.create()
    
  1185.         rider = Rider.objects.create()
    
  1186.         PonyRider.objects.create(pony=pony, rider=rider)
    
  1187. 
    
  1188.         project_state = self.apply_operations(
    
  1189.             app_label,
    
  1190.             project_state,
    
  1191.             operations=[
    
  1192.                 migrations.RenameModel("PonyRider", "PonyRider2"),
    
  1193.             ],
    
  1194.         )
    
  1195.         Pony = project_state.apps.get_model(app_label, "Pony")
    
  1196.         Rider = project_state.apps.get_model(app_label, "Rider")
    
  1197.         PonyRider = project_state.apps.get_model(app_label, "PonyRider2")
    
  1198.         pony = Pony.objects.first()
    
  1199.         rider = Rider.objects.create()
    
  1200.         PonyRider.objects.create(pony=pony, rider=rider)
    
  1201.         self.assertEqual(Pony.objects.count(), 1)
    
  1202.         self.assertEqual(Rider.objects.count(), 2)
    
  1203.         self.assertEqual(PonyRider.objects.count(), 2)
    
  1204.         self.assertEqual(pony.riders.count(), 2)
    
  1205. 
    
  1206.     def test_rename_m2m_model_after_rename_field(self):
    
  1207.         """RenameModel renames a many-to-many column after a RenameField."""
    
  1208.         app_label = "test_rename_multiple"
    
  1209.         project_state = self.apply_operations(
    
  1210.             app_label,
    
  1211.             ProjectState(),
    
  1212.             operations=[
    
  1213.                 migrations.CreateModel(
    
  1214.                     "Pony",
    
  1215.                     fields=[
    
  1216.                         ("id", models.AutoField(primary_key=True)),
    
  1217.                         ("name", models.CharField(max_length=20)),
    
  1218.                     ],
    
  1219.                 ),
    
  1220.                 migrations.CreateModel(
    
  1221.                     "Rider",
    
  1222.                     fields=[
    
  1223.                         ("id", models.AutoField(primary_key=True)),
    
  1224.                         (
    
  1225.                             "pony",
    
  1226.                             models.ForeignKey(
    
  1227.                                 "test_rename_multiple.Pony", models.CASCADE
    
  1228.                             ),
    
  1229.                         ),
    
  1230.                     ],
    
  1231.                 ),
    
  1232.                 migrations.CreateModel(
    
  1233.                     "PonyRider",
    
  1234.                     fields=[
    
  1235.                         ("id", models.AutoField(primary_key=True)),
    
  1236.                         ("riders", models.ManyToManyField("Rider")),
    
  1237.                     ],
    
  1238.                 ),
    
  1239.                 migrations.RenameField(
    
  1240.                     model_name="pony", old_name="name", new_name="fancy_name"
    
  1241.                 ),
    
  1242.                 migrations.RenameModel(old_name="Rider", new_name="Jockey"),
    
  1243.             ],
    
  1244.             atomic=connection.features.supports_atomic_references_rename,
    
  1245.         )
    
  1246.         Pony = project_state.apps.get_model(app_label, "Pony")
    
  1247.         Jockey = project_state.apps.get_model(app_label, "Jockey")
    
  1248.         PonyRider = project_state.apps.get_model(app_label, "PonyRider")
    
  1249.         # No "no such column" error means the column was renamed correctly.
    
  1250.         pony = Pony.objects.create(fancy_name="a good name")
    
  1251.         jockey = Jockey.objects.create(pony=pony)
    
  1252.         ponyrider = PonyRider.objects.create()
    
  1253.         ponyrider.riders.add(jockey)
    
  1254. 
    
  1255.     def test_add_field(self):
    
  1256.         """
    
  1257.         Tests the AddField operation.
    
  1258.         """
    
  1259.         # Test the state alteration
    
  1260.         operation = migrations.AddField(
    
  1261.             "Pony",
    
  1262.             "height",
    
  1263.             models.FloatField(null=True, default=5),
    
  1264.         )
    
  1265.         self.assertEqual(operation.describe(), "Add field height to Pony")
    
  1266.         self.assertEqual(operation.migration_name_fragment, "pony_height")
    
  1267.         project_state, new_state = self.make_test_state("test_adfl", operation)
    
  1268.         self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
    
  1269.         field = new_state.models["test_adfl", "pony"].fields["height"]
    
  1270.         self.assertEqual(field.default, 5)
    
  1271.         # Test the database alteration
    
  1272.         self.assertColumnNotExists("test_adfl_pony", "height")
    
  1273.         with connection.schema_editor() as editor:
    
  1274.             operation.database_forwards("test_adfl", editor, project_state, new_state)
    
  1275.         self.assertColumnExists("test_adfl_pony", "height")
    
  1276.         # And test reversal
    
  1277.         with connection.schema_editor() as editor:
    
  1278.             operation.database_backwards("test_adfl", editor, new_state, project_state)
    
  1279.         self.assertColumnNotExists("test_adfl_pony", "height")
    
  1280.         # And deconstruction
    
  1281.         definition = operation.deconstruct()
    
  1282.         self.assertEqual(definition[0], "AddField")
    
  1283.         self.assertEqual(definition[1], [])
    
  1284.         self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
    
  1285. 
    
  1286.     def test_add_charfield(self):
    
  1287.         """
    
  1288.         Tests the AddField operation on TextField.
    
  1289.         """
    
  1290.         project_state = self.set_up_test_model("test_adchfl")
    
  1291. 
    
  1292.         Pony = project_state.apps.get_model("test_adchfl", "Pony")
    
  1293.         pony = Pony.objects.create(weight=42)
    
  1294. 
    
  1295.         new_state = self.apply_operations(
    
  1296.             "test_adchfl",
    
  1297.             project_state,
    
  1298.             [
    
  1299.                 migrations.AddField(
    
  1300.                     "Pony",
    
  1301.                     "text",
    
  1302.                     models.CharField(max_length=10, default="some text"),
    
  1303.                 ),
    
  1304.                 migrations.AddField(
    
  1305.                     "Pony",
    
  1306.                     "empty",
    
  1307.                     models.CharField(max_length=10, default=""),
    
  1308.                 ),
    
  1309.                 # If not properly quoted digits would be interpreted as an int.
    
  1310.                 migrations.AddField(
    
  1311.                     "Pony",
    
  1312.                     "digits",
    
  1313.                     models.CharField(max_length=10, default="42"),
    
  1314.                 ),
    
  1315.                 # Manual quoting is fragile and could trip on quotes. Refs #xyz.
    
  1316.                 migrations.AddField(
    
  1317.                     "Pony",
    
  1318.                     "quotes",
    
  1319.                     models.CharField(max_length=10, default='"\'"'),
    
  1320.                 ),
    
  1321.             ],
    
  1322.         )
    
  1323. 
    
  1324.         Pony = new_state.apps.get_model("test_adchfl", "Pony")
    
  1325.         pony = Pony.objects.get(pk=pony.pk)
    
  1326.         self.assertEqual(pony.text, "some text")
    
  1327.         self.assertEqual(pony.empty, "")
    
  1328.         self.assertEqual(pony.digits, "42")
    
  1329.         self.assertEqual(pony.quotes, '"\'"')
    
  1330. 
    
  1331.     def test_add_textfield(self):
    
  1332.         """
    
  1333.         Tests the AddField operation on TextField.
    
  1334.         """
    
  1335.         project_state = self.set_up_test_model("test_adtxtfl")
    
  1336. 
    
  1337.         Pony = project_state.apps.get_model("test_adtxtfl", "Pony")
    
  1338.         pony = Pony.objects.create(weight=42)
    
  1339. 
    
  1340.         new_state = self.apply_operations(
    
  1341.             "test_adtxtfl",
    
  1342.             project_state,
    
  1343.             [
    
  1344.                 migrations.AddField(
    
  1345.                     "Pony",
    
  1346.                     "text",
    
  1347.                     models.TextField(default="some text"),
    
  1348.                 ),
    
  1349.                 migrations.AddField(
    
  1350.                     "Pony",
    
  1351.                     "empty",
    
  1352.                     models.TextField(default=""),
    
  1353.                 ),
    
  1354.                 # If not properly quoted digits would be interpreted as an int.
    
  1355.                 migrations.AddField(
    
  1356.                     "Pony",
    
  1357.                     "digits",
    
  1358.                     models.TextField(default="42"),
    
  1359.                 ),
    
  1360.                 # Manual quoting is fragile and could trip on quotes. Refs #xyz.
    
  1361.                 migrations.AddField(
    
  1362.                     "Pony",
    
  1363.                     "quotes",
    
  1364.                     models.TextField(default='"\'"'),
    
  1365.                 ),
    
  1366.             ],
    
  1367.         )
    
  1368. 
    
  1369.         Pony = new_state.apps.get_model("test_adtxtfl", "Pony")
    
  1370.         pony = Pony.objects.get(pk=pony.pk)
    
  1371.         self.assertEqual(pony.text, "some text")
    
  1372.         self.assertEqual(pony.empty, "")
    
  1373.         self.assertEqual(pony.digits, "42")
    
  1374.         self.assertEqual(pony.quotes, '"\'"')
    
  1375. 
    
  1376.     def test_add_binaryfield(self):
    
  1377.         """
    
  1378.         Tests the AddField operation on TextField/BinaryField.
    
  1379.         """
    
  1380.         project_state = self.set_up_test_model("test_adbinfl")
    
  1381. 
    
  1382.         Pony = project_state.apps.get_model("test_adbinfl", "Pony")
    
  1383.         pony = Pony.objects.create(weight=42)
    
  1384. 
    
  1385.         new_state = self.apply_operations(
    
  1386.             "test_adbinfl",
    
  1387.             project_state,
    
  1388.             [
    
  1389.                 migrations.AddField(
    
  1390.                     "Pony",
    
  1391.                     "blob",
    
  1392.                     models.BinaryField(default=b"some text"),
    
  1393.                 ),
    
  1394.                 migrations.AddField(
    
  1395.                     "Pony",
    
  1396.                     "empty",
    
  1397.                     models.BinaryField(default=b""),
    
  1398.                 ),
    
  1399.                 # If not properly quoted digits would be interpreted as an int.
    
  1400.                 migrations.AddField(
    
  1401.                     "Pony",
    
  1402.                     "digits",
    
  1403.                     models.BinaryField(default=b"42"),
    
  1404.                 ),
    
  1405.                 # Manual quoting is fragile and could trip on quotes. Refs #xyz.
    
  1406.                 migrations.AddField(
    
  1407.                     "Pony",
    
  1408.                     "quotes",
    
  1409.                     models.BinaryField(default=b'"\'"'),
    
  1410.                 ),
    
  1411.             ],
    
  1412.         )
    
  1413. 
    
  1414.         Pony = new_state.apps.get_model("test_adbinfl", "Pony")
    
  1415.         pony = Pony.objects.get(pk=pony.pk)
    
  1416.         # SQLite returns buffer/memoryview, cast to bytes for checking.
    
  1417.         self.assertEqual(bytes(pony.blob), b"some text")
    
  1418.         self.assertEqual(bytes(pony.empty), b"")
    
  1419.         self.assertEqual(bytes(pony.digits), b"42")
    
  1420.         self.assertEqual(bytes(pony.quotes), b'"\'"')
    
  1421. 
    
  1422.     def test_column_name_quoting(self):
    
  1423.         """
    
  1424.         Column names that are SQL keywords shouldn't cause problems when used
    
  1425.         in migrations (#22168).
    
  1426.         """
    
  1427.         project_state = self.set_up_test_model("test_regr22168")
    
  1428.         operation = migrations.AddField(
    
  1429.             "Pony",
    
  1430.             "order",
    
  1431.             models.IntegerField(default=0),
    
  1432.         )
    
  1433.         new_state = project_state.clone()
    
  1434.         operation.state_forwards("test_regr22168", new_state)
    
  1435.         with connection.schema_editor() as editor:
    
  1436.             operation.database_forwards(
    
  1437.                 "test_regr22168", editor, project_state, new_state
    
  1438.             )
    
  1439.         self.assertColumnExists("test_regr22168_pony", "order")
    
  1440. 
    
  1441.     def test_add_field_preserve_default(self):
    
  1442.         """
    
  1443.         Tests the AddField operation's state alteration
    
  1444.         when preserve_default = False.
    
  1445.         """
    
  1446.         project_state = self.set_up_test_model("test_adflpd")
    
  1447.         # Test the state alteration
    
  1448.         operation = migrations.AddField(
    
  1449.             "Pony",
    
  1450.             "height",
    
  1451.             models.FloatField(null=True, default=4),
    
  1452.             preserve_default=False,
    
  1453.         )
    
  1454.         new_state = project_state.clone()
    
  1455.         operation.state_forwards("test_adflpd", new_state)
    
  1456.         self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
    
  1457.         field = new_state.models["test_adflpd", "pony"].fields["height"]
    
  1458.         self.assertEqual(field.default, models.NOT_PROVIDED)
    
  1459.         # Test the database alteration
    
  1460.         project_state.apps.get_model("test_adflpd", "pony").objects.create(
    
  1461.             weight=4,
    
  1462.         )
    
  1463.         self.assertColumnNotExists("test_adflpd_pony", "height")
    
  1464.         with connection.schema_editor() as editor:
    
  1465.             operation.database_forwards("test_adflpd", editor, project_state, new_state)
    
  1466.         self.assertColumnExists("test_adflpd_pony", "height")
    
  1467.         # And deconstruction
    
  1468.         definition = operation.deconstruct()
    
  1469.         self.assertEqual(definition[0], "AddField")
    
  1470.         self.assertEqual(definition[1], [])
    
  1471.         self.assertEqual(
    
  1472.             sorted(definition[2]), ["field", "model_name", "name", "preserve_default"]
    
  1473.         )
    
  1474. 
    
  1475.     def test_add_field_m2m(self):
    
  1476.         """
    
  1477.         Tests the AddField operation with a ManyToManyField.
    
  1478.         """
    
  1479.         project_state = self.set_up_test_model("test_adflmm", second_model=True)
    
  1480.         # Test the state alteration
    
  1481.         operation = migrations.AddField(
    
  1482.             "Pony", "stables", models.ManyToManyField("Stable", related_name="ponies")
    
  1483.         )
    
  1484.         new_state = project_state.clone()
    
  1485.         operation.state_forwards("test_adflmm", new_state)
    
  1486.         self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 4)
    
  1487.         # Test the database alteration
    
  1488.         self.assertTableNotExists("test_adflmm_pony_stables")
    
  1489.         with connection.schema_editor() as editor:
    
  1490.             operation.database_forwards("test_adflmm", editor, project_state, new_state)
    
  1491.         self.assertTableExists("test_adflmm_pony_stables")
    
  1492.         self.assertColumnNotExists("test_adflmm_pony", "stables")
    
  1493.         # Make sure the M2M field actually works
    
  1494.         with atomic():
    
  1495.             Pony = new_state.apps.get_model("test_adflmm", "Pony")
    
  1496.             p = Pony.objects.create(pink=False, weight=4.55)
    
  1497.             p.stables.create()
    
  1498.             self.assertEqual(p.stables.count(), 1)
    
  1499.             p.stables.all().delete()
    
  1500.         # And test reversal
    
  1501.         with connection.schema_editor() as editor:
    
  1502.             operation.database_backwards(
    
  1503.                 "test_adflmm", editor, new_state, project_state
    
  1504.             )
    
  1505.         self.assertTableNotExists("test_adflmm_pony_stables")
    
  1506. 
    
  1507.     def test_alter_field_m2m(self):
    
  1508.         project_state = self.set_up_test_model("test_alflmm", second_model=True)
    
  1509. 
    
  1510.         project_state = self.apply_operations(
    
  1511.             "test_alflmm",
    
  1512.             project_state,
    
  1513.             operations=[
    
  1514.                 migrations.AddField(
    
  1515.                     "Pony",
    
  1516.                     "stables",
    
  1517.                     models.ManyToManyField("Stable", related_name="ponies"),
    
  1518.                 )
    
  1519.             ],
    
  1520.         )
    
  1521.         Pony = project_state.apps.get_model("test_alflmm", "Pony")
    
  1522.         self.assertFalse(Pony._meta.get_field("stables").blank)
    
  1523. 
    
  1524.         project_state = self.apply_operations(
    
  1525.             "test_alflmm",
    
  1526.             project_state,
    
  1527.             operations=[
    
  1528.                 migrations.AlterField(
    
  1529.                     "Pony",
    
  1530.                     "stables",
    
  1531.                     models.ManyToManyField(
    
  1532.                         to="Stable", related_name="ponies", blank=True
    
  1533.                     ),
    
  1534.                 )
    
  1535.             ],
    
  1536.         )
    
  1537.         Pony = project_state.apps.get_model("test_alflmm", "Pony")
    
  1538.         self.assertTrue(Pony._meta.get_field("stables").blank)
    
  1539. 
    
  1540.     def test_repoint_field_m2m(self):
    
  1541.         project_state = self.set_up_test_model(
    
  1542.             "test_alflmm", second_model=True, third_model=True
    
  1543.         )
    
  1544. 
    
  1545.         project_state = self.apply_operations(
    
  1546.             "test_alflmm",
    
  1547.             project_state,
    
  1548.             operations=[
    
  1549.                 migrations.AddField(
    
  1550.                     "Pony",
    
  1551.                     "places",
    
  1552.                     models.ManyToManyField("Stable", related_name="ponies"),
    
  1553.                 )
    
  1554.             ],
    
  1555.         )
    
  1556.         Pony = project_state.apps.get_model("test_alflmm", "Pony")
    
  1557. 
    
  1558.         project_state = self.apply_operations(
    
  1559.             "test_alflmm",
    
  1560.             project_state,
    
  1561.             operations=[
    
  1562.                 migrations.AlterField(
    
  1563.                     "Pony",
    
  1564.                     "places",
    
  1565.                     models.ManyToManyField(to="Van", related_name="ponies"),
    
  1566.                 )
    
  1567.             ],
    
  1568.         )
    
  1569. 
    
  1570.         # Ensure the new field actually works
    
  1571.         Pony = project_state.apps.get_model("test_alflmm", "Pony")
    
  1572.         p = Pony.objects.create(pink=False, weight=4.55)
    
  1573.         p.places.create()
    
  1574.         self.assertEqual(p.places.count(), 1)
    
  1575.         p.places.all().delete()
    
  1576. 
    
  1577.     def test_remove_field_m2m(self):
    
  1578.         project_state = self.set_up_test_model("test_rmflmm", second_model=True)
    
  1579. 
    
  1580.         project_state = self.apply_operations(
    
  1581.             "test_rmflmm",
    
  1582.             project_state,
    
  1583.             operations=[
    
  1584.                 migrations.AddField(
    
  1585.                     "Pony",
    
  1586.                     "stables",
    
  1587.                     models.ManyToManyField("Stable", related_name="ponies"),
    
  1588.                 )
    
  1589.             ],
    
  1590.         )
    
  1591.         self.assertTableExists("test_rmflmm_pony_stables")
    
  1592. 
    
  1593.         with_field_state = project_state.clone()
    
  1594.         operations = [migrations.RemoveField("Pony", "stables")]
    
  1595.         project_state = self.apply_operations(
    
  1596.             "test_rmflmm", project_state, operations=operations
    
  1597.         )
    
  1598.         self.assertTableNotExists("test_rmflmm_pony_stables")
    
  1599. 
    
  1600.         # And test reversal
    
  1601.         self.unapply_operations("test_rmflmm", with_field_state, operations=operations)
    
  1602.         self.assertTableExists("test_rmflmm_pony_stables")
    
  1603. 
    
  1604.     def test_remove_field_m2m_with_through(self):
    
  1605.         project_state = self.set_up_test_model("test_rmflmmwt", second_model=True)
    
  1606. 
    
  1607.         self.assertTableNotExists("test_rmflmmwt_ponystables")
    
  1608.         project_state = self.apply_operations(
    
  1609.             "test_rmflmmwt",
    
  1610.             project_state,
    
  1611.             operations=[
    
  1612.                 migrations.CreateModel(
    
  1613.                     "PonyStables",
    
  1614.                     fields=[
    
  1615.                         (
    
  1616.                             "pony",
    
  1617.                             models.ForeignKey("test_rmflmmwt.Pony", models.CASCADE),
    
  1618.                         ),
    
  1619.                         (
    
  1620.                             "stable",
    
  1621.                             models.ForeignKey("test_rmflmmwt.Stable", models.CASCADE),
    
  1622.                         ),
    
  1623.                     ],
    
  1624.                 ),
    
  1625.                 migrations.AddField(
    
  1626.                     "Pony",
    
  1627.                     "stables",
    
  1628.                     models.ManyToManyField(
    
  1629.                         "Stable",
    
  1630.                         related_name="ponies",
    
  1631.                         through="test_rmflmmwt.PonyStables",
    
  1632.                     ),
    
  1633.                 ),
    
  1634.             ],
    
  1635.         )
    
  1636.         self.assertTableExists("test_rmflmmwt_ponystables")
    
  1637. 
    
  1638.         operations = [
    
  1639.             migrations.RemoveField("Pony", "stables"),
    
  1640.             migrations.DeleteModel("PonyStables"),
    
  1641.         ]
    
  1642.         self.apply_operations("test_rmflmmwt", project_state, operations=operations)
    
  1643. 
    
  1644.     def test_remove_field(self):
    
  1645.         """
    
  1646.         Tests the RemoveField operation.
    
  1647.         """
    
  1648.         project_state = self.set_up_test_model("test_rmfl")
    
  1649.         # Test the state alteration
    
  1650.         operation = migrations.RemoveField("Pony", "pink")
    
  1651.         self.assertEqual(operation.describe(), "Remove field pink from Pony")
    
  1652.         self.assertEqual(operation.migration_name_fragment, "remove_pony_pink")
    
  1653.         new_state = project_state.clone()
    
  1654.         operation.state_forwards("test_rmfl", new_state)
    
  1655.         self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2)
    
  1656.         # Test the database alteration
    
  1657.         self.assertColumnExists("test_rmfl_pony", "pink")
    
  1658.         with connection.schema_editor() as editor:
    
  1659.             operation.database_forwards("test_rmfl", editor, project_state, new_state)
    
  1660.         self.assertColumnNotExists("test_rmfl_pony", "pink")
    
  1661.         # And test reversal
    
  1662.         with connection.schema_editor() as editor:
    
  1663.             operation.database_backwards("test_rmfl", editor, new_state, project_state)
    
  1664.         self.assertColumnExists("test_rmfl_pony", "pink")
    
  1665.         # And deconstruction
    
  1666.         definition = operation.deconstruct()
    
  1667.         self.assertEqual(definition[0], "RemoveField")
    
  1668.         self.assertEqual(definition[1], [])
    
  1669.         self.assertEqual(definition[2], {"model_name": "Pony", "name": "pink"})
    
  1670. 
    
  1671.     def test_remove_fk(self):
    
  1672.         """
    
  1673.         Tests the RemoveField operation on a foreign key.
    
  1674.         """
    
  1675.         project_state = self.set_up_test_model("test_rfk", related_model=True)
    
  1676.         self.assertColumnExists("test_rfk_rider", "pony_id")
    
  1677.         operation = migrations.RemoveField("Rider", "pony")
    
  1678. 
    
  1679.         new_state = project_state.clone()
    
  1680.         operation.state_forwards("test_rfk", new_state)
    
  1681.         with connection.schema_editor() as editor:
    
  1682.             operation.database_forwards("test_rfk", editor, project_state, new_state)
    
  1683.         self.assertColumnNotExists("test_rfk_rider", "pony_id")
    
  1684.         with connection.schema_editor() as editor:
    
  1685.             operation.database_backwards("test_rfk", editor, new_state, project_state)
    
  1686.         self.assertColumnExists("test_rfk_rider", "pony_id")
    
  1687. 
    
  1688.     def test_alter_model_table(self):
    
  1689.         """
    
  1690.         Tests the AlterModelTable operation.
    
  1691.         """
    
  1692.         project_state = self.set_up_test_model("test_almota")
    
  1693.         # Test the state alteration
    
  1694.         operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
    
  1695.         self.assertEqual(
    
  1696.             operation.describe(), "Rename table for Pony to test_almota_pony_2"
    
  1697.         )
    
  1698.         self.assertEqual(operation.migration_name_fragment, "alter_pony_table")
    
  1699.         new_state = project_state.clone()
    
  1700.         operation.state_forwards("test_almota", new_state)
    
  1701.         self.assertEqual(
    
  1702.             new_state.models["test_almota", "pony"].options["db_table"],
    
  1703.             "test_almota_pony_2",
    
  1704.         )
    
  1705.         # Test the database alteration
    
  1706.         self.assertTableExists("test_almota_pony")
    
  1707.         self.assertTableNotExists("test_almota_pony_2")
    
  1708.         with connection.schema_editor() as editor:
    
  1709.             operation.database_forwards("test_almota", editor, project_state, new_state)
    
  1710.         self.assertTableNotExists("test_almota_pony")
    
  1711.         self.assertTableExists("test_almota_pony_2")
    
  1712.         # And test reversal
    
  1713.         with connection.schema_editor() as editor:
    
  1714.             operation.database_backwards(
    
  1715.                 "test_almota", editor, new_state, project_state
    
  1716.             )
    
  1717.         self.assertTableExists("test_almota_pony")
    
  1718.         self.assertTableNotExists("test_almota_pony_2")
    
  1719.         # And deconstruction
    
  1720.         definition = operation.deconstruct()
    
  1721.         self.assertEqual(definition[0], "AlterModelTable")
    
  1722.         self.assertEqual(definition[1], [])
    
  1723.         self.assertEqual(definition[2], {"name": "Pony", "table": "test_almota_pony_2"})
    
  1724. 
    
  1725.     def test_alter_model_table_none(self):
    
  1726.         """
    
  1727.         Tests the AlterModelTable operation if the table name is set to None.
    
  1728.         """
    
  1729.         operation = migrations.AlterModelTable("Pony", None)
    
  1730.         self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
    
  1731. 
    
  1732.     def test_alter_model_table_noop(self):
    
  1733.         """
    
  1734.         Tests the AlterModelTable operation if the table name is not changed.
    
  1735.         """
    
  1736.         project_state = self.set_up_test_model("test_almota")
    
  1737.         # Test the state alteration
    
  1738.         operation = migrations.AlterModelTable("Pony", "test_almota_pony")
    
  1739.         new_state = project_state.clone()
    
  1740.         operation.state_forwards("test_almota", new_state)
    
  1741.         self.assertEqual(
    
  1742.             new_state.models["test_almota", "pony"].options["db_table"],
    
  1743.             "test_almota_pony",
    
  1744.         )
    
  1745.         # Test the database alteration
    
  1746.         self.assertTableExists("test_almota_pony")
    
  1747.         with connection.schema_editor() as editor:
    
  1748.             operation.database_forwards("test_almota", editor, project_state, new_state)
    
  1749.         self.assertTableExists("test_almota_pony")
    
  1750.         # And test reversal
    
  1751.         with connection.schema_editor() as editor:
    
  1752.             operation.database_backwards(
    
  1753.                 "test_almota", editor, new_state, project_state
    
  1754.             )
    
  1755.         self.assertTableExists("test_almota_pony")
    
  1756. 
    
  1757.     def test_alter_model_table_m2m(self):
    
  1758.         """
    
  1759.         AlterModelTable should rename auto-generated M2M tables.
    
  1760.         """
    
  1761.         app_label = "test_talflmltlm2m"
    
  1762.         pony_db_table = "pony_foo"
    
  1763.         project_state = self.set_up_test_model(
    
  1764.             app_label, second_model=True, db_table=pony_db_table
    
  1765.         )
    
  1766.         # Add the M2M field
    
  1767.         first_state = project_state.clone()
    
  1768.         operation = migrations.AddField(
    
  1769.             "Pony", "stables", models.ManyToManyField("Stable")
    
  1770.         )
    
  1771.         operation.state_forwards(app_label, first_state)
    
  1772.         with connection.schema_editor() as editor:
    
  1773.             operation.database_forwards(app_label, editor, project_state, first_state)
    
  1774.         original_m2m_table = "%s_%s" % (pony_db_table, "stables")
    
  1775.         new_m2m_table = "%s_%s" % (app_label, "pony_stables")
    
  1776.         self.assertTableExists(original_m2m_table)
    
  1777.         self.assertTableNotExists(new_m2m_table)
    
  1778.         # Rename the Pony db_table which should also rename the m2m table.
    
  1779.         second_state = first_state.clone()
    
  1780.         operation = migrations.AlterModelTable(name="pony", table=None)
    
  1781.         operation.state_forwards(app_label, second_state)
    
  1782.         atomic_rename = connection.features.supports_atomic_references_rename
    
  1783.         with connection.schema_editor(atomic=atomic_rename) as editor:
    
  1784.             operation.database_forwards(app_label, editor, first_state, second_state)
    
  1785.         self.assertTableExists(new_m2m_table)
    
  1786.         self.assertTableNotExists(original_m2m_table)
    
  1787.         # And test reversal
    
  1788.         with connection.schema_editor(atomic=atomic_rename) as editor:
    
  1789.             operation.database_backwards(app_label, editor, second_state, first_state)
    
  1790.         self.assertTableExists(original_m2m_table)
    
  1791.         self.assertTableNotExists(new_m2m_table)
    
  1792. 
    
  1793.     def test_alter_field(self):
    
  1794.         """
    
  1795.         Tests the AlterField operation.
    
  1796.         """
    
  1797.         project_state = self.set_up_test_model("test_alfl")
    
  1798.         # Test the state alteration
    
  1799.         operation = migrations.AlterField(
    
  1800.             "Pony", "pink", models.IntegerField(null=True)
    
  1801.         )
    
  1802.         self.assertEqual(operation.describe(), "Alter field pink on Pony")
    
  1803.         self.assertEqual(operation.migration_name_fragment, "alter_pony_pink")
    
  1804.         new_state = project_state.clone()
    
  1805.         operation.state_forwards("test_alfl", new_state)
    
  1806.         self.assertIs(
    
  1807.             project_state.models["test_alfl", "pony"].fields["pink"].null, False
    
  1808.         )
    
  1809.         self.assertIs(new_state.models["test_alfl", "pony"].fields["pink"].null, True)
    
  1810.         # Test the database alteration
    
  1811.         self.assertColumnNotNull("test_alfl_pony", "pink")
    
  1812.         with connection.schema_editor() as editor:
    
  1813.             operation.database_forwards("test_alfl", editor, project_state, new_state)
    
  1814.         self.assertColumnNull("test_alfl_pony", "pink")
    
  1815.         # And test reversal
    
  1816.         with connection.schema_editor() as editor:
    
  1817.             operation.database_backwards("test_alfl", editor, new_state, project_state)
    
  1818.         self.assertColumnNotNull("test_alfl_pony", "pink")
    
  1819.         # And deconstruction
    
  1820.         definition = operation.deconstruct()
    
  1821.         self.assertEqual(definition[0], "AlterField")
    
  1822.         self.assertEqual(definition[1], [])
    
  1823.         self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
    
  1824. 
    
  1825.     def test_alter_field_add_db_column_noop(self):
    
  1826.         """
    
  1827.         AlterField operation is a noop when adding only a db_column and the
    
  1828.         column name is not changed.
    
  1829.         """
    
  1830.         app_label = "test_afadbn"
    
  1831.         project_state = self.set_up_test_model(app_label, related_model=True)
    
  1832.         pony_table = "%s_pony" % app_label
    
  1833.         new_state = project_state.clone()
    
  1834.         operation = migrations.AlterField(
    
  1835.             "Pony", "weight", models.FloatField(db_column="weight")
    
  1836.         )
    
  1837.         operation.state_forwards(app_label, new_state)
    
  1838.         self.assertIsNone(
    
  1839.             project_state.models[app_label, "pony"].fields["weight"].db_column,
    
  1840.         )
    
  1841.         self.assertEqual(
    
  1842.             new_state.models[app_label, "pony"].fields["weight"].db_column,
    
  1843.             "weight",
    
  1844.         )
    
  1845.         self.assertColumnExists(pony_table, "weight")
    
  1846.         with connection.schema_editor() as editor:
    
  1847.             with self.assertNumQueries(0):
    
  1848.                 operation.database_forwards(app_label, editor, project_state, new_state)
    
  1849.         self.assertColumnExists(pony_table, "weight")
    
  1850.         with connection.schema_editor() as editor:
    
  1851.             with self.assertNumQueries(0):
    
  1852.                 operation.database_backwards(
    
  1853.                     app_label, editor, new_state, project_state
    
  1854.                 )
    
  1855.         self.assertColumnExists(pony_table, "weight")
    
  1856. 
    
  1857.         rider_table = "%s_rider" % app_label
    
  1858.         new_state = project_state.clone()
    
  1859.         operation = migrations.AlterField(
    
  1860.             "Rider",
    
  1861.             "pony",
    
  1862.             models.ForeignKey("Pony", models.CASCADE, db_column="pony_id"),
    
  1863.         )
    
  1864.         operation.state_forwards(app_label, new_state)
    
  1865.         self.assertIsNone(
    
  1866.             project_state.models[app_label, "rider"].fields["pony"].db_column,
    
  1867.         )
    
  1868.         self.assertIs(
    
  1869.             new_state.models[app_label, "rider"].fields["pony"].db_column,
    
  1870.             "pony_id",
    
  1871.         )
    
  1872.         self.assertColumnExists(rider_table, "pony_id")
    
  1873.         with connection.schema_editor() as editor:
    
  1874.             with self.assertNumQueries(0):
    
  1875.                 operation.database_forwards(app_label, editor, project_state, new_state)
    
  1876.         self.assertColumnExists(rider_table, "pony_id")
    
  1877.         with connection.schema_editor() as editor:
    
  1878.             with self.assertNumQueries(0):
    
  1879.                 operation.database_forwards(app_label, editor, new_state, project_state)
    
  1880.         self.assertColumnExists(rider_table, "pony_id")
    
  1881. 
    
  1882.     def test_alter_field_pk(self):
    
  1883.         """
    
  1884.         The AlterField operation on primary keys (things like PostgreSQL's
    
  1885.         SERIAL weirdness).
    
  1886.         """
    
  1887.         project_state = self.set_up_test_model("test_alflpk")
    
  1888.         # Test the state alteration
    
  1889.         operation = migrations.AlterField(
    
  1890.             "Pony", "id", models.IntegerField(primary_key=True)
    
  1891.         )
    
  1892.         new_state = project_state.clone()
    
  1893.         operation.state_forwards("test_alflpk", new_state)
    
  1894.         self.assertIsInstance(
    
  1895.             project_state.models["test_alflpk", "pony"].fields["id"],
    
  1896.             models.AutoField,
    
  1897.         )
    
  1898.         self.assertIsInstance(
    
  1899.             new_state.models["test_alflpk", "pony"].fields["id"],
    
  1900.             models.IntegerField,
    
  1901.         )
    
  1902.         # Test the database alteration
    
  1903.         with connection.schema_editor() as editor:
    
  1904.             operation.database_forwards("test_alflpk", editor, project_state, new_state)
    
  1905.         # And test reversal
    
  1906.         with connection.schema_editor() as editor:
    
  1907.             operation.database_backwards(
    
  1908.                 "test_alflpk", editor, new_state, project_state
    
  1909.             )
    
  1910. 
    
  1911.     @skipUnlessDBFeature("supports_foreign_keys")
    
  1912.     def test_alter_field_pk_fk(self):
    
  1913.         """
    
  1914.         Tests the AlterField operation on primary keys changes any FKs pointing to it.
    
  1915.         """
    
  1916.         project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
    
  1917.         project_state = self.apply_operations(
    
  1918.             "test_alflpkfk",
    
  1919.             project_state,
    
  1920.             [
    
  1921.                 migrations.CreateModel(
    
  1922.                     "Stable",
    
  1923.                     fields=[
    
  1924.                         ("ponies", models.ManyToManyField("Pony")),
    
  1925.                     ],
    
  1926.                 ),
    
  1927.                 migrations.AddField(
    
  1928.                     "Pony",
    
  1929.                     "stables",
    
  1930.                     models.ManyToManyField("Stable"),
    
  1931.                 ),
    
  1932.             ],
    
  1933.         )
    
  1934.         # Test the state alteration
    
  1935.         operation = migrations.AlterField(
    
  1936.             "Pony", "id", models.FloatField(primary_key=True)
    
  1937.         )
    
  1938.         new_state = project_state.clone()
    
  1939.         operation.state_forwards("test_alflpkfk", new_state)
    
  1940.         self.assertIsInstance(
    
  1941.             project_state.models["test_alflpkfk", "pony"].fields["id"],
    
  1942.             models.AutoField,
    
  1943.         )
    
  1944.         self.assertIsInstance(
    
  1945.             new_state.models["test_alflpkfk", "pony"].fields["id"],
    
  1946.             models.FloatField,
    
  1947.         )
    
  1948. 
    
  1949.         def assertIdTypeEqualsFkType():
    
  1950.             with connection.cursor() as cursor:
    
  1951.                 id_type, id_null = [
    
  1952.                     (c.type_code, c.null_ok)
    
  1953.                     for c in connection.introspection.get_table_description(
    
  1954.                         cursor, "test_alflpkfk_pony"
    
  1955.                     )
    
  1956.                     if c.name == "id"
    
  1957.                 ][0]
    
  1958.                 fk_type, fk_null = [
    
  1959.                     (c.type_code, c.null_ok)
    
  1960.                     for c in connection.introspection.get_table_description(
    
  1961.                         cursor, "test_alflpkfk_rider"
    
  1962.                     )
    
  1963.                     if c.name == "pony_id"
    
  1964.                 ][0]
    
  1965.                 m2m_fk_type, m2m_fk_null = [
    
  1966.                     (c.type_code, c.null_ok)
    
  1967.                     for c in connection.introspection.get_table_description(
    
  1968.                         cursor,
    
  1969.                         "test_alflpkfk_pony_stables",
    
  1970.                     )
    
  1971.                     if c.name == "pony_id"
    
  1972.                 ][0]
    
  1973.                 remote_m2m_fk_type, remote_m2m_fk_null = [
    
  1974.                     (c.type_code, c.null_ok)
    
  1975.                     for c in connection.introspection.get_table_description(
    
  1976.                         cursor,
    
  1977.                         "test_alflpkfk_stable_ponies",
    
  1978.                     )
    
  1979.                     if c.name == "pony_id"
    
  1980.                 ][0]
    
  1981.             self.assertEqual(id_type, fk_type)
    
  1982.             self.assertEqual(id_type, m2m_fk_type)
    
  1983.             self.assertEqual(id_type, remote_m2m_fk_type)
    
  1984.             self.assertEqual(id_null, fk_null)
    
  1985.             self.assertEqual(id_null, m2m_fk_null)
    
  1986.             self.assertEqual(id_null, remote_m2m_fk_null)
    
  1987. 
    
  1988.         assertIdTypeEqualsFkType()
    
  1989.         # Test the database alteration
    
  1990.         with connection.schema_editor() as editor:
    
  1991.             operation.database_forwards(
    
  1992.                 "test_alflpkfk", editor, project_state, new_state
    
  1993.             )
    
  1994.         assertIdTypeEqualsFkType()
    
  1995.         if connection.features.supports_foreign_keys:
    
  1996.             self.assertFKExists(
    
  1997.                 "test_alflpkfk_pony_stables",
    
  1998.                 ["pony_id"],
    
  1999.                 ("test_alflpkfk_pony", "id"),
    
  2000.             )
    
  2001.             self.assertFKExists(
    
  2002.                 "test_alflpkfk_stable_ponies",
    
  2003.                 ["pony_id"],
    
  2004.                 ("test_alflpkfk_pony", "id"),
    
  2005.             )
    
  2006.         # And test reversal
    
  2007.         with connection.schema_editor() as editor:
    
  2008.             operation.database_backwards(
    
  2009.                 "test_alflpkfk", editor, new_state, project_state
    
  2010.             )
    
  2011.         assertIdTypeEqualsFkType()
    
  2012.         if connection.features.supports_foreign_keys:
    
  2013.             self.assertFKExists(
    
  2014.                 "test_alflpkfk_pony_stables",
    
  2015.                 ["pony_id"],
    
  2016.                 ("test_alflpkfk_pony", "id"),
    
  2017.             )
    
  2018.             self.assertFKExists(
    
  2019.                 "test_alflpkfk_stable_ponies",
    
  2020.                 ["pony_id"],
    
  2021.                 ("test_alflpkfk_pony", "id"),
    
  2022.             )
    
  2023. 
    
  2024.     @skipUnlessDBFeature("supports_collation_on_charfield", "supports_foreign_keys")
    
  2025.     def test_alter_field_pk_fk_db_collation(self):
    
  2026.         """
    
  2027.         AlterField operation of db_collation on primary keys changes any FKs
    
  2028.         pointing to it.
    
  2029.         """
    
  2030.         collation = connection.features.test_collations.get("non_default")
    
  2031.         if not collation:
    
  2032.             self.skipTest("Language collations are not supported.")
    
  2033. 
    
  2034.         app_label = "test_alflpkfkdbc"
    
  2035.         project_state = self.apply_operations(
    
  2036.             app_label,
    
  2037.             ProjectState(),
    
  2038.             [
    
  2039.                 migrations.CreateModel(
    
  2040.                     "Pony",
    
  2041.                     [
    
  2042.                         ("id", models.CharField(primary_key=True, max_length=10)),
    
  2043.                     ],
    
  2044.                 ),
    
  2045.                 migrations.CreateModel(
    
  2046.                     "Rider",
    
  2047.                     [
    
  2048.                         ("pony", models.ForeignKey("Pony", models.CASCADE)),
    
  2049.                     ],
    
  2050.                 ),
    
  2051.                 migrations.CreateModel(
    
  2052.                     "Stable",
    
  2053.                     [
    
  2054.                         ("ponies", models.ManyToManyField("Pony")),
    
  2055.                     ],
    
  2056.                 ),
    
  2057.             ],
    
  2058.         )
    
  2059.         # State alteration.
    
  2060.         operation = migrations.AlterField(
    
  2061.             "Pony",
    
  2062.             "id",
    
  2063.             models.CharField(
    
  2064.                 primary_key=True,
    
  2065.                 max_length=10,
    
  2066.                 db_collation=collation,
    
  2067.             ),
    
  2068.         )
    
  2069.         new_state = project_state.clone()
    
  2070.         operation.state_forwards(app_label, new_state)
    
  2071.         # Database alteration.
    
  2072.         with connection.schema_editor() as editor:
    
  2073.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  2074.         self.assertColumnCollation(f"{app_label}_pony", "id", collation)
    
  2075.         self.assertColumnCollation(f"{app_label}_rider", "pony_id", collation)
    
  2076.         self.assertColumnCollation(f"{app_label}_stable_ponies", "pony_id", collation)
    
  2077.         # Reversal.
    
  2078.         with connection.schema_editor() as editor:
    
  2079.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  2080. 
    
  2081.     def test_alter_field_pk_mti_fk(self):
    
  2082.         app_label = "test_alflpkmtifk"
    
  2083.         project_state = self.set_up_test_model(app_label, mti_model=True)
    
  2084.         project_state = self.apply_operations(
    
  2085.             app_label,
    
  2086.             project_state,
    
  2087.             [
    
  2088.                 migrations.CreateModel(
    
  2089.                     "ShetlandRider",
    
  2090.                     fields=[
    
  2091.                         (
    
  2092.                             "pony",
    
  2093.                             models.ForeignKey(
    
  2094.                                 f"{app_label}.ShetlandPony", models.CASCADE
    
  2095.                             ),
    
  2096.                         ),
    
  2097.                     ],
    
  2098.                 ),
    
  2099.             ],
    
  2100.         )
    
  2101.         operation = migrations.AlterField(
    
  2102.             "Pony",
    
  2103.             "id",
    
  2104.             models.BigAutoField(primary_key=True),
    
  2105.         )
    
  2106.         new_state = project_state.clone()
    
  2107.         operation.state_forwards(app_label, new_state)
    
  2108.         self.assertIsInstance(
    
  2109.             new_state.models[app_label, "pony"].fields["id"],
    
  2110.             models.BigAutoField,
    
  2111.         )
    
  2112. 
    
  2113.         def _get_column_id_type(cursor, table, column):
    
  2114.             return [
    
  2115.                 c.type_code
    
  2116.                 for c in connection.introspection.get_table_description(
    
  2117.                     cursor,
    
  2118.                     f"{app_label}_{table}",
    
  2119.                 )
    
  2120.                 if c.name == column
    
  2121.             ][0]
    
  2122. 
    
  2123.         def assertIdTypeEqualsMTIFkType():
    
  2124.             with connection.cursor() as cursor:
    
  2125.                 parent_id_type = _get_column_id_type(cursor, "pony", "id")
    
  2126.                 child_id_type = _get_column_id_type(
    
  2127.                     cursor, "shetlandpony", "pony_ptr_id"
    
  2128.                 )
    
  2129.                 mti_id_type = _get_column_id_type(cursor, "shetlandrider", "pony_id")
    
  2130.             self.assertEqual(parent_id_type, child_id_type)
    
  2131.             self.assertEqual(parent_id_type, mti_id_type)
    
  2132. 
    
  2133.         assertIdTypeEqualsMTIFkType()
    
  2134.         # Alter primary key.
    
  2135.         with connection.schema_editor() as editor:
    
  2136.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  2137.         assertIdTypeEqualsMTIFkType()
    
  2138.         if connection.features.supports_foreign_keys:
    
  2139.             self.assertFKExists(
    
  2140.                 f"{app_label}_shetlandpony",
    
  2141.                 ["pony_ptr_id"],
    
  2142.                 (f"{app_label}_pony", "id"),
    
  2143.             )
    
  2144.             self.assertFKExists(
    
  2145.                 f"{app_label}_shetlandrider",
    
  2146.                 ["pony_id"],
    
  2147.                 (f"{app_label}_shetlandpony", "pony_ptr_id"),
    
  2148.             )
    
  2149.         # Reversal.
    
  2150.         with connection.schema_editor() as editor:
    
  2151.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  2152.         assertIdTypeEqualsMTIFkType()
    
  2153.         if connection.features.supports_foreign_keys:
    
  2154.             self.assertFKExists(
    
  2155.                 f"{app_label}_shetlandpony",
    
  2156.                 ["pony_ptr_id"],
    
  2157.                 (f"{app_label}_pony", "id"),
    
  2158.             )
    
  2159.             self.assertFKExists(
    
  2160.                 f"{app_label}_shetlandrider",
    
  2161.                 ["pony_id"],
    
  2162.                 (f"{app_label}_shetlandpony", "pony_ptr_id"),
    
  2163.             )
    
  2164. 
    
  2165.     def test_alter_field_pk_mti_and_fk_to_base(self):
    
  2166.         app_label = "test_alflpkmtiftb"
    
  2167.         project_state = self.set_up_test_model(
    
  2168.             app_label,
    
  2169.             mti_model=True,
    
  2170.             related_model=True,
    
  2171.         )
    
  2172.         operation = migrations.AlterField(
    
  2173.             "Pony",
    
  2174.             "id",
    
  2175.             models.BigAutoField(primary_key=True),
    
  2176.         )
    
  2177.         new_state = project_state.clone()
    
  2178.         operation.state_forwards(app_label, new_state)
    
  2179.         self.assertIsInstance(
    
  2180.             new_state.models[app_label, "pony"].fields["id"],
    
  2181.             models.BigAutoField,
    
  2182.         )
    
  2183. 
    
  2184.         def _get_column_id_type(cursor, table, column):
    
  2185.             return [
    
  2186.                 c.type_code
    
  2187.                 for c in connection.introspection.get_table_description(
    
  2188.                     cursor,
    
  2189.                     f"{app_label}_{table}",
    
  2190.                 )
    
  2191.                 if c.name == column
    
  2192.             ][0]
    
  2193. 
    
  2194.         def assertIdTypeEqualsMTIFkType():
    
  2195.             with connection.cursor() as cursor:
    
  2196.                 parent_id_type = _get_column_id_type(cursor, "pony", "id")
    
  2197.                 fk_id_type = _get_column_id_type(cursor, "rider", "pony_id")
    
  2198.                 child_id_type = _get_column_id_type(
    
  2199.                     cursor, "shetlandpony", "pony_ptr_id"
    
  2200.                 )
    
  2201.             self.assertEqual(parent_id_type, child_id_type)
    
  2202.             self.assertEqual(parent_id_type, fk_id_type)
    
  2203. 
    
  2204.         assertIdTypeEqualsMTIFkType()
    
  2205.         # Alter primary key.
    
  2206.         with connection.schema_editor() as editor:
    
  2207.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  2208.         assertIdTypeEqualsMTIFkType()
    
  2209.         if connection.features.supports_foreign_keys:
    
  2210.             self.assertFKExists(
    
  2211.                 f"{app_label}_shetlandpony",
    
  2212.                 ["pony_ptr_id"],
    
  2213.                 (f"{app_label}_pony", "id"),
    
  2214.             )
    
  2215.             self.assertFKExists(
    
  2216.                 f"{app_label}_rider",
    
  2217.                 ["pony_id"],
    
  2218.                 (f"{app_label}_pony", "id"),
    
  2219.             )
    
  2220.         # Reversal.
    
  2221.         with connection.schema_editor() as editor:
    
  2222.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  2223.         assertIdTypeEqualsMTIFkType()
    
  2224.         if connection.features.supports_foreign_keys:
    
  2225.             self.assertFKExists(
    
  2226.                 f"{app_label}_shetlandpony",
    
  2227.                 ["pony_ptr_id"],
    
  2228.                 (f"{app_label}_pony", "id"),
    
  2229.             )
    
  2230.             self.assertFKExists(
    
  2231.                 f"{app_label}_rider",
    
  2232.                 ["pony_id"],
    
  2233.                 (f"{app_label}_pony", "id"),
    
  2234.             )
    
  2235. 
    
  2236.     @skipUnlessDBFeature("supports_foreign_keys")
    
  2237.     def test_alter_field_reloads_state_on_fk_with_to_field_target_type_change(self):
    
  2238.         app_label = "test_alflrsfkwtflttc"
    
  2239.         project_state = self.apply_operations(
    
  2240.             app_label,
    
  2241.             ProjectState(),
    
  2242.             operations=[
    
  2243.                 migrations.CreateModel(
    
  2244.                     "Rider",
    
  2245.                     fields=[
    
  2246.                         ("id", models.AutoField(primary_key=True)),
    
  2247.                         ("code", models.IntegerField(unique=True)),
    
  2248.                     ],
    
  2249.                 ),
    
  2250.                 migrations.CreateModel(
    
  2251.                     "Pony",
    
  2252.                     fields=[
    
  2253.                         ("id", models.AutoField(primary_key=True)),
    
  2254.                         (
    
  2255.                             "rider",
    
  2256.                             models.ForeignKey(
    
  2257.                                 "%s.Rider" % app_label, models.CASCADE, to_field="code"
    
  2258.                             ),
    
  2259.                         ),
    
  2260.                     ],
    
  2261.                 ),
    
  2262.             ],
    
  2263.         )
    
  2264.         operation = migrations.AlterField(
    
  2265.             "Rider",
    
  2266.             "code",
    
  2267.             models.CharField(max_length=100, unique=True),
    
  2268.         )
    
  2269.         self.apply_operations(app_label, project_state, operations=[operation])
    
  2270.         id_type, id_null = [
    
  2271.             (c.type_code, c.null_ok)
    
  2272.             for c in self.get_table_description("%s_rider" % app_label)
    
  2273.             if c.name == "code"
    
  2274.         ][0]
    
  2275.         fk_type, fk_null = [
    
  2276.             (c.type_code, c.null_ok)
    
  2277.             for c in self.get_table_description("%s_pony" % app_label)
    
  2278.             if c.name == "rider_id"
    
  2279.         ][0]
    
  2280.         self.assertEqual(id_type, fk_type)
    
  2281.         self.assertEqual(id_null, fk_null)
    
  2282. 
    
  2283.     @skipUnlessDBFeature("supports_foreign_keys")
    
  2284.     def test_alter_field_reloads_state_fk_with_to_field_related_name_target_type_change(
    
  2285.         self,
    
  2286.     ):
    
  2287.         app_label = "test_alflrsfkwtflrnttc"
    
  2288.         project_state = self.apply_operations(
    
  2289.             app_label,
    
  2290.             ProjectState(),
    
  2291.             operations=[
    
  2292.                 migrations.CreateModel(
    
  2293.                     "Rider",
    
  2294.                     fields=[
    
  2295.                         ("id", models.AutoField(primary_key=True)),
    
  2296.                         ("code", models.PositiveIntegerField(unique=True)),
    
  2297.                     ],
    
  2298.                 ),
    
  2299.                 migrations.CreateModel(
    
  2300.                     "Pony",
    
  2301.                     fields=[
    
  2302.                         ("id", models.AutoField(primary_key=True)),
    
  2303.                         (
    
  2304.                             "rider",
    
  2305.                             models.ForeignKey(
    
  2306.                                 "%s.Rider" % app_label,
    
  2307.                                 models.CASCADE,
    
  2308.                                 to_field="code",
    
  2309.                                 related_name="+",
    
  2310.                             ),
    
  2311.                         ),
    
  2312.                     ],
    
  2313.                 ),
    
  2314.             ],
    
  2315.         )
    
  2316.         operation = migrations.AlterField(
    
  2317.             "Rider",
    
  2318.             "code",
    
  2319.             models.CharField(max_length=100, unique=True),
    
  2320.         )
    
  2321.         self.apply_operations(app_label, project_state, operations=[operation])
    
  2322. 
    
  2323.     def test_alter_field_reloads_state_on_fk_target_changes(self):
    
  2324.         """
    
  2325.         If AlterField doesn't reload state appropriately, the second AlterField
    
  2326.         crashes on MySQL due to not dropping the PonyRider.pony foreign key
    
  2327.         constraint before modifying the column.
    
  2328.         """
    
  2329.         app_label = "alter_alter_field_reloads_state_on_fk_target_changes"
    
  2330.         project_state = self.apply_operations(
    
  2331.             app_label,
    
  2332.             ProjectState(),
    
  2333.             operations=[
    
  2334.                 migrations.CreateModel(
    
  2335.                     "Rider",
    
  2336.                     fields=[
    
  2337.                         ("id", models.CharField(primary_key=True, max_length=100)),
    
  2338.                     ],
    
  2339.                 ),
    
  2340.                 migrations.CreateModel(
    
  2341.                     "Pony",
    
  2342.                     fields=[
    
  2343.                         ("id", models.CharField(primary_key=True, max_length=100)),
    
  2344.                         (
    
  2345.                             "rider",
    
  2346.                             models.ForeignKey("%s.Rider" % app_label, models.CASCADE),
    
  2347.                         ),
    
  2348.                     ],
    
  2349.                 ),
    
  2350.                 migrations.CreateModel(
    
  2351.                     "PonyRider",
    
  2352.                     fields=[
    
  2353.                         ("id", models.AutoField(primary_key=True)),
    
  2354.                         (
    
  2355.                             "pony",
    
  2356.                             models.ForeignKey("%s.Pony" % app_label, models.CASCADE),
    
  2357.                         ),
    
  2358.                     ],
    
  2359.                 ),
    
  2360.             ],
    
  2361.         )
    
  2362.         project_state = self.apply_operations(
    
  2363.             app_label,
    
  2364.             project_state,
    
  2365.             operations=[
    
  2366.                 migrations.AlterField(
    
  2367.                     "Rider", "id", models.CharField(primary_key=True, max_length=99)
    
  2368.                 ),
    
  2369.                 migrations.AlterField(
    
  2370.                     "Pony", "id", models.CharField(primary_key=True, max_length=99)
    
  2371.                 ),
    
  2372.             ],
    
  2373.         )
    
  2374. 
    
  2375.     def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
    
  2376.         """
    
  2377.         If AlterField doesn't reload state appropriately, the second AlterField
    
  2378.         crashes on MySQL due to not dropping the PonyRider.pony foreign key
    
  2379.         constraint before modifying the column.
    
  2380.         """
    
  2381.         app_label = "alter_alter_field_reloads_state_on_fk_with_to_field_target_changes"
    
  2382.         project_state = self.apply_operations(
    
  2383.             app_label,
    
  2384.             ProjectState(),
    
  2385.             operations=[
    
  2386.                 migrations.CreateModel(
    
  2387.                     "Rider",
    
  2388.                     fields=[
    
  2389.                         ("id", models.CharField(primary_key=True, max_length=100)),
    
  2390.                         ("slug", models.CharField(unique=True, max_length=100)),
    
  2391.                     ],
    
  2392.                 ),
    
  2393.                 migrations.CreateModel(
    
  2394.                     "Pony",
    
  2395.                     fields=[
    
  2396.                         ("id", models.CharField(primary_key=True, max_length=100)),
    
  2397.                         (
    
  2398.                             "rider",
    
  2399.                             models.ForeignKey(
    
  2400.                                 "%s.Rider" % app_label, models.CASCADE, to_field="slug"
    
  2401.                             ),
    
  2402.                         ),
    
  2403.                         ("slug", models.CharField(unique=True, max_length=100)),
    
  2404.                     ],
    
  2405.                 ),
    
  2406.                 migrations.CreateModel(
    
  2407.                     "PonyRider",
    
  2408.                     fields=[
    
  2409.                         ("id", models.AutoField(primary_key=True)),
    
  2410.                         (
    
  2411.                             "pony",
    
  2412.                             models.ForeignKey(
    
  2413.                                 "%s.Pony" % app_label, models.CASCADE, to_field="slug"
    
  2414.                             ),
    
  2415.                         ),
    
  2416.                     ],
    
  2417.                 ),
    
  2418.             ],
    
  2419.         )
    
  2420.         project_state = self.apply_operations(
    
  2421.             app_label,
    
  2422.             project_state,
    
  2423.             operations=[
    
  2424.                 migrations.AlterField(
    
  2425.                     "Rider", "slug", models.CharField(unique=True, max_length=99)
    
  2426.                 ),
    
  2427.                 migrations.AlterField(
    
  2428.                     "Pony", "slug", models.CharField(unique=True, max_length=99)
    
  2429.                 ),
    
  2430.             ],
    
  2431.         )
    
  2432. 
    
  2433.     def test_rename_field_reloads_state_on_fk_target_changes(self):
    
  2434.         """
    
  2435.         If RenameField doesn't reload state appropriately, the AlterField
    
  2436.         crashes on MySQL due to not dropping the PonyRider.pony foreign key
    
  2437.         constraint before modifying the column.
    
  2438.         """
    
  2439.         app_label = "alter_rename_field_reloads_state_on_fk_target_changes"
    
  2440.         project_state = self.apply_operations(
    
  2441.             app_label,
    
  2442.             ProjectState(),
    
  2443.             operations=[
    
  2444.                 migrations.CreateModel(
    
  2445.                     "Rider",
    
  2446.                     fields=[
    
  2447.                         ("id", models.CharField(primary_key=True, max_length=100)),
    
  2448.                     ],
    
  2449.                 ),
    
  2450.                 migrations.CreateModel(
    
  2451.                     "Pony",
    
  2452.                     fields=[
    
  2453.                         ("id", models.CharField(primary_key=True, max_length=100)),
    
  2454.                         (
    
  2455.                             "rider",
    
  2456.                             models.ForeignKey("%s.Rider" % app_label, models.CASCADE),
    
  2457.                         ),
    
  2458.                     ],
    
  2459.                 ),
    
  2460.                 migrations.CreateModel(
    
  2461.                     "PonyRider",
    
  2462.                     fields=[
    
  2463.                         ("id", models.AutoField(primary_key=True)),
    
  2464.                         (
    
  2465.                             "pony",
    
  2466.                             models.ForeignKey("%s.Pony" % app_label, models.CASCADE),
    
  2467.                         ),
    
  2468.                     ],
    
  2469.                 ),
    
  2470.             ],
    
  2471.         )
    
  2472.         project_state = self.apply_operations(
    
  2473.             app_label,
    
  2474.             project_state,
    
  2475.             operations=[
    
  2476.                 migrations.RenameField("Rider", "id", "id2"),
    
  2477.                 migrations.AlterField(
    
  2478.                     "Pony", "id", models.CharField(primary_key=True, max_length=99)
    
  2479.                 ),
    
  2480.             ],
    
  2481.             atomic=connection.features.supports_atomic_references_rename,
    
  2482.         )
    
  2483. 
    
  2484.     def test_rename_field(self):
    
  2485.         """
    
  2486.         Tests the RenameField operation.
    
  2487.         """
    
  2488.         project_state = self.set_up_test_model(
    
  2489.             "test_rnfl", unique_together=True, index_together=True
    
  2490.         )
    
  2491.         # Test the state alteration
    
  2492.         operation = migrations.RenameField("Pony", "pink", "blue")
    
  2493.         self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
    
  2494.         self.assertEqual(operation.migration_name_fragment, "rename_pink_pony_blue")
    
  2495.         new_state = project_state.clone()
    
  2496.         operation.state_forwards("test_rnfl", new_state)
    
  2497.         self.assertIn("blue", new_state.models["test_rnfl", "pony"].fields)
    
  2498.         self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].fields)
    
  2499.         # Make sure the unique_together has the renamed column too
    
  2500.         self.assertIn(
    
  2501.             "blue", new_state.models["test_rnfl", "pony"].options["unique_together"][0]
    
  2502.         )
    
  2503.         self.assertNotIn(
    
  2504.             "pink", new_state.models["test_rnfl", "pony"].options["unique_together"][0]
    
  2505.         )
    
  2506.         # Make sure the index_together has the renamed column too
    
  2507.         self.assertIn(
    
  2508.             "blue", new_state.models["test_rnfl", "pony"].options["index_together"][0]
    
  2509.         )
    
  2510.         self.assertNotIn(
    
  2511.             "pink", new_state.models["test_rnfl", "pony"].options["index_together"][0]
    
  2512.         )
    
  2513.         # Test the database alteration
    
  2514.         self.assertColumnExists("test_rnfl_pony", "pink")
    
  2515.         self.assertColumnNotExists("test_rnfl_pony", "blue")
    
  2516.         with connection.schema_editor() as editor:
    
  2517.             operation.database_forwards("test_rnfl", editor, project_state, new_state)
    
  2518.         self.assertColumnExists("test_rnfl_pony", "blue")
    
  2519.         self.assertColumnNotExists("test_rnfl_pony", "pink")
    
  2520.         # Ensure the unique constraint has been ported over
    
  2521.         with connection.cursor() as cursor:
    
  2522.             cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
    
  2523.             with self.assertRaises(IntegrityError):
    
  2524.                 with atomic():
    
  2525.                     cursor.execute(
    
  2526.                         "INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)"
    
  2527.                     )
    
  2528.             cursor.execute("DELETE FROM test_rnfl_pony")
    
  2529.         # Ensure the index constraint has been ported over
    
  2530.         self.assertIndexExists("test_rnfl_pony", ["weight", "blue"])
    
  2531.         # And test reversal
    
  2532.         with connection.schema_editor() as editor:
    
  2533.             operation.database_backwards("test_rnfl", editor, new_state, project_state)
    
  2534.         self.assertColumnExists("test_rnfl_pony", "pink")
    
  2535.         self.assertColumnNotExists("test_rnfl_pony", "blue")
    
  2536.         # Ensure the index constraint has been reset
    
  2537.         self.assertIndexExists("test_rnfl_pony", ["weight", "pink"])
    
  2538.         # And deconstruction
    
  2539.         definition = operation.deconstruct()
    
  2540.         self.assertEqual(definition[0], "RenameField")
    
  2541.         self.assertEqual(definition[1], [])
    
  2542.         self.assertEqual(
    
  2543.             definition[2],
    
  2544.             {"model_name": "Pony", "old_name": "pink", "new_name": "blue"},
    
  2545.         )
    
  2546. 
    
  2547.     def test_rename_field_with_db_column(self):
    
  2548.         project_state = self.apply_operations(
    
  2549.             "test_rfwdbc",
    
  2550.             ProjectState(),
    
  2551.             operations=[
    
  2552.                 migrations.CreateModel(
    
  2553.                     "Pony",
    
  2554.                     fields=[
    
  2555.                         ("id", models.AutoField(primary_key=True)),
    
  2556.                         ("field", models.IntegerField(db_column="db_field")),
    
  2557.                         (
    
  2558.                             "fk_field",
    
  2559.                             models.ForeignKey(
    
  2560.                                 "Pony",
    
  2561.                                 models.CASCADE,
    
  2562.                                 db_column="db_fk_field",
    
  2563.                             ),
    
  2564.                         ),
    
  2565.                     ],
    
  2566.                 ),
    
  2567.             ],
    
  2568.         )
    
  2569.         new_state = project_state.clone()
    
  2570.         operation = migrations.RenameField("Pony", "field", "renamed_field")
    
  2571.         operation.state_forwards("test_rfwdbc", new_state)
    
  2572.         self.assertIn("renamed_field", new_state.models["test_rfwdbc", "pony"].fields)
    
  2573.         self.assertNotIn("field", new_state.models["test_rfwdbc", "pony"].fields)
    
  2574.         self.assertColumnExists("test_rfwdbc_pony", "db_field")
    
  2575.         with connection.schema_editor() as editor:
    
  2576.             with self.assertNumQueries(0):
    
  2577.                 operation.database_forwards(
    
  2578.                     "test_rfwdbc", editor, project_state, new_state
    
  2579.                 )
    
  2580.         self.assertColumnExists("test_rfwdbc_pony", "db_field")
    
  2581.         with connection.schema_editor() as editor:
    
  2582.             with self.assertNumQueries(0):
    
  2583.                 operation.database_backwards(
    
  2584.                     "test_rfwdbc", editor, new_state, project_state
    
  2585.                 )
    
  2586.         self.assertColumnExists("test_rfwdbc_pony", "db_field")
    
  2587. 
    
  2588.         new_state = project_state.clone()
    
  2589.         operation = migrations.RenameField("Pony", "fk_field", "renamed_fk_field")
    
  2590.         operation.state_forwards("test_rfwdbc", new_state)
    
  2591.         self.assertIn(
    
  2592.             "renamed_fk_field", new_state.models["test_rfwdbc", "pony"].fields
    
  2593.         )
    
  2594.         self.assertNotIn("fk_field", new_state.models["test_rfwdbc", "pony"].fields)
    
  2595.         self.assertColumnExists("test_rfwdbc_pony", "db_fk_field")
    
  2596.         with connection.schema_editor() as editor:
    
  2597.             with self.assertNumQueries(0):
    
  2598.                 operation.database_forwards(
    
  2599.                     "test_rfwdbc", editor, project_state, new_state
    
  2600.                 )
    
  2601.         self.assertColumnExists("test_rfwdbc_pony", "db_fk_field")
    
  2602.         with connection.schema_editor() as editor:
    
  2603.             with self.assertNumQueries(0):
    
  2604.                 operation.database_backwards(
    
  2605.                     "test_rfwdbc", editor, new_state, project_state
    
  2606.                 )
    
  2607.         self.assertColumnExists("test_rfwdbc_pony", "db_fk_field")
    
  2608. 
    
  2609.     def test_rename_field_case(self):
    
  2610.         project_state = self.apply_operations(
    
  2611.             "test_rfmx",
    
  2612.             ProjectState(),
    
  2613.             operations=[
    
  2614.                 migrations.CreateModel(
    
  2615.                     "Pony",
    
  2616.                     fields=[
    
  2617.                         ("id", models.AutoField(primary_key=True)),
    
  2618.                         ("field", models.IntegerField()),
    
  2619.                     ],
    
  2620.                 ),
    
  2621.             ],
    
  2622.         )
    
  2623.         new_state = project_state.clone()
    
  2624.         operation = migrations.RenameField("Pony", "field", "FiElD")
    
  2625.         operation.state_forwards("test_rfmx", new_state)
    
  2626.         self.assertIn("FiElD", new_state.models["test_rfmx", "pony"].fields)
    
  2627.         self.assertColumnExists("test_rfmx_pony", "field")
    
  2628.         with connection.schema_editor() as editor:
    
  2629.             operation.database_forwards("test_rfmx", editor, project_state, new_state)
    
  2630.         self.assertColumnExists(
    
  2631.             "test_rfmx_pony",
    
  2632.             connection.introspection.identifier_converter("FiElD"),
    
  2633.         )
    
  2634.         with connection.schema_editor() as editor:
    
  2635.             operation.database_backwards("test_rfmx", editor, new_state, project_state)
    
  2636.         self.assertColumnExists("test_rfmx_pony", "field")
    
  2637. 
    
  2638.     def test_rename_missing_field(self):
    
  2639.         state = ProjectState()
    
  2640.         state.add_model(ModelState("app", "model", []))
    
  2641.         with self.assertRaisesMessage(
    
  2642.             FieldDoesNotExist, "app.model has no field named 'field'"
    
  2643.         ):
    
  2644.             migrations.RenameField("model", "field", "new_field").state_forwards(
    
  2645.                 "app", state
    
  2646.             )
    
  2647. 
    
  2648.     def test_rename_referenced_field_state_forward(self):
    
  2649.         state = ProjectState()
    
  2650.         state.add_model(
    
  2651.             ModelState(
    
  2652.                 "app",
    
  2653.                 "Model",
    
  2654.                 [
    
  2655.                     ("id", models.AutoField(primary_key=True)),
    
  2656.                     ("field", models.IntegerField(unique=True)),
    
  2657.                 ],
    
  2658.             )
    
  2659.         )
    
  2660.         state.add_model(
    
  2661.             ModelState(
    
  2662.                 "app",
    
  2663.                 "OtherModel",
    
  2664.                 [
    
  2665.                     ("id", models.AutoField(primary_key=True)),
    
  2666.                     (
    
  2667.                         "fk",
    
  2668.                         models.ForeignKey("Model", models.CASCADE, to_field="field"),
    
  2669.                     ),
    
  2670.                     (
    
  2671.                         "fo",
    
  2672.                         models.ForeignObject(
    
  2673.                             "Model",
    
  2674.                             models.CASCADE,
    
  2675.                             from_fields=("fk",),
    
  2676.                             to_fields=("field",),
    
  2677.                         ),
    
  2678.                     ),
    
  2679.                 ],
    
  2680.             )
    
  2681.         )
    
  2682.         operation = migrations.RenameField("Model", "field", "renamed")
    
  2683.         new_state = state.clone()
    
  2684.         operation.state_forwards("app", new_state)
    
  2685.         self.assertEqual(
    
  2686.             new_state.models["app", "othermodel"].fields["fk"].remote_field.field_name,
    
  2687.             "renamed",
    
  2688.         )
    
  2689.         self.assertEqual(
    
  2690.             new_state.models["app", "othermodel"].fields["fk"].from_fields, ["self"]
    
  2691.         )
    
  2692.         self.assertEqual(
    
  2693.             new_state.models["app", "othermodel"].fields["fk"].to_fields, ("renamed",)
    
  2694.         )
    
  2695.         self.assertEqual(
    
  2696.             new_state.models["app", "othermodel"].fields["fo"].from_fields, ("fk",)
    
  2697.         )
    
  2698.         self.assertEqual(
    
  2699.             new_state.models["app", "othermodel"].fields["fo"].to_fields, ("renamed",)
    
  2700.         )
    
  2701.         operation = migrations.RenameField("OtherModel", "fk", "renamed_fk")
    
  2702.         new_state = state.clone()
    
  2703.         operation.state_forwards("app", new_state)
    
  2704.         self.assertEqual(
    
  2705.             new_state.models["app", "othermodel"]
    
  2706.             .fields["renamed_fk"]
    
  2707.             .remote_field.field_name,
    
  2708.             "renamed",
    
  2709.         )
    
  2710.         self.assertEqual(
    
  2711.             new_state.models["app", "othermodel"].fields["renamed_fk"].from_fields,
    
  2712.             ("self",),
    
  2713.         )
    
  2714.         self.assertEqual(
    
  2715.             new_state.models["app", "othermodel"].fields["renamed_fk"].to_fields,
    
  2716.             ("renamed",),
    
  2717.         )
    
  2718.         self.assertEqual(
    
  2719.             new_state.models["app", "othermodel"].fields["fo"].from_fields,
    
  2720.             ("renamed_fk",),
    
  2721.         )
    
  2722.         self.assertEqual(
    
  2723.             new_state.models["app", "othermodel"].fields["fo"].to_fields, ("renamed",)
    
  2724.         )
    
  2725. 
    
  2726.     def test_alter_unique_together(self):
    
  2727.         """
    
  2728.         Tests the AlterUniqueTogether operation.
    
  2729.         """
    
  2730.         project_state = self.set_up_test_model("test_alunto")
    
  2731.         # Test the state alteration
    
  2732.         operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")])
    
  2733.         self.assertEqual(
    
  2734.             operation.describe(), "Alter unique_together for Pony (1 constraint(s))"
    
  2735.         )
    
  2736.         self.assertEqual(
    
  2737.             operation.migration_name_fragment,
    
  2738.             "alter_pony_unique_together",
    
  2739.         )
    
  2740.         new_state = project_state.clone()
    
  2741.         operation.state_forwards("test_alunto", new_state)
    
  2742.         self.assertEqual(
    
  2743.             len(
    
  2744.                 project_state.models["test_alunto", "pony"].options.get(
    
  2745.                     "unique_together", set()
    
  2746.                 )
    
  2747.             ),
    
  2748.             0,
    
  2749.         )
    
  2750.         self.assertEqual(
    
  2751.             len(
    
  2752.                 new_state.models["test_alunto", "pony"].options.get(
    
  2753.                     "unique_together", set()
    
  2754.                 )
    
  2755.             ),
    
  2756.             1,
    
  2757.         )
    
  2758.         # Make sure we can insert duplicate rows
    
  2759.         with connection.cursor() as cursor:
    
  2760.             cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
    
  2761.             cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
    
  2762.             cursor.execute("DELETE FROM test_alunto_pony")
    
  2763.             # Test the database alteration
    
  2764.             with connection.schema_editor() as editor:
    
  2765.                 operation.database_forwards(
    
  2766.                     "test_alunto", editor, project_state, new_state
    
  2767.                 )
    
  2768.             cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
    
  2769.             with self.assertRaises(IntegrityError):
    
  2770.                 with atomic():
    
  2771.                     cursor.execute(
    
  2772.                         "INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)"
    
  2773.                     )
    
  2774.             cursor.execute("DELETE FROM test_alunto_pony")
    
  2775.             # And test reversal
    
  2776.             with connection.schema_editor() as editor:
    
  2777.                 operation.database_backwards(
    
  2778.                     "test_alunto", editor, new_state, project_state
    
  2779.                 )
    
  2780.             cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
    
  2781.             cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
    
  2782.             cursor.execute("DELETE FROM test_alunto_pony")
    
  2783.         # Test flat unique_together
    
  2784.         operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
    
  2785.         operation.state_forwards("test_alunto", new_state)
    
  2786.         self.assertEqual(
    
  2787.             len(
    
  2788.                 new_state.models["test_alunto", "pony"].options.get(
    
  2789.                     "unique_together", set()
    
  2790.                 )
    
  2791.             ),
    
  2792.             1,
    
  2793.         )
    
  2794.         # And deconstruction
    
  2795.         definition = operation.deconstruct()
    
  2796.         self.assertEqual(definition[0], "AlterUniqueTogether")
    
  2797.         self.assertEqual(definition[1], [])
    
  2798.         self.assertEqual(
    
  2799.             definition[2], {"name": "Pony", "unique_together": {("pink", "weight")}}
    
  2800.         )
    
  2801. 
    
  2802.     def test_alter_unique_together_remove(self):
    
  2803.         operation = migrations.AlterUniqueTogether("Pony", None)
    
  2804.         self.assertEqual(
    
  2805.             operation.describe(), "Alter unique_together for Pony (0 constraint(s))"
    
  2806.         )
    
  2807. 
    
  2808.     @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
    
  2809.     def test_remove_unique_together_on_pk_field(self):
    
  2810.         app_label = "test_rutopkf"
    
  2811.         project_state = self.apply_operations(
    
  2812.             app_label,
    
  2813.             ProjectState(),
    
  2814.             operations=[
    
  2815.                 migrations.CreateModel(
    
  2816.                     "Pony",
    
  2817.                     fields=[("id", models.AutoField(primary_key=True))],
    
  2818.                     options={"unique_together": {("id",)}},
    
  2819.                 ),
    
  2820.             ],
    
  2821.         )
    
  2822.         table_name = f"{app_label}_pony"
    
  2823.         pk_constraint_name = f"{table_name}_pkey"
    
  2824.         unique_together_constraint_name = f"{table_name}_id_fb61f881_uniq"
    
  2825.         self.assertConstraintExists(table_name, pk_constraint_name, value=False)
    
  2826.         self.assertConstraintExists(
    
  2827.             table_name, unique_together_constraint_name, value=False
    
  2828.         )
    
  2829. 
    
  2830.         new_state = project_state.clone()
    
  2831.         operation = migrations.AlterUniqueTogether("Pony", set())
    
  2832.         operation.state_forwards(app_label, new_state)
    
  2833.         with connection.schema_editor() as editor:
    
  2834.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  2835.         self.assertConstraintExists(table_name, pk_constraint_name, value=False)
    
  2836.         self.assertConstraintNotExists(table_name, unique_together_constraint_name)
    
  2837. 
    
  2838.     @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
    
  2839.     def test_remove_unique_together_on_unique_field(self):
    
  2840.         app_label = "test_rutouf"
    
  2841.         project_state = self.apply_operations(
    
  2842.             app_label,
    
  2843.             ProjectState(),
    
  2844.             operations=[
    
  2845.                 migrations.CreateModel(
    
  2846.                     "Pony",
    
  2847.                     fields=[
    
  2848.                         ("id", models.AutoField(primary_key=True)),
    
  2849.                         ("name", models.CharField(max_length=30, unique=True)),
    
  2850.                     ],
    
  2851.                     options={"unique_together": {("name",)}},
    
  2852.                 ),
    
  2853.             ],
    
  2854.         )
    
  2855.         table_name = f"{app_label}_pony"
    
  2856.         unique_constraint_name = f"{table_name}_name_key"
    
  2857.         unique_together_constraint_name = f"{table_name}_name_694f3b9f_uniq"
    
  2858.         self.assertConstraintExists(table_name, unique_constraint_name, value=False)
    
  2859.         self.assertConstraintExists(
    
  2860.             table_name, unique_together_constraint_name, value=False
    
  2861.         )
    
  2862. 
    
  2863.         new_state = project_state.clone()
    
  2864.         operation = migrations.AlterUniqueTogether("Pony", set())
    
  2865.         operation.state_forwards(app_label, new_state)
    
  2866.         with connection.schema_editor() as editor:
    
  2867.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  2868.         self.assertConstraintExists(table_name, unique_constraint_name, value=False)
    
  2869.         self.assertConstraintNotExists(table_name, unique_together_constraint_name)
    
  2870. 
    
  2871.     def test_add_index(self):
    
  2872.         """
    
  2873.         Test the AddIndex operation.
    
  2874.         """
    
  2875.         project_state = self.set_up_test_model("test_adin")
    
  2876.         msg = (
    
  2877.             "Indexes passed to AddIndex operations require a name argument. "
    
  2878.             "<Index: fields=['pink']> doesn't have one."
    
  2879.         )
    
  2880.         with self.assertRaisesMessage(ValueError, msg):
    
  2881.             migrations.AddIndex("Pony", models.Index(fields=["pink"]))
    
  2882.         index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
    
  2883.         operation = migrations.AddIndex("Pony", index)
    
  2884.         self.assertEqual(
    
  2885.             operation.describe(),
    
  2886.             "Create index test_adin_pony_pink_idx on field(s) pink of model Pony",
    
  2887.         )
    
  2888.         self.assertEqual(
    
  2889.             operation.migration_name_fragment,
    
  2890.             "pony_test_adin_pony_pink_idx",
    
  2891.         )
    
  2892.         new_state = project_state.clone()
    
  2893.         operation.state_forwards("test_adin", new_state)
    
  2894.         # Test the database alteration
    
  2895.         self.assertEqual(
    
  2896.             len(new_state.models["test_adin", "pony"].options["indexes"]), 1
    
  2897.         )
    
  2898.         self.assertIndexNotExists("test_adin_pony", ["pink"])
    
  2899.         with connection.schema_editor() as editor:
    
  2900.             operation.database_forwards("test_adin", editor, project_state, new_state)
    
  2901.         self.assertIndexExists("test_adin_pony", ["pink"])
    
  2902.         # And test reversal
    
  2903.         with connection.schema_editor() as editor:
    
  2904.             operation.database_backwards("test_adin", editor, new_state, project_state)
    
  2905.         self.assertIndexNotExists("test_adin_pony", ["pink"])
    
  2906.         # And deconstruction
    
  2907.         definition = operation.deconstruct()
    
  2908.         self.assertEqual(definition[0], "AddIndex")
    
  2909.         self.assertEqual(definition[1], [])
    
  2910.         self.assertEqual(definition[2], {"model_name": "Pony", "index": index})
    
  2911. 
    
  2912.     def test_remove_index(self):
    
  2913.         """
    
  2914.         Test the RemoveIndex operation.
    
  2915.         """
    
  2916.         project_state = self.set_up_test_model("test_rmin", multicol_index=True)
    
  2917.         self.assertTableExists("test_rmin_pony")
    
  2918.         self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
    
  2919.         operation = migrations.RemoveIndex("Pony", "pony_test_idx")
    
  2920.         self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
    
  2921.         self.assertEqual(
    
  2922.             operation.migration_name_fragment,
    
  2923.             "remove_pony_pony_test_idx",
    
  2924.         )
    
  2925.         new_state = project_state.clone()
    
  2926.         operation.state_forwards("test_rmin", new_state)
    
  2927.         # Test the state alteration
    
  2928.         self.assertEqual(
    
  2929.             len(new_state.models["test_rmin", "pony"].options["indexes"]), 0
    
  2930.         )
    
  2931.         self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
    
  2932.         # Test the database alteration
    
  2933.         with connection.schema_editor() as editor:
    
  2934.             operation.database_forwards("test_rmin", editor, project_state, new_state)
    
  2935.         self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
    
  2936.         # And test reversal
    
  2937.         with connection.schema_editor() as editor:
    
  2938.             operation.database_backwards("test_rmin", editor, new_state, project_state)
    
  2939.         self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
    
  2940.         # And deconstruction
    
  2941.         definition = operation.deconstruct()
    
  2942.         self.assertEqual(definition[0], "RemoveIndex")
    
  2943.         self.assertEqual(definition[1], [])
    
  2944.         self.assertEqual(definition[2], {"model_name": "Pony", "name": "pony_test_idx"})
    
  2945. 
    
  2946.         # Also test a field dropped with index - sqlite remake issue
    
  2947.         operations = [
    
  2948.             migrations.RemoveIndex("Pony", "pony_test_idx"),
    
  2949.             migrations.RemoveField("Pony", "pink"),
    
  2950.         ]
    
  2951.         self.assertColumnExists("test_rmin_pony", "pink")
    
  2952.         self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
    
  2953.         # Test database alteration
    
  2954.         new_state = project_state.clone()
    
  2955.         self.apply_operations("test_rmin", new_state, operations=operations)
    
  2956.         self.assertColumnNotExists("test_rmin_pony", "pink")
    
  2957.         self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
    
  2958.         # And test reversal
    
  2959.         self.unapply_operations("test_rmin", project_state, operations=operations)
    
  2960.         self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
    
  2961. 
    
  2962.     def test_rename_index(self):
    
  2963.         app_label = "test_rnin"
    
  2964.         project_state = self.set_up_test_model(app_label, index=True)
    
  2965.         table_name = app_label + "_pony"
    
  2966.         self.assertIndexNameExists(table_name, "pony_pink_idx")
    
  2967.         self.assertIndexNameNotExists(table_name, "new_pony_test_idx")
    
  2968.         operation = migrations.RenameIndex(
    
  2969.             "Pony", new_name="new_pony_test_idx", old_name="pony_pink_idx"
    
  2970.         )
    
  2971.         self.assertEqual(
    
  2972.             operation.describe(),
    
  2973.             "Rename index pony_pink_idx on Pony to new_pony_test_idx",
    
  2974.         )
    
  2975.         self.assertEqual(
    
  2976.             operation.migration_name_fragment,
    
  2977.             "rename_pony_pink_idx_new_pony_test_idx",
    
  2978.         )
    
  2979. 
    
  2980.         new_state = project_state.clone()
    
  2981.         operation.state_forwards(app_label, new_state)
    
  2982.         # Rename index.
    
  2983.         expected_queries = 1 if connection.features.can_rename_index else 2
    
  2984.         with connection.schema_editor() as editor, self.assertNumQueries(
    
  2985.             expected_queries
    
  2986.         ):
    
  2987.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  2988.         self.assertIndexNameNotExists(table_name, "pony_pink_idx")
    
  2989.         self.assertIndexNameExists(table_name, "new_pony_test_idx")
    
  2990.         # Reversal.
    
  2991.         with connection.schema_editor() as editor, self.assertNumQueries(
    
  2992.             expected_queries
    
  2993.         ):
    
  2994.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  2995.         self.assertIndexNameExists(table_name, "pony_pink_idx")
    
  2996.         self.assertIndexNameNotExists(table_name, "new_pony_test_idx")
    
  2997.         # Deconstruction.
    
  2998.         definition = operation.deconstruct()
    
  2999.         self.assertEqual(definition[0], "RenameIndex")
    
  3000.         self.assertEqual(definition[1], [])
    
  3001.         self.assertEqual(
    
  3002.             definition[2],
    
  3003.             {
    
  3004.                 "model_name": "Pony",
    
  3005.                 "old_name": "pony_pink_idx",
    
  3006.                 "new_name": "new_pony_test_idx",
    
  3007.             },
    
  3008.         )
    
  3009. 
    
  3010.     def test_rename_index_arguments(self):
    
  3011.         msg = "RenameIndex.old_name and old_fields are mutually exclusive."
    
  3012.         with self.assertRaisesMessage(ValueError, msg):
    
  3013.             migrations.RenameIndex(
    
  3014.                 "Pony",
    
  3015.                 new_name="new_idx_name",
    
  3016.                 old_name="old_idx_name",
    
  3017.                 old_fields=("weight", "pink"),
    
  3018.             )
    
  3019.         msg = "RenameIndex requires one of old_name and old_fields arguments to be set."
    
  3020.         with self.assertRaisesMessage(ValueError, msg):
    
  3021.             migrations.RenameIndex("Pony", new_name="new_idx_name")
    
  3022. 
    
  3023.     def test_rename_index_unnamed_index(self):
    
  3024.         app_label = "test_rninui"
    
  3025.         project_state = self.set_up_test_model(app_label, index_together=True)
    
  3026.         table_name = app_label + "_pony"
    
  3027.         self.assertIndexNameNotExists(table_name, "new_pony_test_idx")
    
  3028.         operation = migrations.RenameIndex(
    
  3029.             "Pony", new_name="new_pony_test_idx", old_fields=("weight", "pink")
    
  3030.         )
    
  3031.         self.assertEqual(
    
  3032.             operation.describe(),
    
  3033.             "Rename unnamed index for ('weight', 'pink') on Pony to new_pony_test_idx",
    
  3034.         )
    
  3035.         self.assertEqual(
    
  3036.             operation.migration_name_fragment,
    
  3037.             "rename_pony_weight_pink_new_pony_test_idx",
    
  3038.         )
    
  3039. 
    
  3040.         new_state = project_state.clone()
    
  3041.         operation.state_forwards(app_label, new_state)
    
  3042.         # Rename index.
    
  3043.         with connection.schema_editor() as editor:
    
  3044.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3045.         self.assertIndexNameExists(table_name, "new_pony_test_idx")
    
  3046.         # Reverse is a no-op.
    
  3047.         with connection.schema_editor() as editor, self.assertNumQueries(0):
    
  3048.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3049.         self.assertIndexNameExists(table_name, "new_pony_test_idx")
    
  3050.         # Reapply, RenameIndex operation is a noop when the old and new name
    
  3051.         # match.
    
  3052.         with connection.schema_editor() as editor:
    
  3053.             operation.database_forwards(app_label, editor, new_state, project_state)
    
  3054.         self.assertIndexNameExists(table_name, "new_pony_test_idx")
    
  3055.         # Deconstruction.
    
  3056.         definition = operation.deconstruct()
    
  3057.         self.assertEqual(definition[0], "RenameIndex")
    
  3058.         self.assertEqual(definition[1], [])
    
  3059.         self.assertEqual(
    
  3060.             definition[2],
    
  3061.             {
    
  3062.                 "model_name": "Pony",
    
  3063.                 "new_name": "new_pony_test_idx",
    
  3064.                 "old_fields": ("weight", "pink"),
    
  3065.             },
    
  3066.         )
    
  3067. 
    
  3068.     def test_rename_index_unknown_unnamed_index(self):
    
  3069.         app_label = "test_rninuui"
    
  3070.         project_state = self.set_up_test_model(app_label)
    
  3071.         operation = migrations.RenameIndex(
    
  3072.             "Pony", new_name="new_pony_test_idx", old_fields=("weight", "pink")
    
  3073.         )
    
  3074.         new_state = project_state.clone()
    
  3075.         operation.state_forwards(app_label, new_state)
    
  3076.         msg = "Found wrong number (0) of indexes for test_rninuui_pony(weight, pink)."
    
  3077.         with connection.schema_editor() as editor:
    
  3078.             with self.assertRaisesMessage(ValueError, msg):
    
  3079.                 operation.database_forwards(app_label, editor, project_state, new_state)
    
  3080. 
    
  3081.     def test_add_index_state_forwards(self):
    
  3082.         project_state = self.set_up_test_model("test_adinsf")
    
  3083.         index = models.Index(fields=["pink"], name="test_adinsf_pony_pink_idx")
    
  3084.         old_model = project_state.apps.get_model("test_adinsf", "Pony")
    
  3085.         new_state = project_state.clone()
    
  3086. 
    
  3087.         operation = migrations.AddIndex("Pony", index)
    
  3088.         operation.state_forwards("test_adinsf", new_state)
    
  3089.         new_model = new_state.apps.get_model("test_adinsf", "Pony")
    
  3090.         self.assertIsNot(old_model, new_model)
    
  3091. 
    
  3092.     def test_remove_index_state_forwards(self):
    
  3093.         project_state = self.set_up_test_model("test_rminsf")
    
  3094.         index = models.Index(fields=["pink"], name="test_rminsf_pony_pink_idx")
    
  3095.         migrations.AddIndex("Pony", index).state_forwards("test_rminsf", project_state)
    
  3096.         old_model = project_state.apps.get_model("test_rminsf", "Pony")
    
  3097.         new_state = project_state.clone()
    
  3098. 
    
  3099.         operation = migrations.RemoveIndex("Pony", "test_rminsf_pony_pink_idx")
    
  3100.         operation.state_forwards("test_rminsf", new_state)
    
  3101.         new_model = new_state.apps.get_model("test_rminsf", "Pony")
    
  3102.         self.assertIsNot(old_model, new_model)
    
  3103. 
    
  3104.     def test_rename_index_state_forwards(self):
    
  3105.         app_label = "test_rnidsf"
    
  3106.         project_state = self.set_up_test_model(app_label, index=True)
    
  3107.         old_model = project_state.apps.get_model(app_label, "Pony")
    
  3108.         new_state = project_state.clone()
    
  3109. 
    
  3110.         operation = migrations.RenameIndex(
    
  3111.             "Pony", new_name="new_pony_pink_idx", old_name="pony_pink_idx"
    
  3112.         )
    
  3113.         operation.state_forwards(app_label, new_state)
    
  3114.         new_model = new_state.apps.get_model(app_label, "Pony")
    
  3115.         self.assertIsNot(old_model, new_model)
    
  3116.         self.assertEqual(new_model._meta.indexes[0].name, "new_pony_pink_idx")
    
  3117. 
    
  3118.     def test_rename_index_state_forwards_unnamed_index(self):
    
  3119.         app_label = "test_rnidsfui"
    
  3120.         project_state = self.set_up_test_model(app_label, index_together=True)
    
  3121.         old_model = project_state.apps.get_model(app_label, "Pony")
    
  3122.         new_state = project_state.clone()
    
  3123. 
    
  3124.         operation = migrations.RenameIndex(
    
  3125.             "Pony", new_name="new_pony_pink_idx", old_fields=("weight", "pink")
    
  3126.         )
    
  3127.         operation.state_forwards(app_label, new_state)
    
  3128.         new_model = new_state.apps.get_model(app_label, "Pony")
    
  3129.         self.assertIsNot(old_model, new_model)
    
  3130.         self.assertEqual(new_model._meta.index_together, tuple())
    
  3131.         self.assertEqual(new_model._meta.indexes[0].name, "new_pony_pink_idx")
    
  3132. 
    
  3133.     @skipUnlessDBFeature("supports_expression_indexes")
    
  3134.     def test_add_func_index(self):
    
  3135.         app_label = "test_addfuncin"
    
  3136.         index_name = f"{app_label}_pony_abs_idx"
    
  3137.         table_name = f"{app_label}_pony"
    
  3138.         project_state = self.set_up_test_model(app_label)
    
  3139.         index = models.Index(Abs("weight"), name=index_name)
    
  3140.         operation = migrations.AddIndex("Pony", index)
    
  3141.         self.assertEqual(
    
  3142.             operation.describe(),
    
  3143.             "Create index test_addfuncin_pony_abs_idx on Abs(F(weight)) on model Pony",
    
  3144.         )
    
  3145.         self.assertEqual(
    
  3146.             operation.migration_name_fragment,
    
  3147.             "pony_test_addfuncin_pony_abs_idx",
    
  3148.         )
    
  3149.         new_state = project_state.clone()
    
  3150.         operation.state_forwards(app_label, new_state)
    
  3151.         self.assertEqual(len(new_state.models[app_label, "pony"].options["indexes"]), 1)
    
  3152.         self.assertIndexNameNotExists(table_name, index_name)
    
  3153.         # Add index.
    
  3154.         with connection.schema_editor() as editor:
    
  3155.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3156.         self.assertIndexNameExists(table_name, index_name)
    
  3157.         # Reversal.
    
  3158.         with connection.schema_editor() as editor:
    
  3159.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3160.         self.assertIndexNameNotExists(table_name, index_name)
    
  3161.         # Deconstruction.
    
  3162.         definition = operation.deconstruct()
    
  3163.         self.assertEqual(definition[0], "AddIndex")
    
  3164.         self.assertEqual(definition[1], [])
    
  3165.         self.assertEqual(definition[2], {"model_name": "Pony", "index": index})
    
  3166. 
    
  3167.     @skipUnlessDBFeature("supports_expression_indexes")
    
  3168.     def test_remove_func_index(self):
    
  3169.         app_label = "test_rmfuncin"
    
  3170.         index_name = f"{app_label}_pony_abs_idx"
    
  3171.         table_name = f"{app_label}_pony"
    
  3172.         project_state = self.set_up_test_model(
    
  3173.             app_label,
    
  3174.             indexes=[
    
  3175.                 models.Index(Abs("weight"), name=index_name),
    
  3176.             ],
    
  3177.         )
    
  3178.         self.assertTableExists(table_name)
    
  3179.         self.assertIndexNameExists(table_name, index_name)
    
  3180.         operation = migrations.RemoveIndex("Pony", index_name)
    
  3181.         self.assertEqual(
    
  3182.             operation.describe(),
    
  3183.             "Remove index test_rmfuncin_pony_abs_idx from Pony",
    
  3184.         )
    
  3185.         self.assertEqual(
    
  3186.             operation.migration_name_fragment,
    
  3187.             "remove_pony_test_rmfuncin_pony_abs_idx",
    
  3188.         )
    
  3189.         new_state = project_state.clone()
    
  3190.         operation.state_forwards(app_label, new_state)
    
  3191.         self.assertEqual(len(new_state.models[app_label, "pony"].options["indexes"]), 0)
    
  3192.         # Remove index.
    
  3193.         with connection.schema_editor() as editor:
    
  3194.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3195.         self.assertIndexNameNotExists(table_name, index_name)
    
  3196.         # Reversal.
    
  3197.         with connection.schema_editor() as editor:
    
  3198.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3199.         self.assertIndexNameExists(table_name, index_name)
    
  3200.         # Deconstruction.
    
  3201.         definition = operation.deconstruct()
    
  3202.         self.assertEqual(definition[0], "RemoveIndex")
    
  3203.         self.assertEqual(definition[1], [])
    
  3204.         self.assertEqual(definition[2], {"model_name": "Pony", "name": index_name})
    
  3205. 
    
  3206.     @skipUnlessDBFeature("supports_expression_indexes")
    
  3207.     def test_alter_field_with_func_index(self):
    
  3208.         app_label = "test_alfuncin"
    
  3209.         index_name = f"{app_label}_pony_idx"
    
  3210.         table_name = f"{app_label}_pony"
    
  3211.         project_state = self.set_up_test_model(
    
  3212.             app_label,
    
  3213.             indexes=[models.Index(Abs("pink"), name=index_name)],
    
  3214.         )
    
  3215.         operation = migrations.AlterField(
    
  3216.             "Pony", "pink", models.IntegerField(null=True)
    
  3217.         )
    
  3218.         new_state = project_state.clone()
    
  3219.         operation.state_forwards(app_label, new_state)
    
  3220.         with connection.schema_editor() as editor:
    
  3221.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3222.         self.assertIndexNameExists(table_name, index_name)
    
  3223.         with connection.schema_editor() as editor:
    
  3224.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3225.         self.assertIndexNameExists(table_name, index_name)
    
  3226. 
    
  3227.     def test_alter_field_with_index(self):
    
  3228.         """
    
  3229.         Test AlterField operation with an index to ensure indexes created via
    
  3230.         Meta.indexes don't get dropped with sqlite3 remake.
    
  3231.         """
    
  3232.         project_state = self.set_up_test_model("test_alflin", index=True)
    
  3233.         operation = migrations.AlterField(
    
  3234.             "Pony", "pink", models.IntegerField(null=True)
    
  3235.         )
    
  3236.         new_state = project_state.clone()
    
  3237.         operation.state_forwards("test_alflin", new_state)
    
  3238.         # Test the database alteration
    
  3239.         self.assertColumnNotNull("test_alflin_pony", "pink")
    
  3240.         with connection.schema_editor() as editor:
    
  3241.             operation.database_forwards("test_alflin", editor, project_state, new_state)
    
  3242.         # Index hasn't been dropped
    
  3243.         self.assertIndexExists("test_alflin_pony", ["pink"])
    
  3244.         # And test reversal
    
  3245.         with connection.schema_editor() as editor:
    
  3246.             operation.database_backwards(
    
  3247.                 "test_alflin", editor, new_state, project_state
    
  3248.             )
    
  3249.         # Ensure the index is still there
    
  3250.         self.assertIndexExists("test_alflin_pony", ["pink"])
    
  3251. 
    
  3252.     def test_alter_index_together(self):
    
  3253.         """
    
  3254.         Tests the AlterIndexTogether operation.
    
  3255.         """
    
  3256.         project_state = self.set_up_test_model("test_alinto")
    
  3257.         # Test the state alteration
    
  3258.         operation = migrations.AlterIndexTogether("Pony", [("pink", "weight")])
    
  3259.         self.assertEqual(
    
  3260.             operation.describe(), "Alter index_together for Pony (1 constraint(s))"
    
  3261.         )
    
  3262.         self.assertEqual(
    
  3263.             operation.migration_name_fragment,
    
  3264.             "alter_pony_index_together",
    
  3265.         )
    
  3266.         new_state = project_state.clone()
    
  3267.         operation.state_forwards("test_alinto", new_state)
    
  3268.         self.assertEqual(
    
  3269.             len(
    
  3270.                 project_state.models["test_alinto", "pony"].options.get(
    
  3271.                     "index_together", set()
    
  3272.                 )
    
  3273.             ),
    
  3274.             0,
    
  3275.         )
    
  3276.         self.assertEqual(
    
  3277.             len(
    
  3278.                 new_state.models["test_alinto", "pony"].options.get(
    
  3279.                     "index_together", set()
    
  3280.                 )
    
  3281.             ),
    
  3282.             1,
    
  3283.         )
    
  3284.         # Make sure there's no matching index
    
  3285.         self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
    
  3286.         # Test the database alteration
    
  3287.         with connection.schema_editor() as editor:
    
  3288.             operation.database_forwards("test_alinto", editor, project_state, new_state)
    
  3289.         self.assertIndexExists("test_alinto_pony", ["pink", "weight"])
    
  3290.         # And test reversal
    
  3291.         with connection.schema_editor() as editor:
    
  3292.             operation.database_backwards(
    
  3293.                 "test_alinto", editor, new_state, project_state
    
  3294.             )
    
  3295.         self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
    
  3296.         # And deconstruction
    
  3297.         definition = operation.deconstruct()
    
  3298.         self.assertEqual(definition[0], "AlterIndexTogether")
    
  3299.         self.assertEqual(definition[1], [])
    
  3300.         self.assertEqual(
    
  3301.             definition[2], {"name": "Pony", "index_together": {("pink", "weight")}}
    
  3302.         )
    
  3303. 
    
  3304.     def test_alter_index_together_remove(self):
    
  3305.         operation = migrations.AlterIndexTogether("Pony", None)
    
  3306.         self.assertEqual(
    
  3307.             operation.describe(), "Alter index_together for Pony (0 constraint(s))"
    
  3308.         )
    
  3309. 
    
  3310.     @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
    
  3311.     def test_alter_index_together_remove_with_unique_together(self):
    
  3312.         app_label = "test_alintoremove_wunto"
    
  3313.         table_name = "%s_pony" % app_label
    
  3314.         project_state = self.set_up_test_model(app_label, unique_together=True)
    
  3315.         self.assertUniqueConstraintExists(table_name, ["pink", "weight"])
    
  3316.         # Add index together.
    
  3317.         new_state = project_state.clone()
    
  3318.         operation = migrations.AlterIndexTogether("Pony", [("pink", "weight")])
    
  3319.         operation.state_forwards(app_label, new_state)
    
  3320.         with connection.schema_editor() as editor:
    
  3321.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3322.         self.assertIndexExists(table_name, ["pink", "weight"])
    
  3323.         # Remove index together.
    
  3324.         project_state = new_state
    
  3325.         new_state = project_state.clone()
    
  3326.         operation = migrations.AlterIndexTogether("Pony", set())
    
  3327.         operation.state_forwards(app_label, new_state)
    
  3328.         with connection.schema_editor() as editor:
    
  3329.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3330.         self.assertIndexNotExists(table_name, ["pink", "weight"])
    
  3331.         self.assertUniqueConstraintExists(table_name, ["pink", "weight"])
    
  3332. 
    
  3333.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  3334.     def test_add_constraint(self):
    
  3335.         project_state = self.set_up_test_model("test_addconstraint")
    
  3336.         gt_check = models.Q(pink__gt=2)
    
  3337.         gt_constraint = models.CheckConstraint(
    
  3338.             check=gt_check, name="test_add_constraint_pony_pink_gt_2"
    
  3339.         )
    
  3340.         gt_operation = migrations.AddConstraint("Pony", gt_constraint)
    
  3341.         self.assertEqual(
    
  3342.             gt_operation.describe(),
    
  3343.             "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony",
    
  3344.         )
    
  3345.         self.assertEqual(
    
  3346.             gt_operation.migration_name_fragment,
    
  3347.             "pony_test_add_constraint_pony_pink_gt_2",
    
  3348.         )
    
  3349.         # Test the state alteration
    
  3350.         new_state = project_state.clone()
    
  3351.         gt_operation.state_forwards("test_addconstraint", new_state)
    
  3352.         self.assertEqual(
    
  3353.             len(new_state.models["test_addconstraint", "pony"].options["constraints"]),
    
  3354.             1,
    
  3355.         )
    
  3356.         Pony = new_state.apps.get_model("test_addconstraint", "Pony")
    
  3357.         self.assertEqual(len(Pony._meta.constraints), 1)
    
  3358.         # Test the database alteration
    
  3359.         with connection.schema_editor() as editor:
    
  3360.             gt_operation.database_forwards(
    
  3361.                 "test_addconstraint", editor, project_state, new_state
    
  3362.             )
    
  3363.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3364.             Pony.objects.create(pink=1, weight=1.0)
    
  3365.         # Add another one.
    
  3366.         lt_check = models.Q(pink__lt=100)
    
  3367.         lt_constraint = models.CheckConstraint(
    
  3368.             check=lt_check, name="test_add_constraint_pony_pink_lt_100"
    
  3369.         )
    
  3370.         lt_operation = migrations.AddConstraint("Pony", lt_constraint)
    
  3371.         lt_operation.state_forwards("test_addconstraint", new_state)
    
  3372.         self.assertEqual(
    
  3373.             len(new_state.models["test_addconstraint", "pony"].options["constraints"]),
    
  3374.             2,
    
  3375.         )
    
  3376.         Pony = new_state.apps.get_model("test_addconstraint", "Pony")
    
  3377.         self.assertEqual(len(Pony._meta.constraints), 2)
    
  3378.         with connection.schema_editor() as editor:
    
  3379.             lt_operation.database_forwards(
    
  3380.                 "test_addconstraint", editor, project_state, new_state
    
  3381.             )
    
  3382.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3383.             Pony.objects.create(pink=100, weight=1.0)
    
  3384.         # Test reversal
    
  3385.         with connection.schema_editor() as editor:
    
  3386.             gt_operation.database_backwards(
    
  3387.                 "test_addconstraint", editor, new_state, project_state
    
  3388.             )
    
  3389.         Pony.objects.create(pink=1, weight=1.0)
    
  3390.         # Test deconstruction
    
  3391.         definition = gt_operation.deconstruct()
    
  3392.         self.assertEqual(definition[0], "AddConstraint")
    
  3393.         self.assertEqual(definition[1], [])
    
  3394.         self.assertEqual(
    
  3395.             definition[2], {"model_name": "Pony", "constraint": gt_constraint}
    
  3396.         )
    
  3397. 
    
  3398.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  3399.     def test_add_constraint_percent_escaping(self):
    
  3400.         app_label = "add_constraint_string_quoting"
    
  3401.         operations = [
    
  3402.             migrations.CreateModel(
    
  3403.                 "Author",
    
  3404.                 fields=[
    
  3405.                     ("id", models.AutoField(primary_key=True)),
    
  3406.                     ("name", models.CharField(max_length=100)),
    
  3407.                     ("surname", models.CharField(max_length=100, default="")),
    
  3408.                     ("rebate", models.CharField(max_length=100)),
    
  3409.                 ],
    
  3410.             ),
    
  3411.         ]
    
  3412.         from_state = self.apply_operations(app_label, ProjectState(), operations)
    
  3413.         # "%" generated in startswith lookup should be escaped in a way that is
    
  3414.         # considered a leading wildcard.
    
  3415.         check = models.Q(name__startswith="Albert")
    
  3416.         constraint = models.CheckConstraint(check=check, name="name_constraint")
    
  3417.         operation = migrations.AddConstraint("Author", constraint)
    
  3418.         to_state = from_state.clone()
    
  3419.         operation.state_forwards(app_label, to_state)
    
  3420.         with connection.schema_editor() as editor:
    
  3421.             operation.database_forwards(app_label, editor, from_state, to_state)
    
  3422.         Author = to_state.apps.get_model(app_label, "Author")
    
  3423.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3424.             Author.objects.create(name="Artur")
    
  3425.         # Literal "%" should be escaped in a way that is not a considered a
    
  3426.         # wildcard.
    
  3427.         check = models.Q(rebate__endswith="%")
    
  3428.         constraint = models.CheckConstraint(check=check, name="rebate_constraint")
    
  3429.         operation = migrations.AddConstraint("Author", constraint)
    
  3430.         from_state = to_state
    
  3431.         to_state = from_state.clone()
    
  3432.         operation.state_forwards(app_label, to_state)
    
  3433.         Author = to_state.apps.get_model(app_label, "Author")
    
  3434.         with connection.schema_editor() as editor:
    
  3435.             operation.database_forwards(app_label, editor, from_state, to_state)
    
  3436.         Author = to_state.apps.get_model(app_label, "Author")
    
  3437.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3438.             Author.objects.create(name="Albert", rebate="10$")
    
  3439.         author = Author.objects.create(name="Albert", rebate="10%")
    
  3440.         self.assertEqual(Author.objects.get(), author)
    
  3441.         # Right-hand-side baked "%" literals should not be used for parameters
    
  3442.         # interpolation.
    
  3443.         check = ~models.Q(surname__startswith=models.F("name"))
    
  3444.         constraint = models.CheckConstraint(check=check, name="name_constraint_rhs")
    
  3445.         operation = migrations.AddConstraint("Author", constraint)
    
  3446.         from_state = to_state
    
  3447.         to_state = from_state.clone()
    
  3448.         operation.state_forwards(app_label, to_state)
    
  3449.         with connection.schema_editor() as editor:
    
  3450.             operation.database_forwards(app_label, editor, from_state, to_state)
    
  3451.         Author = to_state.apps.get_model(app_label, "Author")
    
  3452.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3453.             Author.objects.create(name="Albert", surname="Alberto")
    
  3454. 
    
  3455.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  3456.     def test_add_or_constraint(self):
    
  3457.         app_label = "test_addorconstraint"
    
  3458.         constraint_name = "add_constraint_or"
    
  3459.         from_state = self.set_up_test_model(app_label)
    
  3460.         check = models.Q(pink__gt=2, weight__gt=2) | models.Q(weight__lt=0)
    
  3461.         constraint = models.CheckConstraint(check=check, name=constraint_name)
    
  3462.         operation = migrations.AddConstraint("Pony", constraint)
    
  3463.         to_state = from_state.clone()
    
  3464.         operation.state_forwards(app_label, to_state)
    
  3465.         with connection.schema_editor() as editor:
    
  3466.             operation.database_forwards(app_label, editor, from_state, to_state)
    
  3467.         Pony = to_state.apps.get_model(app_label, "Pony")
    
  3468.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3469.             Pony.objects.create(pink=2, weight=3.0)
    
  3470.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3471.             Pony.objects.create(pink=3, weight=1.0)
    
  3472.         Pony.objects.bulk_create(
    
  3473.             [
    
  3474.                 Pony(pink=3, weight=-1.0),
    
  3475.                 Pony(pink=1, weight=-1.0),
    
  3476.                 Pony(pink=3, weight=3.0),
    
  3477.             ]
    
  3478.         )
    
  3479. 
    
  3480.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  3481.     def test_add_constraint_combinable(self):
    
  3482.         app_label = "test_addconstraint_combinable"
    
  3483.         operations = [
    
  3484.             migrations.CreateModel(
    
  3485.                 "Book",
    
  3486.                 fields=[
    
  3487.                     ("id", models.AutoField(primary_key=True)),
    
  3488.                     ("read", models.PositiveIntegerField()),
    
  3489.                     ("unread", models.PositiveIntegerField()),
    
  3490.                 ],
    
  3491.             ),
    
  3492.         ]
    
  3493.         from_state = self.apply_operations(app_label, ProjectState(), operations)
    
  3494.         constraint = models.CheckConstraint(
    
  3495.             check=models.Q(read=(100 - models.F("unread"))),
    
  3496.             name="test_addconstraint_combinable_sum_100",
    
  3497.         )
    
  3498.         operation = migrations.AddConstraint("Book", constraint)
    
  3499.         to_state = from_state.clone()
    
  3500.         operation.state_forwards(app_label, to_state)
    
  3501.         with connection.schema_editor() as editor:
    
  3502.             operation.database_forwards(app_label, editor, from_state, to_state)
    
  3503.         Book = to_state.apps.get_model(app_label, "Book")
    
  3504.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3505.             Book.objects.create(read=70, unread=10)
    
  3506.         Book.objects.create(read=70, unread=30)
    
  3507. 
    
  3508.     @skipUnlessDBFeature("supports_table_check_constraints")
    
  3509.     def test_remove_constraint(self):
    
  3510.         project_state = self.set_up_test_model(
    
  3511.             "test_removeconstraint",
    
  3512.             constraints=[
    
  3513.                 models.CheckConstraint(
    
  3514.                     check=models.Q(pink__gt=2),
    
  3515.                     name="test_remove_constraint_pony_pink_gt_2",
    
  3516.                 ),
    
  3517.                 models.CheckConstraint(
    
  3518.                     check=models.Q(pink__lt=100),
    
  3519.                     name="test_remove_constraint_pony_pink_lt_100",
    
  3520.                 ),
    
  3521.             ],
    
  3522.         )
    
  3523.         gt_operation = migrations.RemoveConstraint(
    
  3524.             "Pony", "test_remove_constraint_pony_pink_gt_2"
    
  3525.         )
    
  3526.         self.assertEqual(
    
  3527.             gt_operation.describe(),
    
  3528.             "Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony",
    
  3529.         )
    
  3530.         self.assertEqual(
    
  3531.             gt_operation.migration_name_fragment,
    
  3532.             "remove_pony_test_remove_constraint_pony_pink_gt_2",
    
  3533.         )
    
  3534.         # Test state alteration
    
  3535.         new_state = project_state.clone()
    
  3536.         gt_operation.state_forwards("test_removeconstraint", new_state)
    
  3537.         self.assertEqual(
    
  3538.             len(
    
  3539.                 new_state.models["test_removeconstraint", "pony"].options["constraints"]
    
  3540.             ),
    
  3541.             1,
    
  3542.         )
    
  3543.         Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
    
  3544.         self.assertEqual(len(Pony._meta.constraints), 1)
    
  3545.         # Test database alteration
    
  3546.         with connection.schema_editor() as editor:
    
  3547.             gt_operation.database_forwards(
    
  3548.                 "test_removeconstraint", editor, project_state, new_state
    
  3549.             )
    
  3550.         Pony.objects.create(pink=1, weight=1.0).delete()
    
  3551.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3552.             Pony.objects.create(pink=100, weight=1.0)
    
  3553.         # Remove the other one.
    
  3554.         lt_operation = migrations.RemoveConstraint(
    
  3555.             "Pony", "test_remove_constraint_pony_pink_lt_100"
    
  3556.         )
    
  3557.         lt_operation.state_forwards("test_removeconstraint", new_state)
    
  3558.         self.assertEqual(
    
  3559.             len(
    
  3560.                 new_state.models["test_removeconstraint", "pony"].options["constraints"]
    
  3561.             ),
    
  3562.             0,
    
  3563.         )
    
  3564.         Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
    
  3565.         self.assertEqual(len(Pony._meta.constraints), 0)
    
  3566.         with connection.schema_editor() as editor:
    
  3567.             lt_operation.database_forwards(
    
  3568.                 "test_removeconstraint", editor, project_state, new_state
    
  3569.             )
    
  3570.         Pony.objects.create(pink=100, weight=1.0).delete()
    
  3571.         # Test reversal
    
  3572.         with connection.schema_editor() as editor:
    
  3573.             gt_operation.database_backwards(
    
  3574.                 "test_removeconstraint", editor, new_state, project_state
    
  3575.             )
    
  3576.         with self.assertRaises(IntegrityError), transaction.atomic():
    
  3577.             Pony.objects.create(pink=1, weight=1.0)
    
  3578.         # Test deconstruction
    
  3579.         definition = gt_operation.deconstruct()
    
  3580.         self.assertEqual(definition[0], "RemoveConstraint")
    
  3581.         self.assertEqual(definition[1], [])
    
  3582.         self.assertEqual(
    
  3583.             definition[2],
    
  3584.             {"model_name": "Pony", "name": "test_remove_constraint_pony_pink_gt_2"},
    
  3585.         )
    
  3586. 
    
  3587.     def test_add_partial_unique_constraint(self):
    
  3588.         project_state = self.set_up_test_model("test_addpartialuniqueconstraint")
    
  3589.         partial_unique_constraint = models.UniqueConstraint(
    
  3590.             fields=["pink"],
    
  3591.             condition=models.Q(weight__gt=5),
    
  3592.             name="test_constraint_pony_pink_for_weight_gt_5_uniq",
    
  3593.         )
    
  3594.         operation = migrations.AddConstraint("Pony", partial_unique_constraint)
    
  3595.         self.assertEqual(
    
  3596.             operation.describe(),
    
  3597.             "Create constraint test_constraint_pony_pink_for_weight_gt_5_uniq "
    
  3598.             "on model Pony",
    
  3599.         )
    
  3600.         # Test the state alteration
    
  3601.         new_state = project_state.clone()
    
  3602.         operation.state_forwards("test_addpartialuniqueconstraint", new_state)
    
  3603.         self.assertEqual(
    
  3604.             len(
    
  3605.                 new_state.models["test_addpartialuniqueconstraint", "pony"].options[
    
  3606.                     "constraints"
    
  3607.                 ]
    
  3608.             ),
    
  3609.             1,
    
  3610.         )
    
  3611.         Pony = new_state.apps.get_model("test_addpartialuniqueconstraint", "Pony")
    
  3612.         self.assertEqual(len(Pony._meta.constraints), 1)
    
  3613.         # Test the database alteration
    
  3614.         with connection.schema_editor() as editor:
    
  3615.             operation.database_forwards(
    
  3616.                 "test_addpartialuniqueconstraint", editor, project_state, new_state
    
  3617.             )
    
  3618.         # Test constraint works
    
  3619.         Pony.objects.create(pink=1, weight=4.0)
    
  3620.         Pony.objects.create(pink=1, weight=4.0)
    
  3621.         Pony.objects.create(pink=1, weight=6.0)
    
  3622.         if connection.features.supports_partial_indexes:
    
  3623.             with self.assertRaises(IntegrityError), transaction.atomic():
    
  3624.                 Pony.objects.create(pink=1, weight=7.0)
    
  3625.         else:
    
  3626.             Pony.objects.create(pink=1, weight=7.0)
    
  3627.         # Test reversal
    
  3628.         with connection.schema_editor() as editor:
    
  3629.             operation.database_backwards(
    
  3630.                 "test_addpartialuniqueconstraint", editor, new_state, project_state
    
  3631.             )
    
  3632.         # Test constraint doesn't work
    
  3633.         Pony.objects.create(pink=1, weight=7.0)
    
  3634.         # Test deconstruction
    
  3635.         definition = operation.deconstruct()
    
  3636.         self.assertEqual(definition[0], "AddConstraint")
    
  3637.         self.assertEqual(definition[1], [])
    
  3638.         self.assertEqual(
    
  3639.             definition[2],
    
  3640.             {"model_name": "Pony", "constraint": partial_unique_constraint},
    
  3641.         )
    
  3642. 
    
  3643.     def test_remove_partial_unique_constraint(self):
    
  3644.         project_state = self.set_up_test_model(
    
  3645.             "test_removepartialuniqueconstraint",
    
  3646.             constraints=[
    
  3647.                 models.UniqueConstraint(
    
  3648.                     fields=["pink"],
    
  3649.                     condition=models.Q(weight__gt=5),
    
  3650.                     name="test_constraint_pony_pink_for_weight_gt_5_uniq",
    
  3651.                 ),
    
  3652.             ],
    
  3653.         )
    
  3654.         gt_operation = migrations.RemoveConstraint(
    
  3655.             "Pony", "test_constraint_pony_pink_for_weight_gt_5_uniq"
    
  3656.         )
    
  3657.         self.assertEqual(
    
  3658.             gt_operation.describe(),
    
  3659.             "Remove constraint test_constraint_pony_pink_for_weight_gt_5_uniq from "
    
  3660.             "model Pony",
    
  3661.         )
    
  3662.         # Test state alteration
    
  3663.         new_state = project_state.clone()
    
  3664.         gt_operation.state_forwards("test_removepartialuniqueconstraint", new_state)
    
  3665.         self.assertEqual(
    
  3666.             len(
    
  3667.                 new_state.models["test_removepartialuniqueconstraint", "pony"].options[
    
  3668.                     "constraints"
    
  3669.                 ]
    
  3670.             ),
    
  3671.             0,
    
  3672.         )
    
  3673.         Pony = new_state.apps.get_model("test_removepartialuniqueconstraint", "Pony")
    
  3674.         self.assertEqual(len(Pony._meta.constraints), 0)
    
  3675.         # Test database alteration
    
  3676.         with connection.schema_editor() as editor:
    
  3677.             gt_operation.database_forwards(
    
  3678.                 "test_removepartialuniqueconstraint", editor, project_state, new_state
    
  3679.             )
    
  3680.         # Test constraint doesn't work
    
  3681.         Pony.objects.create(pink=1, weight=4.0)
    
  3682.         Pony.objects.create(pink=1, weight=4.0)
    
  3683.         Pony.objects.create(pink=1, weight=6.0)
    
  3684.         Pony.objects.create(pink=1, weight=7.0).delete()
    
  3685.         # Test reversal
    
  3686.         with connection.schema_editor() as editor:
    
  3687.             gt_operation.database_backwards(
    
  3688.                 "test_removepartialuniqueconstraint", editor, new_state, project_state
    
  3689.             )
    
  3690.         # Test constraint works
    
  3691.         if connection.features.supports_partial_indexes:
    
  3692.             with self.assertRaises(IntegrityError), transaction.atomic():
    
  3693.                 Pony.objects.create(pink=1, weight=7.0)
    
  3694.         else:
    
  3695.             Pony.objects.create(pink=1, weight=7.0)
    
  3696.         # Test deconstruction
    
  3697.         definition = gt_operation.deconstruct()
    
  3698.         self.assertEqual(definition[0], "RemoveConstraint")
    
  3699.         self.assertEqual(definition[1], [])
    
  3700.         self.assertEqual(
    
  3701.             definition[2],
    
  3702.             {
    
  3703.                 "model_name": "Pony",
    
  3704.                 "name": "test_constraint_pony_pink_for_weight_gt_5_uniq",
    
  3705.             },
    
  3706.         )
    
  3707. 
    
  3708.     def test_add_deferred_unique_constraint(self):
    
  3709.         app_label = "test_adddeferred_uc"
    
  3710.         project_state = self.set_up_test_model(app_label)
    
  3711.         deferred_unique_constraint = models.UniqueConstraint(
    
  3712.             fields=["pink"],
    
  3713.             name="deferred_pink_constraint_add",
    
  3714.             deferrable=models.Deferrable.DEFERRED,
    
  3715.         )
    
  3716.         operation = migrations.AddConstraint("Pony", deferred_unique_constraint)
    
  3717.         self.assertEqual(
    
  3718.             operation.describe(),
    
  3719.             "Create constraint deferred_pink_constraint_add on model Pony",
    
  3720.         )
    
  3721.         # Add constraint.
    
  3722.         new_state = project_state.clone()
    
  3723.         operation.state_forwards(app_label, new_state)
    
  3724.         self.assertEqual(
    
  3725.             len(new_state.models[app_label, "pony"].options["constraints"]), 1
    
  3726.         )
    
  3727.         Pony = new_state.apps.get_model(app_label, "Pony")
    
  3728.         self.assertEqual(len(Pony._meta.constraints), 1)
    
  3729.         with connection.schema_editor() as editor, CaptureQueriesContext(
    
  3730.             connection
    
  3731.         ) as ctx:
    
  3732.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3733.         Pony.objects.create(pink=1, weight=4.0)
    
  3734.         if connection.features.supports_deferrable_unique_constraints:
    
  3735.             # Unique constraint is deferred.
    
  3736.             with transaction.atomic():
    
  3737.                 obj = Pony.objects.create(pink=1, weight=4.0)
    
  3738.                 obj.pink = 2
    
  3739.                 obj.save()
    
  3740.             # Constraint behavior can be changed with SET CONSTRAINTS.
    
  3741.             with self.assertRaises(IntegrityError):
    
  3742.                 with transaction.atomic(), connection.cursor() as cursor:
    
  3743.                     quoted_name = connection.ops.quote_name(
    
  3744.                         deferred_unique_constraint.name
    
  3745.                     )
    
  3746.                     cursor.execute("SET CONSTRAINTS %s IMMEDIATE" % quoted_name)
    
  3747.                     obj = Pony.objects.create(pink=1, weight=4.0)
    
  3748.                     obj.pink = 3
    
  3749.                     obj.save()
    
  3750.         else:
    
  3751.             self.assertEqual(len(ctx), 0)
    
  3752.             Pony.objects.create(pink=1, weight=4.0)
    
  3753.         # Reversal.
    
  3754.         with connection.schema_editor() as editor:
    
  3755.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3756.         # Constraint doesn't work.
    
  3757.         Pony.objects.create(pink=1, weight=4.0)
    
  3758.         # Deconstruction.
    
  3759.         definition = operation.deconstruct()
    
  3760.         self.assertEqual(definition[0], "AddConstraint")
    
  3761.         self.assertEqual(definition[1], [])
    
  3762.         self.assertEqual(
    
  3763.             definition[2],
    
  3764.             {"model_name": "Pony", "constraint": deferred_unique_constraint},
    
  3765.         )
    
  3766. 
    
  3767.     def test_remove_deferred_unique_constraint(self):
    
  3768.         app_label = "test_removedeferred_uc"
    
  3769.         deferred_unique_constraint = models.UniqueConstraint(
    
  3770.             fields=["pink"],
    
  3771.             name="deferred_pink_constraint_rm",
    
  3772.             deferrable=models.Deferrable.DEFERRED,
    
  3773.         )
    
  3774.         project_state = self.set_up_test_model(
    
  3775.             app_label, constraints=[deferred_unique_constraint]
    
  3776.         )
    
  3777.         operation = migrations.RemoveConstraint("Pony", deferred_unique_constraint.name)
    
  3778.         self.assertEqual(
    
  3779.             operation.describe(),
    
  3780.             "Remove constraint deferred_pink_constraint_rm from model Pony",
    
  3781.         )
    
  3782.         # Remove constraint.
    
  3783.         new_state = project_state.clone()
    
  3784.         operation.state_forwards(app_label, new_state)
    
  3785.         self.assertEqual(
    
  3786.             len(new_state.models[app_label, "pony"].options["constraints"]), 0
    
  3787.         )
    
  3788.         Pony = new_state.apps.get_model(app_label, "Pony")
    
  3789.         self.assertEqual(len(Pony._meta.constraints), 0)
    
  3790.         with connection.schema_editor() as editor, CaptureQueriesContext(
    
  3791.             connection
    
  3792.         ) as ctx:
    
  3793.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3794.         # Constraint doesn't work.
    
  3795.         Pony.objects.create(pink=1, weight=4.0)
    
  3796.         Pony.objects.create(pink=1, weight=4.0).delete()
    
  3797.         if not connection.features.supports_deferrable_unique_constraints:
    
  3798.             self.assertEqual(len(ctx), 0)
    
  3799.         # Reversal.
    
  3800.         with connection.schema_editor() as editor:
    
  3801.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3802.         if connection.features.supports_deferrable_unique_constraints:
    
  3803.             # Unique constraint is deferred.
    
  3804.             with transaction.atomic():
    
  3805.                 obj = Pony.objects.create(pink=1, weight=4.0)
    
  3806.                 obj.pink = 2
    
  3807.                 obj.save()
    
  3808.             # Constraint behavior can be changed with SET CONSTRAINTS.
    
  3809.             with self.assertRaises(IntegrityError):
    
  3810.                 with transaction.atomic(), connection.cursor() as cursor:
    
  3811.                     quoted_name = connection.ops.quote_name(
    
  3812.                         deferred_unique_constraint.name
    
  3813.                     )
    
  3814.                     cursor.execute("SET CONSTRAINTS %s IMMEDIATE" % quoted_name)
    
  3815.                     obj = Pony.objects.create(pink=1, weight=4.0)
    
  3816.                     obj.pink = 3
    
  3817.                     obj.save()
    
  3818.         else:
    
  3819.             Pony.objects.create(pink=1, weight=4.0)
    
  3820.         # Deconstruction.
    
  3821.         definition = operation.deconstruct()
    
  3822.         self.assertEqual(definition[0], "RemoveConstraint")
    
  3823.         self.assertEqual(definition[1], [])
    
  3824.         self.assertEqual(
    
  3825.             definition[2],
    
  3826.             {
    
  3827.                 "model_name": "Pony",
    
  3828.                 "name": "deferred_pink_constraint_rm",
    
  3829.             },
    
  3830.         )
    
  3831. 
    
  3832.     def test_add_covering_unique_constraint(self):
    
  3833.         app_label = "test_addcovering_uc"
    
  3834.         project_state = self.set_up_test_model(app_label)
    
  3835.         covering_unique_constraint = models.UniqueConstraint(
    
  3836.             fields=["pink"],
    
  3837.             name="covering_pink_constraint_add",
    
  3838.             include=["weight"],
    
  3839.         )
    
  3840.         operation = migrations.AddConstraint("Pony", covering_unique_constraint)
    
  3841.         self.assertEqual(
    
  3842.             operation.describe(),
    
  3843.             "Create constraint covering_pink_constraint_add on model Pony",
    
  3844.         )
    
  3845.         # Add constraint.
    
  3846.         new_state = project_state.clone()
    
  3847.         operation.state_forwards(app_label, new_state)
    
  3848.         self.assertEqual(
    
  3849.             len(new_state.models[app_label, "pony"].options["constraints"]), 1
    
  3850.         )
    
  3851.         Pony = new_state.apps.get_model(app_label, "Pony")
    
  3852.         self.assertEqual(len(Pony._meta.constraints), 1)
    
  3853.         with connection.schema_editor() as editor, CaptureQueriesContext(
    
  3854.             connection
    
  3855.         ) as ctx:
    
  3856.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3857.         Pony.objects.create(pink=1, weight=4.0)
    
  3858.         if connection.features.supports_covering_indexes:
    
  3859.             with self.assertRaises(IntegrityError):
    
  3860.                 Pony.objects.create(pink=1, weight=4.0)
    
  3861.         else:
    
  3862.             self.assertEqual(len(ctx), 0)
    
  3863.             Pony.objects.create(pink=1, weight=4.0)
    
  3864.         # Reversal.
    
  3865.         with connection.schema_editor() as editor:
    
  3866.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3867.         # Constraint doesn't work.
    
  3868.         Pony.objects.create(pink=1, weight=4.0)
    
  3869.         # Deconstruction.
    
  3870.         definition = operation.deconstruct()
    
  3871.         self.assertEqual(definition[0], "AddConstraint")
    
  3872.         self.assertEqual(definition[1], [])
    
  3873.         self.assertEqual(
    
  3874.             definition[2],
    
  3875.             {"model_name": "Pony", "constraint": covering_unique_constraint},
    
  3876.         )
    
  3877. 
    
  3878.     def test_remove_covering_unique_constraint(self):
    
  3879.         app_label = "test_removecovering_uc"
    
  3880.         covering_unique_constraint = models.UniqueConstraint(
    
  3881.             fields=["pink"],
    
  3882.             name="covering_pink_constraint_rm",
    
  3883.             include=["weight"],
    
  3884.         )
    
  3885.         project_state = self.set_up_test_model(
    
  3886.             app_label, constraints=[covering_unique_constraint]
    
  3887.         )
    
  3888.         operation = migrations.RemoveConstraint("Pony", covering_unique_constraint.name)
    
  3889.         self.assertEqual(
    
  3890.             operation.describe(),
    
  3891.             "Remove constraint covering_pink_constraint_rm from model Pony",
    
  3892.         )
    
  3893.         # Remove constraint.
    
  3894.         new_state = project_state.clone()
    
  3895.         operation.state_forwards(app_label, new_state)
    
  3896.         self.assertEqual(
    
  3897.             len(new_state.models[app_label, "pony"].options["constraints"]), 0
    
  3898.         )
    
  3899.         Pony = new_state.apps.get_model(app_label, "Pony")
    
  3900.         self.assertEqual(len(Pony._meta.constraints), 0)
    
  3901.         with connection.schema_editor() as editor, CaptureQueriesContext(
    
  3902.             connection
    
  3903.         ) as ctx:
    
  3904.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3905.         # Constraint doesn't work.
    
  3906.         Pony.objects.create(pink=1, weight=4.0)
    
  3907.         Pony.objects.create(pink=1, weight=4.0).delete()
    
  3908.         if not connection.features.supports_covering_indexes:
    
  3909.             self.assertEqual(len(ctx), 0)
    
  3910.         # Reversal.
    
  3911.         with connection.schema_editor() as editor:
    
  3912.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3913.         if connection.features.supports_covering_indexes:
    
  3914.             with self.assertRaises(IntegrityError):
    
  3915.                 Pony.objects.create(pink=1, weight=4.0)
    
  3916.         else:
    
  3917.             Pony.objects.create(pink=1, weight=4.0)
    
  3918.         # Deconstruction.
    
  3919.         definition = operation.deconstruct()
    
  3920.         self.assertEqual(definition[0], "RemoveConstraint")
    
  3921.         self.assertEqual(definition[1], [])
    
  3922.         self.assertEqual(
    
  3923.             definition[2],
    
  3924.             {
    
  3925.                 "model_name": "Pony",
    
  3926.                 "name": "covering_pink_constraint_rm",
    
  3927.             },
    
  3928.         )
    
  3929. 
    
  3930.     def test_alter_field_with_func_unique_constraint(self):
    
  3931.         app_label = "test_alfuncuc"
    
  3932.         constraint_name = f"{app_label}_pony_uq"
    
  3933.         table_name = f"{app_label}_pony"
    
  3934.         project_state = self.set_up_test_model(
    
  3935.             app_label,
    
  3936.             constraints=[
    
  3937.                 models.UniqueConstraint("pink", "weight", name=constraint_name)
    
  3938.             ],
    
  3939.         )
    
  3940.         operation = migrations.AlterField(
    
  3941.             "Pony", "pink", models.IntegerField(null=True)
    
  3942.         )
    
  3943.         new_state = project_state.clone()
    
  3944.         operation.state_forwards(app_label, new_state)
    
  3945.         with connection.schema_editor() as editor:
    
  3946.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3947.         if connection.features.supports_expression_indexes:
    
  3948.             self.assertIndexNameExists(table_name, constraint_name)
    
  3949.         with connection.schema_editor() as editor:
    
  3950.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3951.         if connection.features.supports_expression_indexes:
    
  3952.             self.assertIndexNameExists(table_name, constraint_name)
    
  3953. 
    
  3954.     def test_add_func_unique_constraint(self):
    
  3955.         app_label = "test_adfuncuc"
    
  3956.         constraint_name = f"{app_label}_pony_abs_uq"
    
  3957.         table_name = f"{app_label}_pony"
    
  3958.         project_state = self.set_up_test_model(app_label)
    
  3959.         constraint = models.UniqueConstraint(Abs("weight"), name=constraint_name)
    
  3960.         operation = migrations.AddConstraint("Pony", constraint)
    
  3961.         self.assertEqual(
    
  3962.             operation.describe(),
    
  3963.             "Create constraint test_adfuncuc_pony_abs_uq on model Pony",
    
  3964.         )
    
  3965.         self.assertEqual(
    
  3966.             operation.migration_name_fragment,
    
  3967.             "pony_test_adfuncuc_pony_abs_uq",
    
  3968.         )
    
  3969.         new_state = project_state.clone()
    
  3970.         operation.state_forwards(app_label, new_state)
    
  3971.         self.assertEqual(
    
  3972.             len(new_state.models[app_label, "pony"].options["constraints"]), 1
    
  3973.         )
    
  3974.         self.assertIndexNameNotExists(table_name, constraint_name)
    
  3975.         # Add constraint.
    
  3976.         with connection.schema_editor() as editor:
    
  3977.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  3978.         Pony = new_state.apps.get_model(app_label, "Pony")
    
  3979.         Pony.objects.create(weight=4.0)
    
  3980.         if connection.features.supports_expression_indexes:
    
  3981.             self.assertIndexNameExists(table_name, constraint_name)
    
  3982.             with self.assertRaises(IntegrityError):
    
  3983.                 Pony.objects.create(weight=-4.0)
    
  3984.         else:
    
  3985.             self.assertIndexNameNotExists(table_name, constraint_name)
    
  3986.             Pony.objects.create(weight=-4.0)
    
  3987.         # Reversal.
    
  3988.         with connection.schema_editor() as editor:
    
  3989.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  3990.         self.assertIndexNameNotExists(table_name, constraint_name)
    
  3991.         # Constraint doesn't work.
    
  3992.         Pony.objects.create(weight=-4.0)
    
  3993.         # Deconstruction.
    
  3994.         definition = operation.deconstruct()
    
  3995.         self.assertEqual(definition[0], "AddConstraint")
    
  3996.         self.assertEqual(definition[1], [])
    
  3997.         self.assertEqual(
    
  3998.             definition[2],
    
  3999.             {"model_name": "Pony", "constraint": constraint},
    
  4000.         )
    
  4001. 
    
  4002.     def test_remove_func_unique_constraint(self):
    
  4003.         app_label = "test_rmfuncuc"
    
  4004.         constraint_name = f"{app_label}_pony_abs_uq"
    
  4005.         table_name = f"{app_label}_pony"
    
  4006.         project_state = self.set_up_test_model(
    
  4007.             app_label,
    
  4008.             constraints=[
    
  4009.                 models.UniqueConstraint(Abs("weight"), name=constraint_name),
    
  4010.             ],
    
  4011.         )
    
  4012.         self.assertTableExists(table_name)
    
  4013.         if connection.features.supports_expression_indexes:
    
  4014.             self.assertIndexNameExists(table_name, constraint_name)
    
  4015.         operation = migrations.RemoveConstraint("Pony", constraint_name)
    
  4016.         self.assertEqual(
    
  4017.             operation.describe(),
    
  4018.             "Remove constraint test_rmfuncuc_pony_abs_uq from model Pony",
    
  4019.         )
    
  4020.         self.assertEqual(
    
  4021.             operation.migration_name_fragment,
    
  4022.             "remove_pony_test_rmfuncuc_pony_abs_uq",
    
  4023.         )
    
  4024.         new_state = project_state.clone()
    
  4025.         operation.state_forwards(app_label, new_state)
    
  4026.         self.assertEqual(
    
  4027.             len(new_state.models[app_label, "pony"].options["constraints"]), 0
    
  4028.         )
    
  4029.         Pony = new_state.apps.get_model(app_label, "Pony")
    
  4030.         self.assertEqual(len(Pony._meta.constraints), 0)
    
  4031.         # Remove constraint.
    
  4032.         with connection.schema_editor() as editor:
    
  4033.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  4034.         self.assertIndexNameNotExists(table_name, constraint_name)
    
  4035.         # Constraint doesn't work.
    
  4036.         Pony.objects.create(pink=1, weight=4.0)
    
  4037.         Pony.objects.create(pink=1, weight=-4.0).delete()
    
  4038.         # Reversal.
    
  4039.         with connection.schema_editor() as editor:
    
  4040.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  4041.         if connection.features.supports_expression_indexes:
    
  4042.             self.assertIndexNameExists(table_name, constraint_name)
    
  4043.             with self.assertRaises(IntegrityError):
    
  4044.                 Pony.objects.create(weight=-4.0)
    
  4045.         else:
    
  4046.             self.assertIndexNameNotExists(table_name, constraint_name)
    
  4047.             Pony.objects.create(weight=-4.0)
    
  4048.         # Deconstruction.
    
  4049.         definition = operation.deconstruct()
    
  4050.         self.assertEqual(definition[0], "RemoveConstraint")
    
  4051.         self.assertEqual(definition[1], [])
    
  4052.         self.assertEqual(definition[2], {"model_name": "Pony", "name": constraint_name})
    
  4053. 
    
  4054.     def test_alter_model_options(self):
    
  4055.         """
    
  4056.         Tests the AlterModelOptions operation.
    
  4057.         """
    
  4058.         project_state = self.set_up_test_model("test_almoop")
    
  4059.         # Test the state alteration (no DB alteration to test)
    
  4060.         operation = migrations.AlterModelOptions(
    
  4061.             "Pony", {"permissions": [("can_groom", "Can groom")]}
    
  4062.         )
    
  4063.         self.assertEqual(operation.describe(), "Change Meta options on Pony")
    
  4064.         self.assertEqual(operation.migration_name_fragment, "alter_pony_options")
    
  4065.         new_state = project_state.clone()
    
  4066.         operation.state_forwards("test_almoop", new_state)
    
  4067.         self.assertEqual(
    
  4068.             len(
    
  4069.                 project_state.models["test_almoop", "pony"].options.get(
    
  4070.                     "permissions", []
    
  4071.                 )
    
  4072.             ),
    
  4073.             0,
    
  4074.         )
    
  4075.         self.assertEqual(
    
  4076.             len(new_state.models["test_almoop", "pony"].options.get("permissions", [])),
    
  4077.             1,
    
  4078.         )
    
  4079.         self.assertEqual(
    
  4080.             new_state.models["test_almoop", "pony"].options["permissions"][0][0],
    
  4081.             "can_groom",
    
  4082.         )
    
  4083.         # And deconstruction
    
  4084.         definition = operation.deconstruct()
    
  4085.         self.assertEqual(definition[0], "AlterModelOptions")
    
  4086.         self.assertEqual(definition[1], [])
    
  4087.         self.assertEqual(
    
  4088.             definition[2],
    
  4089.             {"name": "Pony", "options": {"permissions": [("can_groom", "Can groom")]}},
    
  4090.         )
    
  4091. 
    
  4092.     def test_alter_model_options_emptying(self):
    
  4093.         """
    
  4094.         The AlterModelOptions operation removes keys from the dict (#23121)
    
  4095.         """
    
  4096.         project_state = self.set_up_test_model("test_almoop", options=True)
    
  4097.         # Test the state alteration (no DB alteration to test)
    
  4098.         operation = migrations.AlterModelOptions("Pony", {})
    
  4099.         self.assertEqual(operation.describe(), "Change Meta options on Pony")
    
  4100.         new_state = project_state.clone()
    
  4101.         operation.state_forwards("test_almoop", new_state)
    
  4102.         self.assertEqual(
    
  4103.             len(
    
  4104.                 project_state.models["test_almoop", "pony"].options.get(
    
  4105.                     "permissions", []
    
  4106.                 )
    
  4107.             ),
    
  4108.             1,
    
  4109.         )
    
  4110.         self.assertEqual(
    
  4111.             len(new_state.models["test_almoop", "pony"].options.get("permissions", [])),
    
  4112.             0,
    
  4113.         )
    
  4114.         # And deconstruction
    
  4115.         definition = operation.deconstruct()
    
  4116.         self.assertEqual(definition[0], "AlterModelOptions")
    
  4117.         self.assertEqual(definition[1], [])
    
  4118.         self.assertEqual(definition[2], {"name": "Pony", "options": {}})
    
  4119. 
    
  4120.     def test_alter_order_with_respect_to(self):
    
  4121.         """
    
  4122.         Tests the AlterOrderWithRespectTo operation.
    
  4123.         """
    
  4124.         project_state = self.set_up_test_model("test_alorwrtto", related_model=True)
    
  4125.         # Test the state alteration
    
  4126.         operation = migrations.AlterOrderWithRespectTo("Rider", "pony")
    
  4127.         self.assertEqual(
    
  4128.             operation.describe(), "Set order_with_respect_to on Rider to pony"
    
  4129.         )
    
  4130.         self.assertEqual(
    
  4131.             operation.migration_name_fragment,
    
  4132.             "alter_rider_order_with_respect_to",
    
  4133.         )
    
  4134.         new_state = project_state.clone()
    
  4135.         operation.state_forwards("test_alorwrtto", new_state)
    
  4136.         self.assertIsNone(
    
  4137.             project_state.models["test_alorwrtto", "rider"].options.get(
    
  4138.                 "order_with_respect_to", None
    
  4139.             )
    
  4140.         )
    
  4141.         self.assertEqual(
    
  4142.             new_state.models["test_alorwrtto", "rider"].options.get(
    
  4143.                 "order_with_respect_to", None
    
  4144.             ),
    
  4145.             "pony",
    
  4146.         )
    
  4147.         # Make sure there's no matching index
    
  4148.         self.assertColumnNotExists("test_alorwrtto_rider", "_order")
    
  4149.         # Create some rows before alteration
    
  4150.         rendered_state = project_state.apps
    
  4151.         pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(
    
  4152.             weight=50
    
  4153.         )
    
  4154.         rider1 = rendered_state.get_model("test_alorwrtto", "Rider").objects.create(
    
  4155.             pony=pony
    
  4156.         )
    
  4157.         rider1.friend = rider1
    
  4158.         rider1.save()
    
  4159.         rider2 = rendered_state.get_model("test_alorwrtto", "Rider").objects.create(
    
  4160.             pony=pony
    
  4161.         )
    
  4162.         rider2.friend = rider2
    
  4163.         rider2.save()
    
  4164.         # Test the database alteration
    
  4165.         with connection.schema_editor() as editor:
    
  4166.             operation.database_forwards(
    
  4167.                 "test_alorwrtto", editor, project_state, new_state
    
  4168.             )
    
  4169.         self.assertColumnExists("test_alorwrtto_rider", "_order")
    
  4170.         # Check for correct value in rows
    
  4171.         updated_riders = new_state.apps.get_model(
    
  4172.             "test_alorwrtto", "Rider"
    
  4173.         ).objects.all()
    
  4174.         self.assertEqual(updated_riders[0]._order, 0)
    
  4175.         self.assertEqual(updated_riders[1]._order, 0)
    
  4176.         # And test reversal
    
  4177.         with connection.schema_editor() as editor:
    
  4178.             operation.database_backwards(
    
  4179.                 "test_alorwrtto", editor, new_state, project_state
    
  4180.             )
    
  4181.         self.assertColumnNotExists("test_alorwrtto_rider", "_order")
    
  4182.         # And deconstruction
    
  4183.         definition = operation.deconstruct()
    
  4184.         self.assertEqual(definition[0], "AlterOrderWithRespectTo")
    
  4185.         self.assertEqual(definition[1], [])
    
  4186.         self.assertEqual(
    
  4187.             definition[2], {"name": "Rider", "order_with_respect_to": "pony"}
    
  4188.         )
    
  4189. 
    
  4190.     def test_alter_model_managers(self):
    
  4191.         """
    
  4192.         The managers on a model are set.
    
  4193.         """
    
  4194.         project_state = self.set_up_test_model("test_almoma")
    
  4195.         # Test the state alteration
    
  4196.         operation = migrations.AlterModelManagers(
    
  4197.             "Pony",
    
  4198.             managers=[
    
  4199.                 ("food_qs", FoodQuerySet.as_manager()),
    
  4200.                 ("food_mgr", FoodManager("a", "b")),
    
  4201.                 ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
    
  4202.             ],
    
  4203.         )
    
  4204.         self.assertEqual(operation.describe(), "Change managers on Pony")
    
  4205.         self.assertEqual(operation.migration_name_fragment, "alter_pony_managers")
    
  4206.         managers = project_state.models["test_almoma", "pony"].managers
    
  4207.         self.assertEqual(managers, [])
    
  4208. 
    
  4209.         new_state = project_state.clone()
    
  4210.         operation.state_forwards("test_almoma", new_state)
    
  4211.         self.assertIn(("test_almoma", "pony"), new_state.models)
    
  4212.         managers = new_state.models["test_almoma", "pony"].managers
    
  4213.         self.assertEqual(managers[0][0], "food_qs")
    
  4214.         self.assertIsInstance(managers[0][1], models.Manager)
    
  4215.         self.assertEqual(managers[1][0], "food_mgr")
    
  4216.         self.assertIsInstance(managers[1][1], FoodManager)
    
  4217.         self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
    
  4218.         self.assertEqual(managers[2][0], "food_mgr_kwargs")
    
  4219.         self.assertIsInstance(managers[2][1], FoodManager)
    
  4220.         self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
    
  4221.         rendered_state = new_state.apps
    
  4222.         model = rendered_state.get_model("test_almoma", "pony")
    
  4223.         self.assertIsInstance(model.food_qs, models.Manager)
    
  4224.         self.assertIsInstance(model.food_mgr, FoodManager)
    
  4225.         self.assertIsInstance(model.food_mgr_kwargs, FoodManager)
    
  4226. 
    
  4227.     def test_alter_model_managers_emptying(self):
    
  4228.         """
    
  4229.         The managers on a model are set.
    
  4230.         """
    
  4231.         project_state = self.set_up_test_model("test_almomae", manager_model=True)
    
  4232.         # Test the state alteration
    
  4233.         operation = migrations.AlterModelManagers("Food", managers=[])
    
  4234.         self.assertEqual(operation.describe(), "Change managers on Food")
    
  4235.         self.assertIn(("test_almomae", "food"), project_state.models)
    
  4236.         managers = project_state.models["test_almomae", "food"].managers
    
  4237.         self.assertEqual(managers[0][0], "food_qs")
    
  4238.         self.assertIsInstance(managers[0][1], models.Manager)
    
  4239.         self.assertEqual(managers[1][0], "food_mgr")
    
  4240.         self.assertIsInstance(managers[1][1], FoodManager)
    
  4241.         self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
    
  4242.         self.assertEqual(managers[2][0], "food_mgr_kwargs")
    
  4243.         self.assertIsInstance(managers[2][1], FoodManager)
    
  4244.         self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
    
  4245. 
    
  4246.         new_state = project_state.clone()
    
  4247.         operation.state_forwards("test_almomae", new_state)
    
  4248.         managers = new_state.models["test_almomae", "food"].managers
    
  4249.         self.assertEqual(managers, [])
    
  4250. 
    
  4251.     def test_alter_fk(self):
    
  4252.         """
    
  4253.         Creating and then altering an FK works correctly
    
  4254.         and deals with the pending SQL (#23091)
    
  4255.         """
    
  4256.         project_state = self.set_up_test_model("test_alfk")
    
  4257.         # Test adding and then altering the FK in one go
    
  4258.         create_operation = migrations.CreateModel(
    
  4259.             name="Rider",
    
  4260.             fields=[
    
  4261.                 ("id", models.AutoField(primary_key=True)),
    
  4262.                 ("pony", models.ForeignKey("Pony", models.CASCADE)),
    
  4263.             ],
    
  4264.         )
    
  4265.         create_state = project_state.clone()
    
  4266.         create_operation.state_forwards("test_alfk", create_state)
    
  4267.         alter_operation = migrations.AlterField(
    
  4268.             model_name="Rider",
    
  4269.             name="pony",
    
  4270.             field=models.ForeignKey("Pony", models.CASCADE, editable=False),
    
  4271.         )
    
  4272.         alter_state = create_state.clone()
    
  4273.         alter_operation.state_forwards("test_alfk", alter_state)
    
  4274.         with connection.schema_editor() as editor:
    
  4275.             create_operation.database_forwards(
    
  4276.                 "test_alfk", editor, project_state, create_state
    
  4277.             )
    
  4278.             alter_operation.database_forwards(
    
  4279.                 "test_alfk", editor, create_state, alter_state
    
  4280.             )
    
  4281. 
    
  4282.     def test_alter_fk_non_fk(self):
    
  4283.         """
    
  4284.         Altering an FK to a non-FK works (#23244)
    
  4285.         """
    
  4286.         # Test the state alteration
    
  4287.         operation = migrations.AlterField(
    
  4288.             model_name="Rider",
    
  4289.             name="pony",
    
  4290.             field=models.FloatField(),
    
  4291.         )
    
  4292.         project_state, new_state = self.make_test_state(
    
  4293.             "test_afknfk", operation, related_model=True
    
  4294.         )
    
  4295.         # Test the database alteration
    
  4296.         self.assertColumnExists("test_afknfk_rider", "pony_id")
    
  4297.         self.assertColumnNotExists("test_afknfk_rider", "pony")
    
  4298.         with connection.schema_editor() as editor:
    
  4299.             operation.database_forwards("test_afknfk", editor, project_state, new_state)
    
  4300.         self.assertColumnExists("test_afknfk_rider", "pony")
    
  4301.         self.assertColumnNotExists("test_afknfk_rider", "pony_id")
    
  4302.         # And test reversal
    
  4303.         with connection.schema_editor() as editor:
    
  4304.             operation.database_backwards(
    
  4305.                 "test_afknfk", editor, new_state, project_state
    
  4306.             )
    
  4307.         self.assertColumnExists("test_afknfk_rider", "pony_id")
    
  4308.         self.assertColumnNotExists("test_afknfk_rider", "pony")
    
  4309. 
    
  4310.     def test_run_sql(self):
    
  4311.         """
    
  4312.         Tests the RunSQL operation.
    
  4313.         """
    
  4314.         project_state = self.set_up_test_model("test_runsql")
    
  4315.         # Create the operation
    
  4316.         operation = migrations.RunSQL(
    
  4317.             # Use a multi-line string with a comment to test splitting on
    
  4318.             # SQLite and MySQL respectively.
    
  4319.             "CREATE TABLE i_love_ponies (id int, special_thing varchar(15));\n"
    
  4320.             "INSERT INTO i_love_ponies (id, special_thing) "
    
  4321.             "VALUES (1, 'i love ponies'); -- this is magic!\n"
    
  4322.             "INSERT INTO i_love_ponies (id, special_thing) "
    
  4323.             "VALUES (2, 'i love django');\n"
    
  4324.             "UPDATE i_love_ponies SET special_thing = 'Ponies' "
    
  4325.             "WHERE special_thing LIKE '%%ponies';"
    
  4326.             "UPDATE i_love_ponies SET special_thing = 'Django' "
    
  4327.             "WHERE special_thing LIKE '%django';",
    
  4328.             # Run delete queries to test for parameter substitution failure
    
  4329.             # reported in #23426
    
  4330.             "DELETE FROM i_love_ponies WHERE special_thing LIKE '%Django%';"
    
  4331.             "DELETE FROM i_love_ponies WHERE special_thing LIKE '%%Ponies%%';"
    
  4332.             "DROP TABLE i_love_ponies",
    
  4333.             state_operations=[
    
  4334.                 migrations.CreateModel(
    
  4335.                     "SomethingElse", [("id", models.AutoField(primary_key=True))]
    
  4336.                 )
    
  4337.             ],
    
  4338.         )
    
  4339.         self.assertEqual(operation.describe(), "Raw SQL operation")
    
  4340.         # Test the state alteration
    
  4341.         new_state = project_state.clone()
    
  4342.         operation.state_forwards("test_runsql", new_state)
    
  4343.         self.assertEqual(
    
  4344.             len(new_state.models["test_runsql", "somethingelse"].fields), 1
    
  4345.         )
    
  4346.         # Make sure there's no table
    
  4347.         self.assertTableNotExists("i_love_ponies")
    
  4348.         # Test SQL collection
    
  4349.         with connection.schema_editor(collect_sql=True) as editor:
    
  4350.             operation.database_forwards("test_runsql", editor, project_state, new_state)
    
  4351.             self.assertIn("LIKE '%%ponies';", "\n".join(editor.collected_sql))
    
  4352.             operation.database_backwards(
    
  4353.                 "test_runsql", editor, project_state, new_state
    
  4354.             )
    
  4355.             self.assertIn("LIKE '%%Ponies%%';", "\n".join(editor.collected_sql))
    
  4356.         # Test the database alteration
    
  4357.         with connection.schema_editor() as editor:
    
  4358.             operation.database_forwards("test_runsql", editor, project_state, new_state)
    
  4359.         self.assertTableExists("i_love_ponies")
    
  4360.         # Make sure all the SQL was processed
    
  4361.         with connection.cursor() as cursor:
    
  4362.             cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
    
  4363.             self.assertEqual(cursor.fetchall()[0][0], 2)
    
  4364.             cursor.execute(
    
  4365.                 "SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Django'"
    
  4366.             )
    
  4367.             self.assertEqual(cursor.fetchall()[0][0], 1)
    
  4368.             cursor.execute(
    
  4369.                 "SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Ponies'"
    
  4370.             )
    
  4371.             self.assertEqual(cursor.fetchall()[0][0], 1)
    
  4372.         # And test reversal
    
  4373.         self.assertTrue(operation.reversible)
    
  4374.         with connection.schema_editor() as editor:
    
  4375.             operation.database_backwards(
    
  4376.                 "test_runsql", editor, new_state, project_state
    
  4377.             )
    
  4378.         self.assertTableNotExists("i_love_ponies")
    
  4379.         # And deconstruction
    
  4380.         definition = operation.deconstruct()
    
  4381.         self.assertEqual(definition[0], "RunSQL")
    
  4382.         self.assertEqual(definition[1], [])
    
  4383.         self.assertEqual(
    
  4384.             sorted(definition[2]), ["reverse_sql", "sql", "state_operations"]
    
  4385.         )
    
  4386.         # And elidable reduction
    
  4387.         self.assertIs(False, operation.reduce(operation, []))
    
  4388.         elidable_operation = migrations.RunSQL("SELECT 1 FROM void;", elidable=True)
    
  4389.         self.assertEqual(elidable_operation.reduce(operation, []), [operation])
    
  4390. 
    
  4391.     def test_run_sql_params(self):
    
  4392.         """
    
  4393.         #23426 - RunSQL should accept parameters.
    
  4394.         """
    
  4395.         project_state = self.set_up_test_model("test_runsql")
    
  4396.         # Create the operation
    
  4397.         operation = migrations.RunSQL(
    
  4398.             ["CREATE TABLE i_love_ponies (id int, special_thing varchar(15));"],
    
  4399.             ["DROP TABLE i_love_ponies"],
    
  4400.         )
    
  4401.         param_operation = migrations.RunSQL(
    
  4402.             # forwards
    
  4403.             (
    
  4404.                 "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'Django');",
    
  4405.                 [
    
  4406.                     "INSERT INTO i_love_ponies (id, special_thing) VALUES (2, %s);",
    
  4407.                     ["Ponies"],
    
  4408.                 ],
    
  4409.                 (
    
  4410.                     "INSERT INTO i_love_ponies (id, special_thing) VALUES (%s, %s);",
    
  4411.                     (
    
  4412.                         3,
    
  4413.                         "Python",
    
  4414.                     ),
    
  4415.                 ),
    
  4416.             ),
    
  4417.             # backwards
    
  4418.             [
    
  4419.                 "DELETE FROM i_love_ponies WHERE special_thing = 'Django';",
    
  4420.                 ["DELETE FROM i_love_ponies WHERE special_thing = 'Ponies';", None],
    
  4421.                 (
    
  4422.                     "DELETE FROM i_love_ponies WHERE id = %s OR special_thing = %s;",
    
  4423.                     [3, "Python"],
    
  4424.                 ),
    
  4425.             ],
    
  4426.         )
    
  4427. 
    
  4428.         # Make sure there's no table
    
  4429.         self.assertTableNotExists("i_love_ponies")
    
  4430.         new_state = project_state.clone()
    
  4431.         # Test the database alteration
    
  4432.         with connection.schema_editor() as editor:
    
  4433.             operation.database_forwards("test_runsql", editor, project_state, new_state)
    
  4434. 
    
  4435.         # Test parameter passing
    
  4436.         with connection.schema_editor() as editor:
    
  4437.             param_operation.database_forwards(
    
  4438.                 "test_runsql", editor, project_state, new_state
    
  4439.             )
    
  4440.         # Make sure all the SQL was processed
    
  4441.         with connection.cursor() as cursor:
    
  4442.             cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
    
  4443.             self.assertEqual(cursor.fetchall()[0][0], 3)
    
  4444. 
    
  4445.         with connection.schema_editor() as editor:
    
  4446.             param_operation.database_backwards(
    
  4447.                 "test_runsql", editor, new_state, project_state
    
  4448.             )
    
  4449.         with connection.cursor() as cursor:
    
  4450.             cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
    
  4451.             self.assertEqual(cursor.fetchall()[0][0], 0)
    
  4452. 
    
  4453.         # And test reversal
    
  4454.         with connection.schema_editor() as editor:
    
  4455.             operation.database_backwards(
    
  4456.                 "test_runsql", editor, new_state, project_state
    
  4457.             )
    
  4458.         self.assertTableNotExists("i_love_ponies")
    
  4459. 
    
  4460.     def test_run_sql_params_invalid(self):
    
  4461.         """
    
  4462.         #23426 - RunSQL should fail when a list of statements with an incorrect
    
  4463.         number of tuples is given.
    
  4464.         """
    
  4465.         project_state = self.set_up_test_model("test_runsql")
    
  4466.         new_state = project_state.clone()
    
  4467.         operation = migrations.RunSQL(
    
  4468.             # forwards
    
  4469.             [["INSERT INTO foo (bar) VALUES ('buz');"]],
    
  4470.             # backwards
    
  4471.             (("DELETE FROM foo WHERE bar = 'buz';", "invalid", "parameter count"),),
    
  4472.         )
    
  4473. 
    
  4474.         with connection.schema_editor() as editor:
    
  4475.             with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 1"):
    
  4476.                 operation.database_forwards(
    
  4477.                     "test_runsql", editor, project_state, new_state
    
  4478.                 )
    
  4479. 
    
  4480.         with connection.schema_editor() as editor:
    
  4481.             with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 3"):
    
  4482.                 operation.database_backwards(
    
  4483.                     "test_runsql", editor, new_state, project_state
    
  4484.                 )
    
  4485. 
    
  4486.     def test_run_sql_noop(self):
    
  4487.         """
    
  4488.         #24098 - Tests no-op RunSQL operations.
    
  4489.         """
    
  4490.         operation = migrations.RunSQL(migrations.RunSQL.noop, migrations.RunSQL.noop)
    
  4491.         with connection.schema_editor() as editor:
    
  4492.             operation.database_forwards("test_runsql", editor, None, None)
    
  4493.             operation.database_backwards("test_runsql", editor, None, None)
    
  4494. 
    
  4495.     def test_run_sql_add_missing_semicolon_on_collect_sql(self):
    
  4496.         project_state = self.set_up_test_model("test_runsql")
    
  4497.         new_state = project_state.clone()
    
  4498.         tests = [
    
  4499.             "INSERT INTO test_runsql_pony (pink, weight) VALUES (1, 1);\n",
    
  4500.             "INSERT INTO test_runsql_pony (pink, weight) VALUES (1, 1)\n",
    
  4501.         ]
    
  4502.         for sql in tests:
    
  4503.             with self.subTest(sql=sql):
    
  4504.                 operation = migrations.RunSQL(sql, migrations.RunPython.noop)
    
  4505.                 with connection.schema_editor(collect_sql=True) as editor:
    
  4506.                     operation.database_forwards(
    
  4507.                         "test_runsql", editor, project_state, new_state
    
  4508.                     )
    
  4509.                     collected_sql = "\n".join(editor.collected_sql)
    
  4510.                     self.assertEqual(collected_sql.count(";"), 1)
    
  4511. 
    
  4512.     def test_run_python(self):
    
  4513.         """
    
  4514.         Tests the RunPython operation
    
  4515.         """
    
  4516. 
    
  4517.         project_state = self.set_up_test_model("test_runpython", mti_model=True)
    
  4518. 
    
  4519.         # Create the operation
    
  4520.         def inner_method(models, schema_editor):
    
  4521.             Pony = models.get_model("test_runpython", "Pony")
    
  4522.             Pony.objects.create(pink=1, weight=3.55)
    
  4523.             Pony.objects.create(weight=5)
    
  4524. 
    
  4525.         def inner_method_reverse(models, schema_editor):
    
  4526.             Pony = models.get_model("test_runpython", "Pony")
    
  4527.             Pony.objects.filter(pink=1, weight=3.55).delete()
    
  4528.             Pony.objects.filter(weight=5).delete()
    
  4529. 
    
  4530.         operation = migrations.RunPython(
    
  4531.             inner_method, reverse_code=inner_method_reverse
    
  4532.         )
    
  4533.         self.assertEqual(operation.describe(), "Raw Python operation")
    
  4534.         # Test the state alteration does nothing
    
  4535.         new_state = project_state.clone()
    
  4536.         operation.state_forwards("test_runpython", new_state)
    
  4537.         self.assertEqual(new_state, project_state)
    
  4538.         # Test the database alteration
    
  4539.         self.assertEqual(
    
  4540.             project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0
    
  4541.         )
    
  4542.         with connection.schema_editor() as editor:
    
  4543.             operation.database_forwards(
    
  4544.                 "test_runpython", editor, project_state, new_state
    
  4545.             )
    
  4546.         self.assertEqual(
    
  4547.             project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2
    
  4548.         )
    
  4549.         # Now test reversal
    
  4550.         self.assertTrue(operation.reversible)
    
  4551.         with connection.schema_editor() as editor:
    
  4552.             operation.database_backwards(
    
  4553.                 "test_runpython", editor, project_state, new_state
    
  4554.             )
    
  4555.         self.assertEqual(
    
  4556.             project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0
    
  4557.         )
    
  4558.         # Now test we can't use a string
    
  4559.         with self.assertRaisesMessage(
    
  4560.             ValueError, "RunPython must be supplied with a callable"
    
  4561.         ):
    
  4562.             migrations.RunPython("print 'ahahaha'")
    
  4563.         # And deconstruction
    
  4564.         definition = operation.deconstruct()
    
  4565.         self.assertEqual(definition[0], "RunPython")
    
  4566.         self.assertEqual(definition[1], [])
    
  4567.         self.assertEqual(sorted(definition[2]), ["code", "reverse_code"])
    
  4568. 
    
  4569.         # Also test reversal fails, with an operation identical to above but
    
  4570.         # without reverse_code set.
    
  4571.         no_reverse_operation = migrations.RunPython(inner_method)
    
  4572.         self.assertFalse(no_reverse_operation.reversible)
    
  4573.         with connection.schema_editor() as editor:
    
  4574.             no_reverse_operation.database_forwards(
    
  4575.                 "test_runpython", editor, project_state, new_state
    
  4576.             )
    
  4577.             with self.assertRaises(NotImplementedError):
    
  4578.                 no_reverse_operation.database_backwards(
    
  4579.                     "test_runpython", editor, new_state, project_state
    
  4580.                 )
    
  4581.         self.assertEqual(
    
  4582.             project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2
    
  4583.         )
    
  4584. 
    
  4585.         def create_ponies(models, schema_editor):
    
  4586.             Pony = models.get_model("test_runpython", "Pony")
    
  4587.             pony1 = Pony.objects.create(pink=1, weight=3.55)
    
  4588.             self.assertIsNot(pony1.pk, None)
    
  4589.             pony2 = Pony.objects.create(weight=5)
    
  4590.             self.assertIsNot(pony2.pk, None)
    
  4591.             self.assertNotEqual(pony1.pk, pony2.pk)
    
  4592. 
    
  4593.         operation = migrations.RunPython(create_ponies)
    
  4594.         with connection.schema_editor() as editor:
    
  4595.             operation.database_forwards(
    
  4596.                 "test_runpython", editor, project_state, new_state
    
  4597.             )
    
  4598.         self.assertEqual(
    
  4599.             project_state.apps.get_model("test_runpython", "Pony").objects.count(), 4
    
  4600.         )
    
  4601.         # And deconstruction
    
  4602.         definition = operation.deconstruct()
    
  4603.         self.assertEqual(definition[0], "RunPython")
    
  4604.         self.assertEqual(definition[1], [])
    
  4605.         self.assertEqual(sorted(definition[2]), ["code"])
    
  4606. 
    
  4607.         def create_shetlandponies(models, schema_editor):
    
  4608.             ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
    
  4609.             pony1 = ShetlandPony.objects.create(weight=4.0)
    
  4610.             self.assertIsNot(pony1.pk, None)
    
  4611.             pony2 = ShetlandPony.objects.create(weight=5.0)
    
  4612.             self.assertIsNot(pony2.pk, None)
    
  4613.             self.assertNotEqual(pony1.pk, pony2.pk)
    
  4614. 
    
  4615.         operation = migrations.RunPython(create_shetlandponies)
    
  4616.         with connection.schema_editor() as editor:
    
  4617.             operation.database_forwards(
    
  4618.                 "test_runpython", editor, project_state, new_state
    
  4619.             )
    
  4620.         self.assertEqual(
    
  4621.             project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6
    
  4622.         )
    
  4623.         self.assertEqual(
    
  4624.             project_state.apps.get_model(
    
  4625.                 "test_runpython", "ShetlandPony"
    
  4626.             ).objects.count(),
    
  4627.             2,
    
  4628.         )
    
  4629.         # And elidable reduction
    
  4630.         self.assertIs(False, operation.reduce(operation, []))
    
  4631.         elidable_operation = migrations.RunPython(inner_method, elidable=True)
    
  4632.         self.assertEqual(elidable_operation.reduce(operation, []), [operation])
    
  4633. 
    
  4634.     def test_run_python_atomic(self):
    
  4635.         """
    
  4636.         Tests the RunPython operation correctly handles the "atomic" keyword
    
  4637.         """
    
  4638.         project_state = self.set_up_test_model("test_runpythonatomic", mti_model=True)
    
  4639. 
    
  4640.         def inner_method(models, schema_editor):
    
  4641.             Pony = models.get_model("test_runpythonatomic", "Pony")
    
  4642.             Pony.objects.create(pink=1, weight=3.55)
    
  4643.             raise ValueError("Adrian hates ponies.")
    
  4644. 
    
  4645.         # Verify atomicity when applying.
    
  4646.         atomic_migration = Migration("test", "test_runpythonatomic")
    
  4647.         atomic_migration.operations = [
    
  4648.             migrations.RunPython(inner_method, reverse_code=inner_method)
    
  4649.         ]
    
  4650.         non_atomic_migration = Migration("test", "test_runpythonatomic")
    
  4651.         non_atomic_migration.operations = [
    
  4652.             migrations.RunPython(inner_method, reverse_code=inner_method, atomic=False)
    
  4653.         ]
    
  4654.         # If we're a fully-transactional database, both versions should rollback
    
  4655.         if connection.features.can_rollback_ddl:
    
  4656.             self.assertEqual(
    
  4657.                 project_state.apps.get_model(
    
  4658.                     "test_runpythonatomic", "Pony"
    
  4659.                 ).objects.count(),
    
  4660.                 0,
    
  4661.             )
    
  4662.             with self.assertRaises(ValueError):
    
  4663.                 with connection.schema_editor() as editor:
    
  4664.                     atomic_migration.apply(project_state, editor)
    
  4665.             self.assertEqual(
    
  4666.                 project_state.apps.get_model(
    
  4667.                     "test_runpythonatomic", "Pony"
    
  4668.                 ).objects.count(),
    
  4669.                 0,
    
  4670.             )
    
  4671.             with self.assertRaises(ValueError):
    
  4672.                 with connection.schema_editor() as editor:
    
  4673.                     non_atomic_migration.apply(project_state, editor)
    
  4674.             self.assertEqual(
    
  4675.                 project_state.apps.get_model(
    
  4676.                     "test_runpythonatomic", "Pony"
    
  4677.                 ).objects.count(),
    
  4678.                 0,
    
  4679.             )
    
  4680.         # Otherwise, the non-atomic operation should leave a row there
    
  4681.         else:
    
  4682.             self.assertEqual(
    
  4683.                 project_state.apps.get_model(
    
  4684.                     "test_runpythonatomic", "Pony"
    
  4685.                 ).objects.count(),
    
  4686.                 0,
    
  4687.             )
    
  4688.             with self.assertRaises(ValueError):
    
  4689.                 with connection.schema_editor() as editor:
    
  4690.                     atomic_migration.apply(project_state, editor)
    
  4691.             self.assertEqual(
    
  4692.                 project_state.apps.get_model(
    
  4693.                     "test_runpythonatomic", "Pony"
    
  4694.                 ).objects.count(),
    
  4695.                 0,
    
  4696.             )
    
  4697.             with self.assertRaises(ValueError):
    
  4698.                 with connection.schema_editor() as editor:
    
  4699.                     non_atomic_migration.apply(project_state, editor)
    
  4700.             self.assertEqual(
    
  4701.                 project_state.apps.get_model(
    
  4702.                     "test_runpythonatomic", "Pony"
    
  4703.                 ).objects.count(),
    
  4704.                 1,
    
  4705.             )
    
  4706.         # Reset object count to zero and verify atomicity when unapplying.
    
  4707.         project_state.apps.get_model(
    
  4708.             "test_runpythonatomic", "Pony"
    
  4709.         ).objects.all().delete()
    
  4710.         # On a fully-transactional database, both versions rollback.
    
  4711.         if connection.features.can_rollback_ddl:
    
  4712.             self.assertEqual(
    
  4713.                 project_state.apps.get_model(
    
  4714.                     "test_runpythonatomic", "Pony"
    
  4715.                 ).objects.count(),
    
  4716.                 0,
    
  4717.             )
    
  4718.             with self.assertRaises(ValueError):
    
  4719.                 with connection.schema_editor() as editor:
    
  4720.                     atomic_migration.unapply(project_state, editor)
    
  4721.             self.assertEqual(
    
  4722.                 project_state.apps.get_model(
    
  4723.                     "test_runpythonatomic", "Pony"
    
  4724.                 ).objects.count(),
    
  4725.                 0,
    
  4726.             )
    
  4727.             with self.assertRaises(ValueError):
    
  4728.                 with connection.schema_editor() as editor:
    
  4729.                     non_atomic_migration.unapply(project_state, editor)
    
  4730.             self.assertEqual(
    
  4731.                 project_state.apps.get_model(
    
  4732.                     "test_runpythonatomic", "Pony"
    
  4733.                 ).objects.count(),
    
  4734.                 0,
    
  4735.             )
    
  4736.         # Otherwise, the non-atomic operation leaves a row there.
    
  4737.         else:
    
  4738.             self.assertEqual(
    
  4739.                 project_state.apps.get_model(
    
  4740.                     "test_runpythonatomic", "Pony"
    
  4741.                 ).objects.count(),
    
  4742.                 0,
    
  4743.             )
    
  4744.             with self.assertRaises(ValueError):
    
  4745.                 with connection.schema_editor() as editor:
    
  4746.                     atomic_migration.unapply(project_state, editor)
    
  4747.             self.assertEqual(
    
  4748.                 project_state.apps.get_model(
    
  4749.                     "test_runpythonatomic", "Pony"
    
  4750.                 ).objects.count(),
    
  4751.                 0,
    
  4752.             )
    
  4753.             with self.assertRaises(ValueError):
    
  4754.                 with connection.schema_editor() as editor:
    
  4755.                     non_atomic_migration.unapply(project_state, editor)
    
  4756.             self.assertEqual(
    
  4757.                 project_state.apps.get_model(
    
  4758.                     "test_runpythonatomic", "Pony"
    
  4759.                 ).objects.count(),
    
  4760.                 1,
    
  4761.             )
    
  4762.         # Verify deconstruction.
    
  4763.         definition = non_atomic_migration.operations[0].deconstruct()
    
  4764.         self.assertEqual(definition[0], "RunPython")
    
  4765.         self.assertEqual(definition[1], [])
    
  4766.         self.assertEqual(sorted(definition[2]), ["atomic", "code", "reverse_code"])
    
  4767. 
    
  4768.     def test_run_python_related_assignment(self):
    
  4769.         """
    
  4770.         #24282 - Model changes to a FK reverse side update the model
    
  4771.         on the FK side as well.
    
  4772.         """
    
  4773. 
    
  4774.         def inner_method(models, schema_editor):
    
  4775.             Author = models.get_model("test_authors", "Author")
    
  4776.             Book = models.get_model("test_books", "Book")
    
  4777.             author = Author.objects.create(name="Hemingway")
    
  4778.             Book.objects.create(title="Old Man and The Sea", author=author)
    
  4779. 
    
  4780.         create_author = migrations.CreateModel(
    
  4781.             "Author",
    
  4782.             [
    
  4783.                 ("id", models.AutoField(primary_key=True)),
    
  4784.                 ("name", models.CharField(max_length=100)),
    
  4785.             ],
    
  4786.             options={},
    
  4787.         )
    
  4788.         create_book = migrations.CreateModel(
    
  4789.             "Book",
    
  4790.             [
    
  4791.                 ("id", models.AutoField(primary_key=True)),
    
  4792.                 ("title", models.CharField(max_length=100)),
    
  4793.                 ("author", models.ForeignKey("test_authors.Author", models.CASCADE)),
    
  4794.             ],
    
  4795.             options={},
    
  4796.         )
    
  4797.         add_hometown = migrations.AddField(
    
  4798.             "Author",
    
  4799.             "hometown",
    
  4800.             models.CharField(max_length=100),
    
  4801.         )
    
  4802.         create_old_man = migrations.RunPython(inner_method, inner_method)
    
  4803. 
    
  4804.         project_state = ProjectState()
    
  4805.         new_state = project_state.clone()
    
  4806.         with connection.schema_editor() as editor:
    
  4807.             create_author.state_forwards("test_authors", new_state)
    
  4808.             create_author.database_forwards(
    
  4809.                 "test_authors", editor, project_state, new_state
    
  4810.             )
    
  4811.         project_state = new_state
    
  4812.         new_state = new_state.clone()
    
  4813.         with connection.schema_editor() as editor:
    
  4814.             create_book.state_forwards("test_books", new_state)
    
  4815.             create_book.database_forwards(
    
  4816.                 "test_books", editor, project_state, new_state
    
  4817.             )
    
  4818.         project_state = new_state
    
  4819.         new_state = new_state.clone()
    
  4820.         with connection.schema_editor() as editor:
    
  4821.             add_hometown.state_forwards("test_authors", new_state)
    
  4822.             add_hometown.database_forwards(
    
  4823.                 "test_authors", editor, project_state, new_state
    
  4824.             )
    
  4825.         project_state = new_state
    
  4826.         new_state = new_state.clone()
    
  4827.         with connection.schema_editor() as editor:
    
  4828.             create_old_man.state_forwards("test_books", new_state)
    
  4829.             create_old_man.database_forwards(
    
  4830.                 "test_books", editor, project_state, new_state
    
  4831.             )
    
  4832. 
    
  4833.     def test_model_with_bigautofield(self):
    
  4834.         """
    
  4835.         A model with BigAutoField can be created.
    
  4836.         """
    
  4837. 
    
  4838.         def create_data(models, schema_editor):
    
  4839.             Author = models.get_model("test_author", "Author")
    
  4840.             Book = models.get_model("test_book", "Book")
    
  4841.             author1 = Author.objects.create(name="Hemingway")
    
  4842.             Book.objects.create(title="Old Man and The Sea", author=author1)
    
  4843.             Book.objects.create(id=2**33, title="A farewell to arms", author=author1)
    
  4844. 
    
  4845.             author2 = Author.objects.create(id=2**33, name="Remarque")
    
  4846.             Book.objects.create(title="All quiet on the western front", author=author2)
    
  4847.             Book.objects.create(title="Arc de Triomphe", author=author2)
    
  4848. 
    
  4849.         create_author = migrations.CreateModel(
    
  4850.             "Author",
    
  4851.             [
    
  4852.                 ("id", models.BigAutoField(primary_key=True)),
    
  4853.                 ("name", models.CharField(max_length=100)),
    
  4854.             ],
    
  4855.             options={},
    
  4856.         )
    
  4857.         create_book = migrations.CreateModel(
    
  4858.             "Book",
    
  4859.             [
    
  4860.                 ("id", models.BigAutoField(primary_key=True)),
    
  4861.                 ("title", models.CharField(max_length=100)),
    
  4862.                 (
    
  4863.                     "author",
    
  4864.                     models.ForeignKey(
    
  4865.                         to="test_author.Author", on_delete=models.CASCADE
    
  4866.                     ),
    
  4867.                 ),
    
  4868.             ],
    
  4869.             options={},
    
  4870.         )
    
  4871.         fill_data = migrations.RunPython(create_data)
    
  4872. 
    
  4873.         project_state = ProjectState()
    
  4874.         new_state = project_state.clone()
    
  4875.         with connection.schema_editor() as editor:
    
  4876.             create_author.state_forwards("test_author", new_state)
    
  4877.             create_author.database_forwards(
    
  4878.                 "test_author", editor, project_state, new_state
    
  4879.             )
    
  4880. 
    
  4881.         project_state = new_state
    
  4882.         new_state = new_state.clone()
    
  4883.         with connection.schema_editor() as editor:
    
  4884.             create_book.state_forwards("test_book", new_state)
    
  4885.             create_book.database_forwards("test_book", editor, project_state, new_state)
    
  4886. 
    
  4887.         project_state = new_state
    
  4888.         new_state = new_state.clone()
    
  4889.         with connection.schema_editor() as editor:
    
  4890.             fill_data.state_forwards("fill_data", new_state)
    
  4891.             fill_data.database_forwards("fill_data", editor, project_state, new_state)
    
  4892. 
    
  4893.     def _test_autofield_foreignfield_growth(
    
  4894.         self, source_field, target_field, target_value
    
  4895.     ):
    
  4896.         """
    
  4897.         A field may be migrated in the following ways:
    
  4898. 
    
  4899.         - AutoField to BigAutoField
    
  4900.         - SmallAutoField to AutoField
    
  4901.         - SmallAutoField to BigAutoField
    
  4902.         """
    
  4903. 
    
  4904.         def create_initial_data(models, schema_editor):
    
  4905.             Article = models.get_model("test_article", "Article")
    
  4906.             Blog = models.get_model("test_blog", "Blog")
    
  4907.             blog = Blog.objects.create(name="web development done right")
    
  4908.             Article.objects.create(name="Frameworks", blog=blog)
    
  4909.             Article.objects.create(name="Programming Languages", blog=blog)
    
  4910. 
    
  4911.         def create_big_data(models, schema_editor):
    
  4912.             Article = models.get_model("test_article", "Article")
    
  4913.             Blog = models.get_model("test_blog", "Blog")
    
  4914.             blog2 = Blog.objects.create(name="Frameworks", id=target_value)
    
  4915.             Article.objects.create(name="Django", blog=blog2)
    
  4916.             Article.objects.create(id=target_value, name="Django2", blog=blog2)
    
  4917. 
    
  4918.         create_blog = migrations.CreateModel(
    
  4919.             "Blog",
    
  4920.             [
    
  4921.                 ("id", source_field(primary_key=True)),
    
  4922.                 ("name", models.CharField(max_length=100)),
    
  4923.             ],
    
  4924.             options={},
    
  4925.         )
    
  4926.         create_article = migrations.CreateModel(
    
  4927.             "Article",
    
  4928.             [
    
  4929.                 ("id", source_field(primary_key=True)),
    
  4930.                 (
    
  4931.                     "blog",
    
  4932.                     models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE),
    
  4933.                 ),
    
  4934.                 ("name", models.CharField(max_length=100)),
    
  4935.                 ("data", models.TextField(default="")),
    
  4936.             ],
    
  4937.             options={},
    
  4938.         )
    
  4939.         fill_initial_data = migrations.RunPython(
    
  4940.             create_initial_data, create_initial_data
    
  4941.         )
    
  4942.         fill_big_data = migrations.RunPython(create_big_data, create_big_data)
    
  4943. 
    
  4944.         grow_article_id = migrations.AlterField(
    
  4945.             "Article", "id", target_field(primary_key=True)
    
  4946.         )
    
  4947.         grow_blog_id = migrations.AlterField(
    
  4948.             "Blog", "id", target_field(primary_key=True)
    
  4949.         )
    
  4950. 
    
  4951.         project_state = ProjectState()
    
  4952.         new_state = project_state.clone()
    
  4953.         with connection.schema_editor() as editor:
    
  4954.             create_blog.state_forwards("test_blog", new_state)
    
  4955.             create_blog.database_forwards("test_blog", editor, project_state, new_state)
    
  4956. 
    
  4957.         project_state = new_state
    
  4958.         new_state = new_state.clone()
    
  4959.         with connection.schema_editor() as editor:
    
  4960.             create_article.state_forwards("test_article", new_state)
    
  4961.             create_article.database_forwards(
    
  4962.                 "test_article", editor, project_state, new_state
    
  4963.             )
    
  4964. 
    
  4965.         project_state = new_state
    
  4966.         new_state = new_state.clone()
    
  4967.         with connection.schema_editor() as editor:
    
  4968.             fill_initial_data.state_forwards("fill_initial_data", new_state)
    
  4969.             fill_initial_data.database_forwards(
    
  4970.                 "fill_initial_data", editor, project_state, new_state
    
  4971.             )
    
  4972. 
    
  4973.         project_state = new_state
    
  4974.         new_state = new_state.clone()
    
  4975.         with connection.schema_editor() as editor:
    
  4976.             grow_article_id.state_forwards("test_article", new_state)
    
  4977.             grow_article_id.database_forwards(
    
  4978.                 "test_article", editor, project_state, new_state
    
  4979.             )
    
  4980. 
    
  4981.         state = new_state.clone()
    
  4982.         article = state.apps.get_model("test_article.Article")
    
  4983.         self.assertIsInstance(article._meta.pk, target_field)
    
  4984. 
    
  4985.         project_state = new_state
    
  4986.         new_state = new_state.clone()
    
  4987.         with connection.schema_editor() as editor:
    
  4988.             grow_blog_id.state_forwards("test_blog", new_state)
    
  4989.             grow_blog_id.database_forwards(
    
  4990.                 "test_blog", editor, project_state, new_state
    
  4991.             )
    
  4992. 
    
  4993.         state = new_state.clone()
    
  4994.         blog = state.apps.get_model("test_blog.Blog")
    
  4995.         self.assertIsInstance(blog._meta.pk, target_field)
    
  4996. 
    
  4997.         project_state = new_state
    
  4998.         new_state = new_state.clone()
    
  4999.         with connection.schema_editor() as editor:
    
  5000.             fill_big_data.state_forwards("fill_big_data", new_state)
    
  5001.             fill_big_data.database_forwards(
    
  5002.                 "fill_big_data", editor, project_state, new_state
    
  5003.             )
    
  5004. 
    
  5005.     def test_autofield__bigautofield_foreignfield_growth(self):
    
  5006.         """A field may be migrated from AutoField to BigAutoField."""
    
  5007.         self._test_autofield_foreignfield_growth(
    
  5008.             models.AutoField,
    
  5009.             models.BigAutoField,
    
  5010.             2**33,
    
  5011.         )
    
  5012. 
    
  5013.     def test_smallfield_autofield_foreignfield_growth(self):
    
  5014.         """A field may be migrated from SmallAutoField to AutoField."""
    
  5015.         self._test_autofield_foreignfield_growth(
    
  5016.             models.SmallAutoField,
    
  5017.             models.AutoField,
    
  5018.             2**22,
    
  5019.         )
    
  5020. 
    
  5021.     def test_smallfield_bigautofield_foreignfield_growth(self):
    
  5022.         """A field may be migrated from SmallAutoField to BigAutoField."""
    
  5023.         self._test_autofield_foreignfield_growth(
    
  5024.             models.SmallAutoField,
    
  5025.             models.BigAutoField,
    
  5026.             2**33,
    
  5027.         )
    
  5028. 
    
  5029.     def test_run_python_noop(self):
    
  5030.         """
    
  5031.         #24098 - Tests no-op RunPython operations.
    
  5032.         """
    
  5033.         project_state = ProjectState()
    
  5034.         new_state = project_state.clone()
    
  5035.         operation = migrations.RunPython(
    
  5036.             migrations.RunPython.noop, migrations.RunPython.noop
    
  5037.         )
    
  5038.         with connection.schema_editor() as editor:
    
  5039.             operation.database_forwards(
    
  5040.                 "test_runpython", editor, project_state, new_state
    
  5041.             )
    
  5042.             operation.database_backwards(
    
  5043.                 "test_runpython", editor, new_state, project_state
    
  5044.             )
    
  5045. 
    
  5046.     def test_separate_database_and_state(self):
    
  5047.         """
    
  5048.         Tests the SeparateDatabaseAndState operation.
    
  5049.         """
    
  5050.         project_state = self.set_up_test_model("test_separatedatabaseandstate")
    
  5051.         # Create the operation
    
  5052.         database_operation = migrations.RunSQL(
    
  5053.             "CREATE TABLE i_love_ponies (id int, special_thing int);",
    
  5054.             "DROP TABLE i_love_ponies;",
    
  5055.         )
    
  5056.         state_operation = migrations.CreateModel(
    
  5057.             "SomethingElse", [("id", models.AutoField(primary_key=True))]
    
  5058.         )
    
  5059.         operation = migrations.SeparateDatabaseAndState(
    
  5060.             state_operations=[state_operation], database_operations=[database_operation]
    
  5061.         )
    
  5062.         self.assertEqual(
    
  5063.             operation.describe(), "Custom state/database change combination"
    
  5064.         )
    
  5065.         # Test the state alteration
    
  5066.         new_state = project_state.clone()
    
  5067.         operation.state_forwards("test_separatedatabaseandstate", new_state)
    
  5068.         self.assertEqual(
    
  5069.             len(
    
  5070.                 new_state.models[
    
  5071.                     "test_separatedatabaseandstate", "somethingelse"
    
  5072.                 ].fields
    
  5073.             ),
    
  5074.             1,
    
  5075.         )
    
  5076.         # Make sure there's no table
    
  5077.         self.assertTableNotExists("i_love_ponies")
    
  5078.         # Test the database alteration
    
  5079.         with connection.schema_editor() as editor:
    
  5080.             operation.database_forwards(
    
  5081.                 "test_separatedatabaseandstate", editor, project_state, new_state
    
  5082.             )
    
  5083.         self.assertTableExists("i_love_ponies")
    
  5084.         # And test reversal
    
  5085.         self.assertTrue(operation.reversible)
    
  5086.         with connection.schema_editor() as editor:
    
  5087.             operation.database_backwards(
    
  5088.                 "test_separatedatabaseandstate", editor, new_state, project_state
    
  5089.             )
    
  5090.         self.assertTableNotExists("i_love_ponies")
    
  5091.         # And deconstruction
    
  5092.         definition = operation.deconstruct()
    
  5093.         self.assertEqual(definition[0], "SeparateDatabaseAndState")
    
  5094.         self.assertEqual(definition[1], [])
    
  5095.         self.assertEqual(
    
  5096.             sorted(definition[2]), ["database_operations", "state_operations"]
    
  5097.         )
    
  5098. 
    
  5099.     def test_separate_database_and_state2(self):
    
  5100.         """
    
  5101.         A complex SeparateDatabaseAndState operation: Multiple operations both
    
  5102.         for state and database. Verify the state dependencies within each list
    
  5103.         and that state ops don't affect the database.
    
  5104.         """
    
  5105.         app_label = "test_separatedatabaseandstate2"
    
  5106.         project_state = self.set_up_test_model(app_label)
    
  5107.         # Create the operation
    
  5108.         database_operations = [
    
  5109.             migrations.CreateModel(
    
  5110.                 "ILovePonies",
    
  5111.                 [("id", models.AutoField(primary_key=True))],
    
  5112.                 options={"db_table": "iloveponies"},
    
  5113.             ),
    
  5114.             migrations.CreateModel(
    
  5115.                 "ILoveMorePonies",
    
  5116.                 # We use IntegerField and not AutoField because
    
  5117.                 # the model is going to be deleted immediately
    
  5118.                 # and with an AutoField this fails on Oracle
    
  5119.                 [("id", models.IntegerField(primary_key=True))],
    
  5120.                 options={"db_table": "ilovemoreponies"},
    
  5121.             ),
    
  5122.             migrations.DeleteModel("ILoveMorePonies"),
    
  5123.             migrations.CreateModel(
    
  5124.                 "ILoveEvenMorePonies",
    
  5125.                 [("id", models.AutoField(primary_key=True))],
    
  5126.                 options={"db_table": "iloveevenmoreponies"},
    
  5127.             ),
    
  5128.         ]
    
  5129.         state_operations = [
    
  5130.             migrations.CreateModel(
    
  5131.                 "SomethingElse",
    
  5132.                 [("id", models.AutoField(primary_key=True))],
    
  5133.                 options={"db_table": "somethingelse"},
    
  5134.             ),
    
  5135.             migrations.DeleteModel("SomethingElse"),
    
  5136.             migrations.CreateModel(
    
  5137.                 "SomethingCompletelyDifferent",
    
  5138.                 [("id", models.AutoField(primary_key=True))],
    
  5139.                 options={"db_table": "somethingcompletelydifferent"},
    
  5140.             ),
    
  5141.         ]
    
  5142.         operation = migrations.SeparateDatabaseAndState(
    
  5143.             state_operations=state_operations,
    
  5144.             database_operations=database_operations,
    
  5145.         )
    
  5146.         # Test the state alteration
    
  5147.         new_state = project_state.clone()
    
  5148.         operation.state_forwards(app_label, new_state)
    
  5149. 
    
  5150.         def assertModelsAndTables(after_db):
    
  5151.             # Tables and models exist, or don't, as they should:
    
  5152.             self.assertNotIn((app_label, "somethingelse"), new_state.models)
    
  5153.             self.assertEqual(
    
  5154.                 len(new_state.models[app_label, "somethingcompletelydifferent"].fields),
    
  5155.                 1,
    
  5156.             )
    
  5157.             self.assertNotIn((app_label, "iloveponiesonies"), new_state.models)
    
  5158.             self.assertNotIn((app_label, "ilovemoreponies"), new_state.models)
    
  5159.             self.assertNotIn((app_label, "iloveevenmoreponies"), new_state.models)
    
  5160.             self.assertTableNotExists("somethingelse")
    
  5161.             self.assertTableNotExists("somethingcompletelydifferent")
    
  5162.             self.assertTableNotExists("ilovemoreponies")
    
  5163.             if after_db:
    
  5164.                 self.assertTableExists("iloveponies")
    
  5165.                 self.assertTableExists("iloveevenmoreponies")
    
  5166.             else:
    
  5167.                 self.assertTableNotExists("iloveponies")
    
  5168.                 self.assertTableNotExists("iloveevenmoreponies")
    
  5169. 
    
  5170.         assertModelsAndTables(after_db=False)
    
  5171.         # Test the database alteration
    
  5172.         with connection.schema_editor() as editor:
    
  5173.             operation.database_forwards(app_label, editor, project_state, new_state)
    
  5174.         assertModelsAndTables(after_db=True)
    
  5175.         # And test reversal
    
  5176.         self.assertTrue(operation.reversible)
    
  5177.         with connection.schema_editor() as editor:
    
  5178.             operation.database_backwards(app_label, editor, new_state, project_state)
    
  5179.         assertModelsAndTables(after_db=False)
    
  5180. 
    
  5181. 
    
  5182. class SwappableOperationTests(OperationTestBase):
    
  5183.     """
    
  5184.     Key operations ignore swappable models
    
  5185.     (we don't want to replicate all of them here, as the functionality
    
  5186.     is in a common base class anyway)
    
  5187.     """
    
  5188. 
    
  5189.     available_apps = ["migrations"]
    
  5190. 
    
  5191.     @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
    
  5192.     def test_create_ignore_swapped(self):
    
  5193.         """
    
  5194.         The CreateTable operation ignores swapped models.
    
  5195.         """
    
  5196.         operation = migrations.CreateModel(
    
  5197.             "Pony",
    
  5198.             [
    
  5199.                 ("id", models.AutoField(primary_key=True)),
    
  5200.                 ("pink", models.IntegerField(default=1)),
    
  5201.             ],
    
  5202.             options={
    
  5203.                 "swappable": "TEST_SWAP_MODEL",
    
  5204.             },
    
  5205.         )
    
  5206.         # Test the state alteration (it should still be there!)
    
  5207.         project_state = ProjectState()
    
  5208.         new_state = project_state.clone()
    
  5209.         operation.state_forwards("test_crigsw", new_state)
    
  5210.         self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
    
  5211.         self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
    
  5212.         # Test the database alteration
    
  5213.         self.assertTableNotExists("test_crigsw_pony")
    
  5214.         with connection.schema_editor() as editor:
    
  5215.             operation.database_forwards("test_crigsw", editor, project_state, new_state)
    
  5216.         self.assertTableNotExists("test_crigsw_pony")
    
  5217.         # And test reversal
    
  5218.         with connection.schema_editor() as editor:
    
  5219.             operation.database_backwards(
    
  5220.                 "test_crigsw", editor, new_state, project_state
    
  5221.             )
    
  5222.         self.assertTableNotExists("test_crigsw_pony")
    
  5223. 
    
  5224.     @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
    
  5225.     def test_delete_ignore_swapped(self):
    
  5226.         """
    
  5227.         Tests the DeleteModel operation ignores swapped models.
    
  5228.         """
    
  5229.         operation = migrations.DeleteModel("Pony")
    
  5230.         project_state, new_state = self.make_test_state("test_dligsw", operation)
    
  5231.         # Test the database alteration
    
  5232.         self.assertTableNotExists("test_dligsw_pony")
    
  5233.         with connection.schema_editor() as editor:
    
  5234.             operation.database_forwards("test_dligsw", editor, project_state, new_state)
    
  5235.         self.assertTableNotExists("test_dligsw_pony")
    
  5236.         # And test reversal
    
  5237.         with connection.schema_editor() as editor:
    
  5238.             operation.database_backwards(
    
  5239.                 "test_dligsw", editor, new_state, project_state
    
  5240.             )
    
  5241.         self.assertTableNotExists("test_dligsw_pony")
    
  5242. 
    
  5243.     @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
    
  5244.     def test_add_field_ignore_swapped(self):
    
  5245.         """
    
  5246.         Tests the AddField operation.
    
  5247.         """
    
  5248.         # Test the state alteration
    
  5249.         operation = migrations.AddField(
    
  5250.             "Pony",
    
  5251.             "height",
    
  5252.             models.FloatField(null=True, default=5),
    
  5253.         )
    
  5254.         project_state, new_state = self.make_test_state("test_adfligsw", operation)
    
  5255.         # Test the database alteration
    
  5256.         self.assertTableNotExists("test_adfligsw_pony")
    
  5257.         with connection.schema_editor() as editor:
    
  5258.             operation.database_forwards(
    
  5259.                 "test_adfligsw", editor, project_state, new_state
    
  5260.             )
    
  5261.         self.assertTableNotExists("test_adfligsw_pony")
    
  5262.         # And test reversal
    
  5263.         with connection.schema_editor() as editor:
    
  5264.             operation.database_backwards(
    
  5265.                 "test_adfligsw", editor, new_state, project_state
    
  5266.             )
    
  5267.         self.assertTableNotExists("test_adfligsw_pony")
    
  5268. 
    
  5269.     @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
    
  5270.     def test_indexes_ignore_swapped(self):
    
  5271.         """
    
  5272.         Add/RemoveIndex operations ignore swapped models.
    
  5273.         """
    
  5274.         operation = migrations.AddIndex(
    
  5275.             "Pony", models.Index(fields=["pink"], name="my_name_idx")
    
  5276.         )
    
  5277.         project_state, new_state = self.make_test_state("test_adinigsw", operation)
    
  5278.         with connection.schema_editor() as editor:
    
  5279.             # No database queries should be run for swapped models
    
  5280.             operation.database_forwards(
    
  5281.                 "test_adinigsw", editor, project_state, new_state
    
  5282.             )
    
  5283.             operation.database_backwards(
    
  5284.                 "test_adinigsw", editor, new_state, project_state
    
  5285.             )
    
  5286. 
    
  5287.         operation = migrations.RemoveIndex(
    
  5288.             "Pony", models.Index(fields=["pink"], name="my_name_idx")
    
  5289.         )
    
  5290.         project_state, new_state = self.make_test_state("test_rminigsw", operation)
    
  5291.         with connection.schema_editor() as editor:
    
  5292.             operation.database_forwards(
    
  5293.                 "test_rminigsw", editor, project_state, new_state
    
  5294.             )
    
  5295.             operation.database_backwards(
    
  5296.                 "test_rminigsw", editor, new_state, project_state
    
  5297.             )
    
  5298. 
    
  5299. 
    
  5300. class TestCreateModel(SimpleTestCase):
    
  5301.     def test_references_model_mixin(self):
    
  5302.         migrations.CreateModel(
    
  5303.             "name",
    
  5304.             fields=[],
    
  5305.             bases=(Mixin, models.Model),
    
  5306.         ).references_model("other_model", "migrations")
    
  5307. 
    
  5308. 
    
  5309. class FieldOperationTests(SimpleTestCase):
    
  5310.     def test_references_model(self):
    
  5311.         operation = FieldOperation(
    
  5312.             "MoDel", "field", models.ForeignKey("Other", models.CASCADE)
    
  5313.         )
    
  5314.         # Model name match.
    
  5315.         self.assertIs(operation.references_model("mOdEl", "migrations"), True)
    
  5316.         # Referenced field.
    
  5317.         self.assertIs(operation.references_model("oTher", "migrations"), True)
    
  5318.         # Doesn't reference.
    
  5319.         self.assertIs(operation.references_model("Whatever", "migrations"), False)
    
  5320. 
    
  5321.     def test_references_field_by_name(self):
    
  5322.         operation = FieldOperation("MoDel", "field", models.BooleanField(default=False))
    
  5323.         self.assertIs(operation.references_field("model", "field", "migrations"), True)
    
  5324. 
    
  5325.     def test_references_field_by_remote_field_model(self):
    
  5326.         operation = FieldOperation(
    
  5327.             "Model", "field", models.ForeignKey("Other", models.CASCADE)
    
  5328.         )
    
  5329.         self.assertIs(
    
  5330.             operation.references_field("Other", "whatever", "migrations"), True
    
  5331.         )
    
  5332.         self.assertIs(
    
  5333.             operation.references_field("Missing", "whatever", "migrations"), False
    
  5334.         )
    
  5335. 
    
  5336.     def test_references_field_by_from_fields(self):
    
  5337.         operation = FieldOperation(
    
  5338.             "Model",
    
  5339.             "field",
    
  5340.             models.fields.related.ForeignObject(
    
  5341.                 "Other", models.CASCADE, ["from"], ["to"]
    
  5342.             ),
    
  5343.         )
    
  5344.         self.assertIs(operation.references_field("Model", "from", "migrations"), True)
    
  5345.         self.assertIs(operation.references_field("Model", "to", "migrations"), False)
    
  5346.         self.assertIs(operation.references_field("Other", "from", "migrations"), False)
    
  5347.         self.assertIs(operation.references_field("Model", "to", "migrations"), False)
    
  5348. 
    
  5349.     def test_references_field_by_to_fields(self):
    
  5350.         operation = FieldOperation(
    
  5351.             "Model",
    
  5352.             "field",
    
  5353.             models.ForeignKey("Other", models.CASCADE, to_field="field"),
    
  5354.         )
    
  5355.         self.assertIs(operation.references_field("Other", "field", "migrations"), True)
    
  5356.         self.assertIs(
    
  5357.             operation.references_field("Other", "whatever", "migrations"), False
    
  5358.         )
    
  5359.         self.assertIs(
    
  5360.             operation.references_field("Missing", "whatever", "migrations"), False
    
  5361.         )
    
  5362. 
    
  5363.     def test_references_field_by_through(self):
    
  5364.         operation = FieldOperation(
    
  5365.             "Model", "field", models.ManyToManyField("Other", through="Through")
    
  5366.         )
    
  5367.         self.assertIs(
    
  5368.             operation.references_field("Other", "whatever", "migrations"), True
    
  5369.         )
    
  5370.         self.assertIs(
    
  5371.             operation.references_field("Through", "whatever", "migrations"), True
    
  5372.         )
    
  5373.         self.assertIs(
    
  5374.             operation.references_field("Missing", "whatever", "migrations"), False
    
  5375.         )
    
  5376. 
    
  5377.     def test_reference_field_by_through_fields(self):
    
  5378.         operation = FieldOperation(
    
  5379.             "Model",
    
  5380.             "field",
    
  5381.             models.ManyToManyField(
    
  5382.                 "Other", through="Through", through_fields=("first", "second")
    
  5383.             ),
    
  5384.         )
    
  5385.         self.assertIs(
    
  5386.             operation.references_field("Other", "whatever", "migrations"), True
    
  5387.         )
    
  5388.         self.assertIs(
    
  5389.             operation.references_field("Through", "whatever", "migrations"), False
    
  5390.         )
    
  5391.         self.assertIs(
    
  5392.             operation.references_field("Through", "first", "migrations"), True
    
  5393.         )
    
  5394.         self.assertIs(
    
  5395.             operation.references_field("Through", "second", "migrations"), True
    
  5396.         )