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

Fix update_highest_version when versions were uploaded out-of-order #1624

Merged
merged 1 commit into from Oct 23, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/1623.bugfix
@@ -0,0 +1 @@
Fixed highest version calculation failing when versions of a collection were created out of order.
25 changes: 17 additions & 8 deletions pulp_ansible/app/tasks/collections.py
Expand Up @@ -429,14 +429,23 @@ def _update_highest_version(collection_version):

qs = collection_version.collection.versions.annotate(prerelease=Q(version_prerelease__ne=""))
highest_subq = qs.order_by("prerelease", "-version")[0:1].values("pk")
# Order them in such a way, that only the latest updated record will recieve true.
# This should prevent hitting the uniquenes constraint.
update_qs = qs.annotate(new_is_highest=Q(pk=highest_subq)).order_by("-prerelease", "version")
try:
update_qs.update(is_highest=F("new_is_highest"))
except IntegrityError:
# Try once more
update_qs.update(is_highest=F("new_is_highest"))
update_qs = qs.annotate(new_is_highest=Q(pk=highest_subq))
# To avoid ordering issues, set everything to False first, then update with proper value
for i in range(2):
try:
with transaction.atomic():
update_qs.update(is_highest=False)
update_qs.update(is_highest=F("new_is_highest"))
except IntegrityError:
log.debug(f"Retrying update_highest_version {collection_version.relative_path}")
pass
else:
return
log.warning(
_("Failed to update is_highest for {}, potentially out of date").format(
collection_version.relative_path
)
)


class AnsibleDeclarativeVersion(DeclarativeVersion):
Expand Down
2 changes: 1 addition & 1 deletion pulp_ansible/app/tasks/upload.py
Expand Up @@ -81,7 +81,7 @@ def finish_collection_upload(collection_version, tags, origin_repository):
tag, created = Tag.objects.get_or_create(name=name)
collection_version.tags.add(tag)

_update_highest_version(collection_version)
if origin_repository is not None:
collection_version.repository = origin_repository
collection_version.save()
_update_highest_version(collection_version)