Skip to content
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

Ensure collection deletion is done in one repository version #1275

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1274.bugfix
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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