summaryrefslogtreecommitdiff
path: root/tests/migrations
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2024-07-23 00:33:31 -0400
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-07-25 07:48:53 +0200
commitf359990e4909db8722820849d61a6f5724338723 (patch)
treed8b6116068513e09a7877ee88311b1a115f40767 /tests/migrations
parent8d6a20b656ff3fa18e36954668a44a831c2f6ddd (diff)
Fixed #35625 -- Fixed a crash when adding a field with db_default and check constraint.
This is the exact same issue as refs #30408 but for creating a model with a constraint containing % escapes instead of column addition. All of these issues stem from a lack of SQL and parameters separation from the BaseConstraint DDL generating methods preventing them from being mixed with other parts of the schema alteration logic that do make use of parametrization on some backends (e.g. Postgres, MySQL for DEFAULT). Prior to the addition of Field.db_default and GeneratedField in 5.0 parametrization of DDL was never exercised on model creation so this is effectively a bug with db_default as the GeneratedField case was addressed by refs #35336. Thanks Julien Chaumont for the report and Mariusz Felisiak for the review.
Diffstat (limited to 'tests/migrations')
-rw-r--r--tests/migrations/test_operations.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index f865500829..3ac813b899 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -4108,6 +4108,64 @@ class OperationTests(OperationTestBase):
)
@skipUnlessDBFeature("supports_table_check_constraints")
+ def test_create_model_constraint_percent_escaping(self):
+ app_label = "add_constraint_string_quoting"
+ from_state = ProjectState()
+ checks = [
+ # "%" generated in startswith lookup should be escaped in a way
+ # that is considered a leading wildcard.
+ (
+ models.Q(name__startswith="Albert"),
+ {"name": "Alberta"},
+ {"name": "Artur"},
+ ),
+ # Literal "%" should be escaped in a way that is not a considered a
+ # wildcard.
+ (models.Q(rebate__endswith="%"), {"rebate": "10%"}, {"rebate": "10%$"}),
+ # Right-hand-side baked "%" literals should not be used for
+ # parameters interpolation.
+ (
+ ~models.Q(surname__startswith=models.F("name")),
+ {"name": "Albert"},
+ {"name": "Albert", "surname": "Alberto"},
+ ),
+ # Exact matches against "%" literals should also be supported.
+ (
+ models.Q(name="%"),
+ {"name": "%"},
+ {"name": "Albert"},
+ ),
+ ]
+ for check, valid, invalid in checks:
+ with self.subTest(condition=check, valid=valid, invalid=invalid):
+ constraint = models.CheckConstraint(condition=check, name="constraint")
+ operation = migrations.CreateModel(
+ "Author",
+ fields=[
+ ("id", models.AutoField(primary_key=True)),
+ ("name", models.CharField(max_length=100)),
+ ("surname", models.CharField(max_length=100, db_default="")),
+ ("rebate", models.CharField(max_length=100)),
+ ],
+ options={"constraints": [constraint]},
+ )
+ to_state = from_state.clone()
+ operation.state_forwards(app_label, to_state)
+ with connection.schema_editor() as editor:
+ operation.database_forwards(app_label, editor, from_state, to_state)
+ Author = to_state.apps.get_model(app_label, "Author")
+ try:
+ with transaction.atomic():
+ Author.objects.create(**valid).delete()
+ with self.assertRaises(IntegrityError), transaction.atomic():
+ Author.objects.create(**invalid)
+ finally:
+ with connection.schema_editor() as editor:
+ migrations.DeleteModel("Author").database_forwards(
+ app_label, editor, to_state, from_state
+ )
+
+ @skipUnlessDBFeature("supports_table_check_constraints")
def test_add_constraint_percent_escaping(self):
app_label = "add_constraint_string_quoting"
operations = [