-
-
Notifications
You must be signed in to change notification settings - Fork 302
Migrate Advisory aliases field to M2M relationship #1784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
keshav-space
merged 14 commits into
main
from
convert-advisory-alias-to-concrete-relation
Mar 26, 2025
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4f6ffe8
Migrate Advisory aliases field to M2M relationship
keshav-space e9e0273
Make Alias vulnerability field optional
keshav-space 0ba0593
Use advisory alias relation in pipelines and importer
keshav-space 6cc5bd3
Update the test fixture to use the new alias field
keshav-space 801f871
Remove unused include_metadata parameter
keshav-space 05ba61f
Make unique_content_id required field
keshav-space 996da13
Update tests to support latest advisory model changes
keshav-space 04cab61
Provide content id when inserting advisory
keshav-space 6963158
Ensure reference_id is always a string
keshav-space 02d75b2
Add test for get_or_create_aliases
keshav-space 16994eb
Use iterator in vulnerability_status improver
keshav-space b3d5fc5
Use unique_content_id for get_or_create Advisory
keshav-space eeee06e
Optimize alias migration using AdvisoryRelatedAlias
keshav-space d86e3c2
Merge remote-tracking branch 'origin/main' into convert-advisory-alia…
keshav-space File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
vulnerabilities/migrations/0090_migrate_advisory_aliases.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| # | ||
| # Copyright (c) nexB Inc. and others. All rights reserved. | ||
| # VulnerableCode is a trademark of nexB Inc. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # See http://www.apache.org/licenses/LICENSE-2.0 for the license text. | ||
| # See https://github.com/aboutcode-org/vulnerablecode for support or download. | ||
| # See https://aboutcode.org for more information about nexB OSS projects. | ||
| # | ||
|
|
||
| from aboutcode.pipeline import LoopProgress | ||
| from django.db import migrations | ||
| from django.db import models | ||
| import django.db.models.deletion | ||
|
|
||
| """ | ||
| Model and data migration for converting the Advisory aliases | ||
| JSON field to a concrete M2M Advisory Alias relationship. | ||
| """ | ||
|
|
||
| def bulk_update(model, items, fields, logger): | ||
| item_count = 0 | ||
| if items: | ||
| try: | ||
| model.objects.bulk_update(objs=items, fields=fields) | ||
| item_count += len(items) | ||
| except Exception as e: | ||
| logger(f"Error updating Advisory: {e}") | ||
| items.clear() | ||
| return item_count | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("vulnerabilities", "0089_alter_advisory_unique_content_id"), | ||
| ] | ||
|
|
||
| def populate_new_advisory_aliases_field(apps, schema_editor): | ||
| Advisory = apps.get_model("vulnerabilities", "Advisory") | ||
| Alias = apps.get_model("vulnerabilities", "Alias") | ||
| advisories = Advisory.objects.all() | ||
|
|
||
| chunk_size = 10000 | ||
| advisories_count = advisories.count() | ||
| print(f"\nPopulate new advisory aliases relationship.") | ||
| progress = LoopProgress( | ||
| total_iterations=advisories_count, | ||
| logger=print, | ||
| progress_step=1, | ||
| ) | ||
| for advisory in progress.iter(advisories.iterator(chunk_size=chunk_size)): | ||
| aliases = Alias.objects.filter(alias__in=advisory.old_aliases) | ||
| advisory.aliases.set(aliases) | ||
|
|
||
| def reverse_populate_new_advisory_aliases_field(apps, schema_editor): | ||
| Advisory = apps.get_model("vulnerabilities", "Advisory") | ||
| advisories = Advisory.objects.all() | ||
|
|
||
| updated_advisory_count = 0 | ||
| batch_size = 10000 | ||
| chunk_size = 10000 | ||
| updated_advisory = [] | ||
| progress = LoopProgress( | ||
| total_iterations=advisories.count(), | ||
| logger=print, | ||
| progress_step=1, | ||
| ) | ||
| for advisory in progress.iter(advisories.iterator(chunk_size=chunk_size)): | ||
| aliases = advisory.aliases.all() | ||
| advisory.old_aliases = [alias.alias for alias in aliases] | ||
| updated_advisory.append(advisory) | ||
|
|
||
| if len(updated_advisory) > batch_size: | ||
| updated_advisory_count += bulk_update( | ||
| model=Advisory, | ||
| items=updated_advisory, | ||
| fields=["old_aliases"], | ||
| logger=print, | ||
| ) | ||
|
|
||
| updated_advisory_count += bulk_update( | ||
| model=Advisory, | ||
| items=updated_advisory, | ||
| fields=["old_aliases"], | ||
| logger=print, | ||
| ) | ||
|
|
||
| operations = [ | ||
| migrations.AlterField( | ||
| model_name="advisory", | ||
| name="unique_content_id", | ||
| field=models.CharField( | ||
| help_text="A 64 character unique identifier for the content of the advisory since we use sha256 as hex", | ||
| max_length=64, | ||
| blank=False, | ||
| null=False, | ||
| ), | ||
| ), | ||
|
|
||
| # Make vulnerability relation optional | ||
| migrations.AlterField( | ||
| model_name="alias", | ||
| name="vulnerability", | ||
| field=models.ForeignKey( | ||
| blank=True, | ||
| null=True, | ||
| on_delete=django.db.models.deletion.SET_NULL, | ||
| related_name="aliases", | ||
| to="vulnerabilities.vulnerability", | ||
| ), | ||
| ), | ||
|
|
||
| # Rename aliases field to old_aliases | ||
| migrations.AlterModelOptions( | ||
| name="advisory", | ||
| options={"ordering": ["date_published", "unique_content_id"]}, | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name="advisory", | ||
| unique_together={("unique_content_id", "date_published", "url")}, | ||
| ), | ||
| migrations.RenameField( | ||
| model_name="advisory", | ||
| old_name="aliases", | ||
| new_name="old_aliases", | ||
| ), | ||
| migrations.AddField( | ||
| model_name="advisory", | ||
| name="aliases", | ||
| field=models.ManyToManyField(related_name="advisories", to="vulnerabilities.alias"), | ||
| ), | ||
| # Populate the new M2M aliases relation | ||
| migrations.RunPython( | ||
| code=populate_new_advisory_aliases_field, | ||
| reverse_code=reverse_populate_new_advisory_aliases_field, | ||
| ), | ||
| # Delete JSON aliases field | ||
| migrations.RemoveField( | ||
| model_name="advisory", | ||
| name="old_aliases", | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.