Skip to content

Commit

Permalink
Ensure collection deletion is done in one repository version
Browse files Browse the repository at this point in the history
fixes: #1274
Required PR: pulp/pulpcore#3300
  • Loading branch information
gerrod3 committed Oct 18, 2022
1 parent df4be5f commit 3250815
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGES/1274.bugfix
@@ -0,0 +1 @@
Fixed unnecessary creation of intermediate repository versions when performing a collection delete.
22 changes: 13 additions & 9 deletions pulp_ansible/app/tasks/deletion.py
Expand Up @@ -14,6 +14,7 @@
"""

import logging
from collections import defaultdict

from pulp_ansible.app.models import Collection, CollectionVersion
from pulpcore.plugin.tasking import add_and_remove, orphan_cleanup
Expand All @@ -27,10 +28,14 @@ def _cleanup_old_versions(repo):
version.delete()


def _remove_collection_version_from_repos(collection_version):
"""Remove CollectionVersion from latest RepositoryVersion of each repo."""
for repo in collection_version.repositories.all():
add_and_remove(repo.pk, add_content_units=[], remove_content_units=[collection_version.pk])
def _remove_collection_version_from_repos(collection_versions):
"""Remove CollectionVersions from latest RepositoryVersion of each repo."""
repos_with_collections_to_delete = defaultdict(list)
for collection_version in collection_versions:
for repo in collection_version.repositories.all():
repos_with_collections_to_delete[repo].append(collection_version.pk)
for repo, collections in repos_with_collections_to_delete.items():
add_and_remove(repo.pk, add_content_units=[], remove_content_units=collections)
_cleanup_old_versions(repo)


Expand All @@ -45,7 +50,7 @@ def delete_collection_version(collection_version_pk):
collection_version = CollectionVersion.objects.get(pk=collection_version_pk)
collection = collection_version.collection

_remove_collection_version_from_repos(collection_version)
_remove_collection_version_from_repos([collection_version])

log.info("Running orphan_cleanup to delete CollectionVersion object and artifact")
# Running orphan_protection_time=0 should be safe since we're specifying the content
Expand All @@ -67,10 +72,9 @@ def delete_collection(collection_pk):
3. Delete Collection
"""
collection = Collection.objects.get(pk=collection_pk)
version_pks = []
for version in collection.versions.all():
_remove_collection_version_from_repos(version)
version_pks.append(version.pk)
versions = collection.versions.all()
_remove_collection_version_from_repos(versions)
version_pks = versions.values_list("pk", flat=True)

log.info("Running orphan_cleanup to delete CollectionVersion objects and artifacts")
# Running orphan_protection_time=0 should be safe since we're specifying the content
Expand Down
Expand Up @@ -36,7 +36,8 @@ def test_collection_deletion(self):
name=self.collection_name,
namespace=self.collection_namespace,
)
monitor_task(resp.task)
task = monitor_task(resp.task)
assert len(task.created_resources) == 1

collections = self.collections_v3api.list(self.distribution.base_path)
assert collections.meta.count == 0
Expand Down

0 comments on commit 3250815

Please sign in to comment.