mirror of
https://github.com/django/django.git
synced 2025-06-23 20:39:12 +00:00
Refs #35038 -- Reduced CreateModel/AlterConstraint operations when optimizing migrations.
This commit is contained in:
parent
bc1bfe12b6
commit
56f468681a
@ -346,6 +346,21 @@ class CreateModel(ModelOperation):
|
||||
},
|
||||
),
|
||||
]
|
||||
elif isinstance(operation, AlterConstraint):
|
||||
options_constraints = [
|
||||
constraint
|
||||
for constraint in self.options.get("constraints", [])
|
||||
if constraint.name != operation.name
|
||||
] + [operation.constraint]
|
||||
return [
|
||||
replace(
|
||||
self,
|
||||
options={
|
||||
**self.options,
|
||||
"constraints": options_constraints,
|
||||
},
|
||||
),
|
||||
]
|
||||
elif isinstance(operation, RemoveConstraint):
|
||||
options_constraints = [
|
||||
constraint
|
||||
|
@ -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(
|
||||
[
|
||||
|
Loading…
x
Reference in New Issue
Block a user