summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Hahler <git@thequod.de>2021-10-01 07:23:57 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-10-01 07:24:45 +0200
commit81bb0ae2213019726a96becb5a1d906924dd5109 (patch)
tree00a2c4b6ba99ffa34ffc9846f498c27691b87722
parent0a49276065575867fbbf279f138b399c535e3b6d (diff)
[4.0.x] Fixed #33160 -- Avoided suppressing query errors in _nodb_cursor() on PostgreSQL.
Backport of 98c8bf1ceeab5c68751c83555f82cff1a9120a67 from main
-rw-r--r--django/db/backends/postgresql/base.py3
-rw-r--r--tests/backends/postgresql/tests.py5
2 files changed, 8 insertions, 0 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
index ad8d85da29..8864a4f543 100644
--- a/django/db/backends/postgresql/base.py
+++ b/django/db/backends/postgresql/base.py
@@ -304,10 +304,13 @@ class DatabaseWrapper(BaseDatabaseWrapper):
@contextmanager
def _nodb_cursor(self):
+ cursor = None
try:
with super()._nodb_cursor() as cursor:
yield cursor
except (Database.DatabaseError, WrappedDatabaseError):
+ if cursor is not None:
+ raise
warnings.warn(
"Normally Django will use a connection to the 'postgres' database "
"to avoid running initialization queries against the production "
diff --git a/tests/backends/postgresql/tests.py b/tests/backends/postgresql/tests.py
index 7f27c69ffb..e8dfcf2f62 100644
--- a/tests/backends/postgresql/tests.py
+++ b/tests/backends/postgresql/tests.py
@@ -103,6 +103,11 @@ class Tests(TestCase):
with connection._nodb_cursor():
pass
+ def test_nodb_cursor_reraise_exceptions(self):
+ with self.assertRaisesMessage(DatabaseError, 'exception'):
+ with connection._nodb_cursor():
+ raise DatabaseError('exception')
+
def test_database_name_too_long(self):
from django.db.backends.postgresql.base import DatabaseWrapper
settings = connection.settings_dict.copy()