Skip to content

Commit

Permalink
Simplify subrepo deletion
Browse files Browse the repository at this point in the history
fixes: #3345
  • Loading branch information
gerrod3 committed Dec 4, 2023
1 parent 7e4dc1a commit 53f782d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGES/3345.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Simplified subrepo deletion logic to make it more reliable.
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
25 changes: 8 additions & 17 deletions pulp_rpm/app/models/distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
from django.dispatch import receiver

from pulpcore.plugin.models import (
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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 = (
Expand Down Expand Up @@ -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:
Expand All @@ -331,19 +331,10 @@ class Meta:
)


@receiver(post_delete, sender=Addon)
@receiver(post_delete, sender=Variant)
@receiver(pre_delete, sender=DistributionTree)
def cleanup_subrepos(sender, instance, **kwargs):
"""
Remove subrepos when a DistributionTree is being removed.
"""
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()

0 comments on commit 53f782d

Please sign in to comment.