-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sha256 uniqueness constraint to CollectionVersion
fixes: #1052
- Loading branch information
Showing
9 changed files
with
206 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
CollectionVersion global uniqueness constraint is now its sha256 digest. Repository level uniqueness | ||
is still (namespace, name, version). |
19 changes: 19 additions & 0 deletions
19
pulp_ansible/app/migrations/0043_collectionversion_sha256.py
This file contains 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,19 @@ | ||
# Generated by Django 3.2.14 on 2022-07-15 22:51 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('ansible', '0042_ansiblerepository_gpgkey'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='collectionversion', | ||
name='sha256', | ||
field=models.CharField(default='', max_length=64), | ||
preserve_default=False, | ||
), | ||
] |
62 changes: 62 additions & 0 deletions
62
pulp_ansible/app/migrations/0044_collectionversion_sha256_migrate.py
This file contains 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,62 @@ | ||
# Generated by Django 3.2.14 on 2022-07-21 22:35 | ||
|
||
from django.db import migrations, transaction | ||
|
||
|
||
def add_sha256_to_current_models(apps, schema_editor): | ||
"""Adds the sha256 to current CollectionVersion models.""" | ||
CollectionVersion = apps.get_model('ansible', 'CollectionVersion') | ||
ContentArtifact = apps.get_model('core', 'ContentArtifact') | ||
collection_bulk = {} | ||
collections_to_update = [] | ||
collections_on_demand = [] | ||
|
||
def find_and_update_sha256(): | ||
# All content have a ContentArtifact | ||
content_artifacts = ContentArtifact.objects.filter(content__in=collection_bulk.keys()).select_related("artifact", "content") | ||
for content_artifact in content_artifacts: | ||
found_collection = collection_bulk[content_artifact.content.pk] | ||
# The ContentArtifact could point to an Artifact or be on-demand | ||
if (artifact := getattr(content_artifact, "artifact")) and artifact.sha256 is not None: | ||
found_collection.sha256 = artifact.sha256 | ||
collections_to_update.append(found_collection) | ||
else: | ||
collections_on_demand.append(found_collection) | ||
collection_bulk.clear() | ||
|
||
for collection_version in CollectionVersion.objects.only("pk", "sha256").iterator(): | ||
if not collection_version.sha256: | ||
collection_bulk[collection_version.pk] = collection_version | ||
if len(collection_bulk) == 1024: | ||
find_and_update_sha256() | ||
if len(collections_to_update) >= 1024: | ||
with transaction.atomic(): | ||
CollectionVersion.objects.bulk_update(collections_to_update, ["sha256",]) | ||
collections_to_update.clear() | ||
# Update remaining collections | ||
if len(collection_bulk) > 0: | ||
find_and_update_sha256() | ||
if len(collections_to_update) > 0: | ||
with transaction.atomic(): | ||
CollectionVersion.objects.bulk_update(collections_to_update, ["sha256",]) | ||
|
||
# If there are on-demand collections then the next migration will fail, so error here with | ||
# helpful message on how to fix. No work will be performed by this migration on a second-run. | ||
if len(collections_on_demand) > 0: | ||
raise Exception( | ||
f"On demand collections found. Please remove or upload/sync their data: " | ||
f"{[c.pk for c in collections_on_demand]}" | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
atomic = False | ||
|
||
dependencies = [ | ||
('ansible', '0043_collectionversion_sha256'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(add_sha256_to_current_models, migrations.RunPython.noop) | ||
] |
22 changes: 22 additions & 0 deletions
22
pulp_ansible/app/migrations/0045_collectionversion_unique_sha256.py
This file contains 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,22 @@ | ||
# Generated by Django 3.2.14 on 2022-07-21 23:05 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('ansible', '0044_collectionversion_sha256_migrate'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='collectionversion', | ||
name='sha256', | ||
field=models.CharField(db_index=True, max_length=64, null=False), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='collectionversion', | ||
unique_together={('sha256',)}, | ||
), | ||
] |
This file contains 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 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 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.