Skip to content

Commit e927770

Browse files
authored
Fix atomic transaction not routing to the the correct DB (#324)
* Fix atomic transaction not routing to the the correct DB * Added test case for atomic transaction to ensure its using the correct db * simplify the test * lint
1 parent 16a18d9 commit e927770

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

django_celery_results/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ def get_all_expired(self, expires):
8787

8888
def delete_expired(self, expires):
8989
"""Delete all expired results."""
90-
with transaction.atomic():
90+
with transaction.atomic(using=self.db):
9191
raw_delete(queryset=self.get_all_expired(expires))
9292

9393

t/unit/test_models.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,32 @@ class TransactionError(Exception):
209209
raise TransactionError()
210210
except TransactionError:
211211
pass
212+
213+
214+
@pytest.mark.usefixtures('depends_on_current_app')
215+
class test_ModelsWithoutDefaultDB(TransactionTestCase):
216+
"""
217+
This class to ensure all operations are done on the
218+
same db we use and dont leak accidentally into another
219+
db. we dont include the default db in databases as by
220+
default an incorrect behavior would route there and
221+
would not be detectable.
222+
223+
The tests will fail with the below error incase we
224+
try to interact from a db other than the one we have
225+
specified.
226+
227+
`AssertionError: Database connections to 'default' are
228+
not allowed in this test`
229+
"""
230+
231+
non_default_test_db = 'secondary'
232+
databases = [non_default_test_db]
233+
234+
def test_operations_with_atomic_transactions(self):
235+
TaskResult.objects.db_manager(
236+
self.non_default_test_db
237+
).delete_expired(expires=10)
238+
GroupResult.objects.db_manager(
239+
self.non_default_test_db
240+
).delete_expired(expires=10)

0 commit comments

Comments
 (0)