Skip to content

Commit 4dd81f3

Browse files
committed
Update Patch model constraint to be database constraints.
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
1 parent 39d1234 commit 4dd81f3

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

vulnerabilities/migrations/0104_packagecommitpatch_patch_and_more.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 4.2.25 on 2025-12-05 09:16
1+
# Generated by Django 4.2.25 on 2025-12-11 11:10
22

33
from django.db import migrations, models
44

@@ -35,9 +35,6 @@ class Migration(migrations.Migration):
3535
("patch_text", models.TextField(blank=True, null=True)),
3636
("patch_checksum", models.CharField(blank=True, max_length=128, null=True)),
3737
],
38-
options={
39-
"unique_together": {("commit_hash", "vcs_url")},
40-
},
4138
),
4239
migrations.CreateModel(
4340
name="Patch",
@@ -72,9 +69,6 @@ class Migration(migrations.Migration):
7269
),
7370
),
7471
],
75-
options={
76-
"unique_together": {("patch_checksum", "patch_url")},
77-
},
7872
),
7973
migrations.RemoveField(
8074
model_name="impactedpackage",
@@ -103,6 +97,23 @@ class Migration(migrations.Migration):
10397
migrations.DeleteModel(
10498
name="CodeCommit",
10599
),
100+
migrations.AddConstraint(
101+
model_name="patch",
102+
constraint=models.CheckConstraint(
103+
check=models.Q(
104+
("patch_url__isnull", False), ("patch_text__isnull", False), _connector="OR"
105+
),
106+
name="patch_url_or_patch_text",
107+
),
108+
),
109+
migrations.AlterUniqueTogether(
110+
name="patch",
111+
unique_together={("patch_checksum", "patch_url")},
112+
),
113+
migrations.AlterUniqueTogether(
114+
name="packagecommitpatch",
115+
unique_together={("commit_hash", "vcs_url")},
116+
),
106117
migrations.AddField(
107118
model_name="advisoryv2",
108119
name="patches",

vulnerabilities/models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,19 +2773,19 @@ class Patch(models.Model):
27732773
help_text="SHA512 checksum of the patch content.",
27742774
)
27752775

2776-
def clean(self):
2777-
if not self.patch_url and not self.patch_text:
2778-
raise ValidationError("Either patch_url or patch_text must be provided.")
2779-
27802776
def save(self, *args, **kwargs):
2781-
# https://docs.djangoproject.com/en/4.2/ref/models/instances/#django.db.models.Model.clean
2782-
self.full_clean()
27832777
if self.patch_text:
27842778
self.patch_checksum = compute_patch_checksum(self.patch_text)
27852779
super().save(*args, **kwargs)
27862780

27872781
class Meta:
27882782
unique_together = ["patch_checksum", "patch_url"]
2783+
constraints = [
2784+
models.CheckConstraint(
2785+
check=(Q(patch_url__isnull=False) | Q(patch_text__isnull=False)),
2786+
name="patch_url_or_patch_text",
2787+
)
2788+
]
27892789

27902790

27912791
class PackageCommitPatch(models.Model):

0 commit comments

Comments
 (0)