From f5e5aac59ebbcea46b98d37834915de0f43d7cc8 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Wed, 25 Nov 2020 12:19:45 +0100 Subject: [PATCH] Fixed #32224 -- Avoided suppressing connection errors in supports_json_field on SQLite.` Regression in 6789ded0a6ab797f0dcdfa6ad5d1cfa46e23abcd. Thanks Juan Garcia Alvite for the report. --- django/db/backends/sqlite3/features.py | 11 ++++++----- docs/releases/3.1.4.txt | 4 ++++ tests/backends/sqlite/test_features.py | 18 ++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 tests/backends/sqlite/test_features.py diff --git a/django/db/backends/sqlite3/features.py b/django/db/backends/sqlite3/features.py index 59b8d6862f..5ab3a00914 100644 --- a/django/db/backends/sqlite3/features.py +++ b/django/db/backends/sqlite3/features.py @@ -70,11 +70,12 @@ class DatabaseFeatures(BaseDatabaseFeatures): @cached_property def supports_json_field(self): - try: - with self.connection.cursor() as cursor, transaction.atomic(): - cursor.execute('SELECT JSON(\'{"a": "b"}\')') - except OperationalError: - return False + with self.connection.cursor() as cursor: + try: + with transaction.atomic(self.connection.alias): + cursor.execute('SELECT JSON(\'{"a": "b"}\')') + except OperationalError: + return False return True can_introspect_json_field = property(operator.attrgetter('supports_json_field')) diff --git a/docs/releases/3.1.4.txt b/docs/releases/3.1.4.txt index 4d9e949791..080db4f290 100644 --- a/docs/releases/3.1.4.txt +++ b/docs/releases/3.1.4.txt @@ -24,3 +24,7 @@ Bugfixes * Fixed a regression in Django 3.1 that caused the incorrect grouping by a ``Q`` object annotation (:ticket:`32200`). + +* Fixed a regression in Django 3.1 that caused suppressing connection errors + when :class:`~django.db.models.JSONField` is used on SQLite + (:ticket:`32224`). diff --git a/tests/backends/sqlite/test_features.py b/tests/backends/sqlite/test_features.py new file mode 100644 index 0000000000..9b74794408 --- /dev/null +++ b/tests/backends/sqlite/test_features.py @@ -0,0 +1,18 @@ +from unittest import mock, skipUnless + +from django.db import OperationalError, connection +from django.test import TestCase + + +@skipUnless(connection.vendor == 'sqlite', 'SQLite tests.') +class FeaturesTests(TestCase): + def test_supports_json_field_operational_error(self): + if hasattr(connection.features, 'supports_json_field'): + del connection.features.supports_json_field + msg = 'unable to open database file' + with mock.patch( + 'django.db.backends.base.base.BaseDatabaseWrapper.cursor', + side_effect=OperationalError(msg), + ): + with self.assertRaisesMessage(OperationalError, msg): + connection.features.supports_json_field