Skip to content

Commit 0d4419d

Browse files
committed
Work around PostgreSQL index issue Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 33e083c commit 0d4419d

5 files changed

Lines changed: 527 additions & 3 deletions

File tree

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-28 19:29
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),
18+
),
19+
]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Generated by Django 4.0.2 on 2022-03-28 19:29
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+
hash = ""
14+
if advisory.summary:
15+
hash = hashlib.md5(advisory.summary.encode("utf-8")).hexdigest()
16+
json_fields = [advisory.affected_packages, advisory.references]
17+
# Case 1: a ="Hello" and b="World"
18+
# Case 2: a ="HelloWo" and b="rld"
19+
# hash(a) + hash(b) for both the cases will give same result
20+
# hash( hash(a) + hash(b) ) is better than hash(a + b)
21+
for json_field in json_fields:
22+
if json_field:
23+
hash = hashlib.md5(
24+
(
25+
hash + hashlib.md5(json.dumps(json_field).encode("utf-8")).hexdigest()
26+
).encode("utf-8")
27+
).hexdigest()
28+
advisory.unique_content_id = hash
29+
advisory.save()
30+
31+
dependencies = [
32+
("vulnerabilities", "0004_advisory_unique_content_id"),
33+
]
34+
35+
operations = [
36+
migrations.RunPython(md5hash),
37+
]
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-28 19:34
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("vulnerabilities", "0005_auto_20220328_1929"),
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: 22 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,26 @@ class Advisory(models.Model):
326327
into structured data
327328
"""
328329

330+
def save(self, *args, **kwargs):
331+
hash = ""
332+
if self.summary:
333+
hash = hashlib.md5(self.summary.encode("utf-8")).hexdigest()
334+
json_fields = [self.affected_packages, self.references]
335+
# Case 1: a ="Hello" and b="World"
336+
# Case 2: a ="HelloWo" and b="rld"
337+
# hash(a) + hash(b) for both the cases will give same result
338+
# therefore hash( hash(a) + hash(b) ) is better than hash(a + b)
339+
for json_field in json_fields:
340+
if json_field:
341+
hash = hashlib.md5(
342+
(hash + hashlib.md5(json.dumps(json_field).encode("utf-8")).hexdigest()).encode(
343+
"utf-8"
344+
)
345+
).hexdigest()
346+
self.unique_content_id = hash
347+
super(Advisory, self).save(*args, **kwargs)
348+
349+
unique_content_id = models.CharField(max_length=32, blank=True)
329350
aliases = models.JSONField(blank=True, default=list, help_text="A list of alias strings")
330351
summary = models.TextField(blank=True, null=True)
331352
# we use a JSON field here to avoid creating a complete relational model for data that
@@ -356,9 +377,7 @@ class Advisory(models.Model):
356377
class Meta:
357378
unique_together = (
358379
"aliases",
359-
"summary",
360-
"affected_packages",
361-
"references",
380+
"unique_content_id",
362381
"date_published",
363382
)
364383

0 commit comments

Comments
 (0)