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
Conversation
|
Attached issue: https://pulp.plan.io/issues/7440 |
a54584a
to
a329871
Compare
pulp_rpm/app/models/distribution.py
Outdated
| @@ -328,3 +331,21 @@ class Meta: | |||
| "packages", | |||
| "distribution_tree", | |||
| ) | |||
|
|
|||
|
|
|||
| @receiver(pre_delete, sender=DistributionTree) | |||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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/
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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"
There was a problem hiding this comment.
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
a329871
to
07c0786
Compare
|
An alternative approach from #django channel which @bmbouter shared is to override Model.delete() and Queryset.delete(). |
|
Needs a changelog entry, but LGTM pending CI results. |
07c0786
to
74c8211
Compare
74c8211
to
def3d00
Compare
Cleanup orphaned subrepos in a migration. closes #7440 https://pulp.plan.io/issues/7440 [nocoverage]
def3d00
to
d2dd79d
Compare
|
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable
|
I'm merging to unblock the release. |
Cleanup orphaned subrepos in a migration.
closes #7440
https://pulp.plan.io/issues/7440