Skip to content

Commit a59b5e4

Browse files
authored
Merge pull request #653 from TG1999/models/migrations
Reference: https://github.com/vulnerablecode/issue/650 Work around PosgreSQL index issue.
2 parents 33e083c + e6f12d5 commit a59b5e4

6 files changed

Lines changed: 507 additions & 4 deletions

File tree

requirements-dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
black==22.1.0
1+
black==22.3.0
22
freezegun==1.1.0
33
ipython==8.0.1
44
isort==5.10.1
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 4.0.2 on 2022-03-29 09:38
2+
3+
from django.db import migrations
4+
from django.db import models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
("vulnerabilities", "0003_alter_advisory_created_by"),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name="advisory",
16+
name="unique_content_id",
17+
field=models.CharField(blank=True, max_length=32, null=True),
18+
),
19+
]
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Generated by Django 4.0.2 on 2022-03-29 09:38
2+
3+
import hashlib
4+
import json
5+
6+
from django.db import migrations
7+
8+
9+
class Migration(migrations.Migration):
10+
def md5hash(apps, schema_editor):
11+
Advisory = apps.get_model("vulnerabilities", "Advisory")
12+
for advisory in Advisory.objects.all():
13+
checksum = hashlib.md5()
14+
for field in (advisory.summary, advisory.affected_packages, advisory.references):
15+
value = json.dumps(field, separators=(",", ":")).encode("utf-8")
16+
checksum.update(value)
17+
advisory.unique_content_id = checksum.hexdigest()
18+
advisory.save()
19+
20+
dependencies = [
21+
("vulnerabilities", "0004_advisory_unique_content_id"),
22+
]
23+
24+
operations = [
25+
migrations.RunPython(md5hash),
26+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 4.0.2 on 2022-03-29 09:41
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("vulnerabilities", "0005_auto_20220329_0938"),
10+
]
11+
12+
operations = [
13+
migrations.AlterUniqueTogether(
14+
name="advisory",
15+
unique_together={("aliases", "unique_content_id", "date_published")},
16+
),
17+
]

vulnerabilities/models.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# Visit https://github.com/nexB/vulnerablecode/ for support and download.
2222

2323
import dataclasses
24+
import hashlib
2425
import importlib
2526
import json
2627
import logging
@@ -326,6 +327,15 @@ class Advisory(models.Model):
326327
into structured data
327328
"""
328329

330+
def save(self, *args, **kwargs):
331+
checksum = hashlib.md5()
332+
for field in (self.summary, self.affected_packages, self.references):
333+
value = json.dumps(field, separators=(",", ":")).encode("utf-8")
334+
checksum.update(value)
335+
self.unique_content_id = checksum.hexdigest()
336+
super().save(*args, **kwargs)
337+
338+
unique_content_id = models.CharField(max_length=32, blank=True, null=True)
329339
aliases = models.JSONField(blank=True, default=list, help_text="A list of alias strings")
330340
summary = models.TextField(blank=True, null=True)
331341
# we use a JSON field here to avoid creating a complete relational model for data that
@@ -356,9 +366,7 @@ class Advisory(models.Model):
356366
class Meta:
357367
unique_together = (
358368
"aliases",
359-
"summary",
360-
"affected_packages",
361-
"references",
369+
"unique_content_id",
362370
"date_published",
363371
)
364372

0 commit comments

Comments
 (0)