Skip to content

Commit

Permalink
Use bulk_update() to speed up VCID migration #811
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Ombredanne <pombredanne@nexb.com>
  • Loading branch information
pombredanne committed Sep 8, 2022
1 parent 2a70836 commit 7c708e8
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion vulnerabilities/migrations/0023_vcid_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ class Migration(migrations.Migration):

def save_vulnerability_id(apps, schema_editor):
Vulnerabilities = apps.get_model("vulnerabilities", "Vulnerability")
updatables = []
for vulnerability in Vulnerabilities.objects.filter(~Q(vulnerability_id__startswith="VCID-")):
vulnerability.vulnerability_id = build_vcid()
vulnerability.save()
updatables.append(vulnerability)
# update in bulk, 500 at a time
updated = Vulnerabilities.objects.bulk_update(
objs=updatables,
fields=["vulnerability_id"],
batch_size=500,
)
print(f"Migrated {updated} vulnerabilities to new VCID")

operations = [
migrations.RunPython(save_vulnerability_id, migrations.RunPython.noop)
Expand Down

0 comments on commit 7c708e8

Please sign in to comment.