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

Simplify subrepo deletion #3343

Merged
merged 1 commit into from
Dec 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
),
),
]
36 changes: 18 additions & 18 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, post_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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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,19 @@ class Meta:
)


@receiver(post_delete, sender=Addon)
@receiver(post_delete, sender=Variant)
def cleanup_subrepos(sender, instance, **kwargs):
@receiver(pre_delete, sender=DistributionTree)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

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()
Loading