Skip to content

Commit

Permalink
Fix ansible collections import error
Browse files Browse the repository at this point in the history
Avoid "duplicate key value violates unique" error
when importing ansible collections.

fixes pulp#1986
  • Loading branch information
hao-yu committed Oct 3, 2024
1 parent cae6721 commit 7d4c318
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/1986.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Find and set the current highest collection version in the database to False before importing the new highest version to prevent conflict.
13 changes: 13 additions & 0 deletions pulp_ansible/app/modelresource.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,19 @@ def before_import_row(self, row, **kwargs):
col = Collection.objects.get(name=row["name"], namespace=row["namespace"])
row["collection"] = str(col.pk)

def get_instance(self, instance_loader, row):
"""
This fixes https://github.com/pulp/pulp_ansible/issues/1986
Find and set the current highest version to False before importing the new highest version.
"""
instance = super().get_instance(instance_loader, row)
if not instance and row["is_highest"] == "1":
CollectionVersion.objects.filter(
collection_id=row["collection"], is_highest=True
).update(is_highest=False)

return instance

def set_up_queryset(self):
"""
:return: CollectionVersion content specific to a specified repo-version.
Expand Down

0 comments on commit 7d4c318

Please sign in to comment.