Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update Patch model constraint to be database constraints.
Signed-off-by: ziad hany <ziadhany2016@gmail.com>
  • Loading branch information
ziadhany committed Dec 15, 2025
commit eb2d06360186e9a6fd68b44eb563fa1f17ebd4b6
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.25 on 2025-12-05 09:16
# Generated by Django 4.2.25 on 2025-12-11 11:10

from django.db import migrations, models

Expand Down Expand Up @@ -35,9 +35,6 @@ class Migration(migrations.Migration):
("patch_text", models.TextField(blank=True, null=True)),
("patch_checksum", models.CharField(blank=True, max_length=128, null=True)),
],
options={
"unique_together": {("commit_hash", "vcs_url")},
},
),
migrations.CreateModel(
name="Patch",
Expand Down Expand Up @@ -72,9 +69,6 @@ class Migration(migrations.Migration):
),
),
],
options={
"unique_together": {("patch_checksum", "patch_url")},
},
),
migrations.RemoveField(
model_name="impactedpackage",
Expand Down Expand Up @@ -103,6 +97,23 @@ class Migration(migrations.Migration):
migrations.DeleteModel(
name="CodeCommit",
),
migrations.AddConstraint(
model_name="patch",
constraint=models.CheckConstraint(
check=models.Q(
("patch_url__isnull", False), ("patch_text__isnull", False), _connector="OR"
),
name="patch_url_or_patch_text",
),
),
migrations.AlterUniqueTogether(
name="patch",
unique_together={("patch_checksum", "patch_url")},
),
migrations.AlterUniqueTogether(
name="packagecommitpatch",
unique_together={("commit_hash", "vcs_url")},
),
migrations.AddField(
model_name="advisoryv2",
name="patches",
Expand Down
12 changes: 6 additions & 6 deletions vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2773,19 +2773,19 @@ class Patch(models.Model):
help_text="SHA512 checksum of the patch content.",
)

def clean(self):
if not self.patch_url and not self.patch_text:
raise ValidationError("Either patch_url or patch_text must be provided.")

def save(self, *args, **kwargs):
# https://docs.djangoproject.com/en/4.2/ref/models/instances/#django.db.models.Model.clean
self.full_clean()
if self.patch_text:
self.patch_checksum = compute_patch_checksum(self.patch_text)
super().save(*args, **kwargs)

class Meta:
unique_together = ["patch_checksum", "patch_url"]
Comment thread
ziadhany marked this conversation as resolved.
constraints = [
models.CheckConstraint(
check=(Q(patch_url__isnull=False) | Q(patch_text__isnull=False)),
Comment thread
ziadhany marked this conversation as resolved.
Outdated
name="patch_url_or_patch_text",
)
]


class PackageCommitPatch(models.Model):
Comment thread
ziadhany marked this conversation as resolved.
Expand Down