Skip to content

Commit

Permalink
Fix error when deleting observer subsciption
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorjerse committed Jan 13, 2025
1 parent ccf5793 commit 074c4a1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit 074c4a1

Please sign in to comment.