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

Remove subrepos during distribution tree removal. #1836

Merged
merged 1 commit into from Sep 23, 2020

Conversation

goosemania
Copy link
Member

Cleanup orphaned subrepos in a migration.

closes #7440
https://pulp.plan.io/issues/7440

@pulpbot
Copy link
Member

pulpbot commented Sep 3, 2020

Attached issue: https://pulp.plan.io/issues/7440

@@ -328,3 +331,21 @@ class Meta:
"packages",
"distribution_tree",
)


@receiver(pre_delete, sender=DistributionTree)
Copy link
Member Author

@goosemania goosemania Sep 3, 2020

Choose a reason for hiding this comment

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

  • I can't override the Model.delete method because it's not called for bulk or queryset removals.
  • To use pre-delete, I had to remove on_delete=PROTECT for repositories, otherwise I can't remove them before DistributionTree itself. It's bad, PROTECT there is not to remove subrepos without a DistributionTree.
  • To use post_delete, I'd need to remove on_delete=CASCADE for variants and addons, otherwise, they are already removed and I don't have information which subrepos to remove.
  • Alternatively, to use post_delete, I could lookup all the subrepos without addon/variant reference. But it would be searching all the rpm repos through backward reference for every distribution tree removal.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe consider using django-lifecycle hook. As of 3.6, all models can use this https://rsinger86.github.io/django-lifecycle/

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll read more about it. Does it solve the problem or order and cascade/protect rules?

Copy link
Member

Choose a reason for hiding this comment

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

It does not, it's mainly a syntactical sugar

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in the next push

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm taking that back :)

with this patch:

diff --git a/pulp_rpm/app/models/distribution.py b/pulp_rpm/app/models/distribution.py
index 988ef37d..14be224c 100644
--- a/pulp_rpm/app/models/distribution.py
+++ b/pulp_rpm/app/models/distribution.py
@@ -3,8 +3,11 @@ from logging import getLogger
 
 from django.db import models
 from django.db import transaction
-from django.db.models.signals import post_delete
-from django.dispatch import receiver
+
+from django_lifecycle import (
+    AFTER_DELETE,
+    hook,
+)
 
 from pulpcore.plugin.models import (
     Artifact,
@@ -91,6 +94,24 @@ class DistributionTree(Content):
     discnum = models.IntegerField(null=True)
     totaldiscs = models.IntegerField(null=True)
 
+    @hook(AFTER_DELETE)
+    def cleanup_subrepos(self):
+        """
+        Remove subrepos when a DistributionTree is being removed.
+
+        """
+        with transaction.atomic():
+            for variant in self.variants.all():
+                subrepo = variant.repository
+                variant.delete()
+                if subrepo:
+                    subrepo.delete()
+            for addon in self.addons.all():
+                subrepo = addon.repository
+                addon.delete()
+                if subrepo:
+                    subrepo.delete()
+
     def repositories(self):
         """
         Return the subrepos in this DistributionTree.
@@ -331,22 +352,3 @@ class Variant(BaseModel):
             "packages",
             "distribution_tree",
         )
-
-
-@receiver(post_delete, sender=DistributionTree)
-def cleanup_subrepos(sender, instance, **kwargs):
-    """
-    Remove subrepos when a DistributionTree is being removed.
-
-    """
-    with transaction.atomic():
-        for variant in instance.variants.all():
-            subrepo = variant.repository
-            variant.delete()
-            if subrepo:
-                subrepo.delete()
-        for addon in instance.addons.all():
-            subrepo = addon.repository
-            addon.delete()
-            if subrepo:
-                subrepo.delete()

I'm getting django.db.utils.IntegrityError: update or delete on table "rpm_distributiontree" violates foreign key constraint "rpm_addon_distribution_tree_id_91a3fbf6_fk_rpm_distr" on table "rpm_addon"

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm leaning towards keeping the signal. Django lifecycle doesn't seem to be just a syntactic sugar, it seem not to use signals at all https://github.com/rsinger86/django-lifecycle/blob/master/django_lifecycle/mixins.py#L138-L141

@goosemania
Copy link
Member Author

An alternative approach from #django channel which @bmbouter shared is to override Model.delete() and Queryset.delete().

@dralley
Copy link
Contributor

dralley commented Sep 8, 2020

Needs a changelog entry, but LGTM pending CI results.

Cleanup orphaned subrepos in a migration.

closes #7440
https://pulp.plan.io/issues/7440
[nocoverage]
@goosemania
Copy link
Member Author

I tried the Model.delete() and QuerySet.delete() approach and I learned that when Django performs CASCADE delete the .delete() methods are not called. https://code.djangoproject.com/ticket/29398

So it seems like signals is the way.

I improved the original approach I believe. I brought back the CASCADE for variants and addons and have simpler subrepo removal code.

Let me know what you all think.

Copy link
Contributor

@dralley dralley left a comment

Choose a reason for hiding this comment

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

Seems reasonable

@goosemania
Copy link
Member Author

I'm merging to unblock the release.
If anyone has any reservations for the chosen approach, we can refactor any time later.

@goosemania goosemania merged commit 9e36af6 into pulp:master Sep 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants