From 7d4c31855b7ab21ba7ee6dbf059e33d82733e80f Mon Sep 17 00:00:00 2001 From: Hao Yu Date: Thu, 3 Oct 2024 14:57:49 +1000 Subject: [PATCH] Fix ansible collections import error Avoid "duplicate key value violates unique" error when importing ansible collections. fixes #1986 --- CHANGES/1986.bugfix | 1 + pulp_ansible/app/modelresource.py | 13 +++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 CHANGES/1986.bugfix diff --git a/CHANGES/1986.bugfix b/CHANGES/1986.bugfix new file mode 100644 index 000000000..f35d3afaa --- /dev/null +++ b/CHANGES/1986.bugfix @@ -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. diff --git a/pulp_ansible/app/modelresource.py b/pulp_ansible/app/modelresource.py index 06fd51319..4ac1b0d02 100644 --- a/pulp_ansible/app/modelresource.py +++ b/pulp_ansible/app/modelresource.py @@ -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.