diff --git a/CHANGES/3345.misc b/CHANGES/3345.misc new file mode 100644 index 000000000..62a54d3ff --- /dev/null +++ b/CHANGES/3345.misc @@ -0,0 +1 @@ +Simplified subrepo deletion logic to make it more reliable. diff --git a/pulp_rpm/app/migrations/0058_alter_addon_repository_alter_variant_repository.py b/pulp_rpm/app/migrations/0058_alter_addon_repository_alter_variant_repository.py new file mode 100644 index 000000000..72fc5a8e1 --- /dev/null +++ b/pulp_rpm/app/migrations/0058_alter_addon_repository_alter_variant_repository.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.7 on 2023-11-29 23:12 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("core", "0114_remove_task_args_remove_task_kwargs"), + ("rpm", "0057_rpmpublication_checksum_type_and_more"), + ] + + operations = [ + migrations.AlterField( + model_name="addon", + name="repository", + field=models.ForeignKey( + on_delete=django.db.models.deletion.RESTRICT, + related_name="addons", + to="core.repository", + ), + ), + migrations.AlterField( + model_name="variant", + name="repository", + field=models.ForeignKey( + null=True, + on_delete=django.db.models.deletion.RESTRICT, + related_name="variants", + to="core.repository", + ), + ), + ] diff --git a/pulp_rpm/app/models/distribution.py b/pulp_rpm/app/models/distribution.py index cf222e7da..e60de41a9 100644 --- a/pulp_rpm/app/models/distribution.py +++ b/pulp_rpm/app/models/distribution.py @@ -2,7 +2,7 @@ from logging import getLogger from django.db import models -from django.db.models.signals import post_delete +from django.db.models.signals import pre_delete, post_delete from django.dispatch import receiver from pulpcore.plugin.models import ( @@ -107,8 +107,8 @@ def repositories(self): """ from pulp_rpm.app.models import RpmRepository - repo_ids = list(self.addons.values_list("repository__pk", flat=True)) - repo_ids += list(self.variants.values_list("repository__pk", flat=True)) + repo_ids = list(self.addons.values_list("repository_id", flat=True)) + repo_ids += list(self.variants.values_list("repository_id", flat=True)) return RpmRepository.objects.filter(pk__in=repo_ids) def content(self): @@ -255,7 +255,7 @@ class Addon(BaseModel): distribution_tree = models.ForeignKey( DistributionTree, on_delete=models.CASCADE, related_name="addons" ) - repository = models.ForeignKey(Repository, on_delete=models.PROTECT, related_name="addons") + repository = models.ForeignKey(Repository, on_delete=models.RESTRICT, related_name="addons") class Meta: unique_together = ( @@ -317,7 +317,7 @@ class Variant(BaseModel): DistributionTree, on_delete=models.CASCADE, related_name="variants" ) repository = models.ForeignKey( - Repository, on_delete=models.PROTECT, related_name="variants", null=True + Repository, on_delete=models.RESTRICT, related_name="variants", null=True ) class Meta: @@ -331,19 +331,19 @@ class Meta: ) -@receiver(post_delete, sender=Addon) -@receiver(post_delete, sender=Variant) -def cleanup_subrepos(sender, instance, **kwargs): +@receiver(pre_delete, sender=DistributionTree) +def setup_cleanup_subrepos(sender, instance, **kwargs): + """ + Find subrepos when a DistributionTree is being removed. """ - Remove subrepos when a DistributionTree is being removed. + repos = instance.repositories() + instance.repositories = lambda: repos.all() + +@receiver(post_delete, sender=DistributionTree) +def cleanup_subrepos(sender, instance, **kwargs): + """ + Delete the found subrepos of the deleted DistributionTree. """ - subrepo = None - try: - subrepo = instance.repository - except Repository.DoesNotExist: - pass - if subrepo: - Variant.objects.filter(repository=subrepo).delete() - Addon.objects.filter(repository=subrepo).delete() - subrepo.delete() + repos = instance.repositories() + repos.delete()