Refs #35038 -- Reduced CreateModel/AlterConstraint operations when optimizing migrations.
Some checks are pending
Linters / flake8 (push) Waiting to run
Linters / isort (push) Waiting to run
Linters / black (push) Waiting to run
Tests / Windows, SQLite, Python 3.13 (push) Waiting to run
Tests / JavaScript tests (push) Waiting to run

This commit is contained in:
Jacob Walls 2025-06-19 06:22:23 -04:00 committed by GitHub
parent bc1bfe12b6
commit 56f468681a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 63 additions and 0 deletions

View file

@ -1434,6 +1434,54 @@ class OptimizerTests(OptimizerTestBase):
],
)
def test_create_model_alter_constraint(self):
original_constraint = models.CheckConstraint(
condition=models.Q(weight__gt=0), name="pony_weight_gt_0"
)
altered_constraint = models.CheckConstraint(
condition=models.Q(weight__gt=0),
name="pony_weight_gt_0",
violation_error_message="incorrect weight",
)
self.assertOptimizesTo(
[
migrations.CreateModel(
name="Pony",
fields=[
("weight", models.IntegerField()),
],
options={
"constraints": [
original_constraint,
models.UniqueConstraint(
"weight", name="pony_weight_unique"
),
],
},
),
migrations.AlterConstraint(
"Pony", "pony_weight_gt_0", altered_constraint
),
],
[
migrations.CreateModel(
name="Pony",
fields=[
("weight", models.IntegerField()),
],
options={
"constraints": [
models.UniqueConstraint(
"weight",
name="pony_weight_unique",
),
altered_constraint,
]
},
),
],
)
def test_create_model_remove_constraint(self):
self.assertOptimizesTo(
[