mirror of
https://github.com/django/django.git
synced 2024-12-22 17:16:24 +00:00
Made select_for_update() don't raise TransactionManagementError on databases that don't support transactions.
This commit is contained in:
parent
db83ac48d4
commit
a0bd006306
@ -653,7 +653,12 @@ class SQLCompiler:
|
|||||||
params.extend(f_params)
|
params.extend(f_params)
|
||||||
|
|
||||||
if self.query.select_for_update and features.has_select_for_update:
|
if self.query.select_for_update and features.has_select_for_update:
|
||||||
if self.connection.get_autocommit():
|
if (
|
||||||
|
self.connection.get_autocommit()
|
||||||
|
# Don't raise an exception when database doesn't
|
||||||
|
# support transactions, as it's a noop.
|
||||||
|
and features.supports_transactions
|
||||||
|
):
|
||||||
raise TransactionManagementError(
|
raise TransactionManagementError(
|
||||||
"select_for_update cannot be used outside of a transaction."
|
"select_for_update cannot be used outside of a transaction."
|
||||||
)
|
)
|
||||||
|
@ -32,19 +32,11 @@ class ExplainTests(TestCase):
|
|||||||
for idx, queryset in enumerate(querysets):
|
for idx, queryset in enumerate(querysets):
|
||||||
for format in all_formats:
|
for format in all_formats:
|
||||||
with self.subTest(format=format, queryset=idx):
|
with self.subTest(format=format, queryset=idx):
|
||||||
with CaptureQueriesContext(connection) as captured_queries:
|
with self.assertNumQueries(1) as captured_queries:
|
||||||
if queryset.query.select_for_update:
|
result = queryset.explain(format=format)
|
||||||
with transaction.atomic():
|
|
||||||
result = queryset.explain(format=format)
|
|
||||||
else:
|
|
||||||
result = queryset.explain(format=format)
|
|
||||||
self.assertEqual(len(captured_queries), 1)
|
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
any(
|
captured_queries[0]["sql"].startswith(
|
||||||
captured_query["sql"].startswith(
|
connection.ops.explain_prefix
|
||||||
connection.ops.explain_prefix
|
|
||||||
)
|
|
||||||
for captured_query in captured_queries
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
self.assertIsInstance(result, str)
|
self.assertIsInstance(result, str)
|
||||||
|
@ -491,7 +491,7 @@ class SelectForUpdateTests(TransactionTestCase):
|
|||||||
str(Person.objects.filter(name="foo").select_for_update().query),
|
str(Person.objects.filter(name="foo").select_for_update().query),
|
||||||
)
|
)
|
||||||
|
|
||||||
@skipUnlessDBFeature("has_select_for_update")
|
@skipUnlessDBFeature("has_select_for_update", "supports_transactions")
|
||||||
def test_for_update_requires_transaction(self):
|
def test_for_update_requires_transaction(self):
|
||||||
"""
|
"""
|
||||||
A TransactionManagementError is raised
|
A TransactionManagementError is raised
|
||||||
@ -501,7 +501,7 @@ class SelectForUpdateTests(TransactionTestCase):
|
|||||||
with self.assertRaisesMessage(transaction.TransactionManagementError, msg):
|
with self.assertRaisesMessage(transaction.TransactionManagementError, msg):
|
||||||
list(Person.objects.select_for_update())
|
list(Person.objects.select_for_update())
|
||||||
|
|
||||||
@skipUnlessDBFeature("has_select_for_update")
|
@skipUnlessDBFeature("has_select_for_update", "supports_transactions")
|
||||||
def test_for_update_requires_transaction_only_in_execution(self):
|
def test_for_update_requires_transaction_only_in_execution(self):
|
||||||
"""
|
"""
|
||||||
No TransactionManagementError is raised
|
No TransactionManagementError is raised
|
||||||
|
Loading…
Reference in New Issue
Block a user