Code

Ticket #6669: 6669-2.diff

File 6669-2.diff, 2.4 KB (added by claudep, 2 years ago)

Set connection dirty even with CursorDebugWrapper

Line 
1diff --git a/django/db/backends/util.py b/django/db/backends/util.py
2index b0463b7..56e350f 100644
3--- a/django/db/backends/util.py
4+++ b/django/db/backends/util.py
5@@ -16,9 +16,12 @@ class CursorWrapper(object):
6         self.cursor = cursor
7         self.db = db
8 
9-    def __getattr__(self, attr):
10+    def _set_dirty(self):
11         if self.db.is_managed():
12             self.db.set_dirty()
13+
14+    def __getattr__(self, attr):
15+        self._set_dirty()
16         if attr in self.__dict__:
17             return self.__dict__[attr]
18         else:
19@@ -31,6 +34,7 @@ class CursorWrapper(object):
20 class CursorDebugWrapper(CursorWrapper):
21 
22     def execute(self, sql, params=()):
23+        self._set_dirty()
24         start = time()
25         try:
26             return self.cursor.execute(sql, params)
27@@ -47,6 +51,7 @@ class CursorDebugWrapper(CursorWrapper):
28             )
29 
30     def executemany(self, sql, param_list):
31+        self._set_dirty()
32         start = time()
33         try:
34             return self.cursor.executemany(sql, param_list)
35diff --git a/tests/regressiontests/transactions_regress/tests.py b/tests/regressiontests/transactions_regress/tests.py
36index 22f1b0f..5907c3a 100644
37--- a/tests/regressiontests/transactions_regress/tests.py
38+++ b/tests/regressiontests/transactions_regress/tests.py
39@@ -1,9 +1,11 @@
40-from __future__ import absolute_import
41+from __future__ import absolute_import, with_statement
42 
43 from django.core.exceptions import ImproperlyConfigured
44 from django.db import connection, transaction
45 from django.db.transaction import commit_on_success, commit_manually, TransactionManagementError
46+from django.db.utils import DatabaseError
47 from django.test import TransactionTestCase, skipUnlessDBFeature
48+from django.test.utils import override_settings
49 from django.utils.unittest import skipIf
50 
51 from .models import Mod, M2mA, M2mB
52@@ -166,6 +168,14 @@ class TestTransactionClosing(TransactionTestCase):
53         except:
54             self.fail("A transaction consisting of a failed operation was not closed.")
55 
56+        # Same test with DebugCursor
57+        with override_settings(DEBUG=True):
58+            self.assertRaises(Exception, create_system_user)
59+            try:
60+                self.assertEqual(User.objects.count(), 1)
61+            except DatabaseError:
62+                self.fail("A transaction consisting of a failed operation was not closed.")
63+
64 
65 class TestManyToManyAddTransaction(TransactionTestCase):
66     def test_manyrelated_add_commit(self):