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

Fix error when deleting observer subscription #1189

Merged
merged 1 commit into from
Jan 15, 2025
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
9 changes: 9 additions & 0 deletions docs/CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ All notable changes to this project are documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.


==========
Unreleased
==========

Fixed
-----
- Fix error when deleting observer subscription


===================
42.2.0 - 2025-01-13
===================
Expand Down
8 changes: 6 additions & 2 deletions resolwe/observers/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,16 +309,20 @@ def subscribe(
)
self.observers.add(observer)

@transaction.atomic
def delete(self):
"""Delete the given subscription.

Delete all observers with no remaining subscriptions.

This method is marked atomic, so no new subsciptions are added to the observers
we are deleting in the meantime causing IntegrityError.
"""
# Find related observers with only one remaining subscription
# (it must be this one) and delete them first.
# First find observers with only this subscription and delete them.
Observer.objects.annotate(subs=Count("subscriptions")).filter(
subscriptions=self.pk, subs=1
).delete()
# Now delete the subscription itself.
super().delete()

@classmethod
Expand Down
Loading