Skip to content

Commit 97bfe0c

Browse files
committed
[3.1.x] Fixed #32224 -- Avoided suppressing connection errors in supports_json_field on SQLite.
Regression in 6789ded. Thanks Juan Garcia Alvite for the report. Backport of f5e5aac from master
1 parent a582ef8 commit 97bfe0c

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

django/db/backends/sqlite3/features.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,12 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5454

5555
@cached_property
5656
def supports_json_field(self):
57-
try:
58-
with self.connection.cursor() as cursor, transaction.atomic():
59-
cursor.execute('SELECT JSON(\'{"a": "b"}\')')
60-
except OperationalError:
61-
return False
57+
with self.connection.cursor() as cursor:
58+
try:
59+
with transaction.atomic(self.connection.alias):
60+
cursor.execute('SELECT JSON(\'{"a": "b"}\')')
61+
except OperationalError:
62+
return False
6263
return True
6364

6465
can_introspect_json_field = property(operator.attrgetter('supports_json_field'))

docs/releases/3.1.4.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ Bugfixes
2424

2525
* Fixed a regression in Django 3.1 that caused the incorrect grouping by a
2626
``Q`` object annotation (:ticket:`32200`).
27+
28+
* Fixed a regression in Django 3.1 that caused suppressing connection errors
29+
when :class:`~django.db.models.JSONField` is used on SQLite
30+
(:ticket:`32224`).
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from unittest import mock, skipUnless
2+
3+
from django.db import OperationalError, connection
4+
from django.test import TestCase
5+
6+
7+
@skipUnless(connection.vendor == 'sqlite', 'SQLite tests.')
8+
class FeaturesTests(TestCase):
9+
def test_supports_json_field_operational_error(self):
10+
if hasattr(connection.features, 'supports_json_field'):
11+
del connection.features.supports_json_field
12+
msg = 'unable to open database file'
13+
with mock.patch(
14+
'django.db.backends.base.base.BaseDatabaseWrapper.cursor',
15+
side_effect=OperationalError(msg),
16+
):
17+
with self.assertRaisesMessage(OperationalError, msg):
18+
connection.features.supports_json_field

0 commit comments

Comments
 (0)