diff options
| author | Jon Dufresne <jon.dufresne@gmail.com> | 2020-02-03 19:07:00 -0800 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-02-06 15:29:38 +0100 |
| commit | f48f671223a20b161ca819cf7d6298e43b8ba5fe (patch) | |
| tree | bc24e2f0ae10a4ffd088141e2681b45173c08b53 /django/db/backends/postgresql | |
| parent | 2d55cb5c4a9c736d04290c9f99ebcbe9e196ac97 (diff) | |
Refs #31233 -- Changed DatabaseWrapper._nodb_connection to _nodb_cursor().
It is now a method instead of a property and returns a context manager
that yields a cursor on entry and closes the cursor and connection upon
exit.
Diffstat (limited to 'django/db/backends/postgresql')
| -rw-r--r-- | django/db/backends/postgresql/base.py | 17 | ||||
| -rw-r--r-- | django/db/backends/postgresql/creation.py | 2 |
2 files changed, 12 insertions, 7 deletions
diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 12b969142e..0f45ca93e1 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -7,6 +7,7 @@ Requires psycopg 2: https://www.psycopg.org/ import asyncio import threading import warnings +from contextlib import contextmanager from django.conf import settings from django.core.exceptions import ImproperlyConfigured @@ -288,11 +289,11 @@ class DatabaseWrapper(BaseDatabaseWrapper): else: return True - @property - def _nodb_connection(self): - nodb_connection = super()._nodb_connection + @contextmanager + def _nodb_cursor(self): try: - nodb_connection.ensure_connection() + with super()._nodb_cursor() as cursor: + yield cursor except (Database.DatabaseError, WrappedDatabaseError): warnings.warn( "Normally Django will use a connection to the 'postgres' database " @@ -304,11 +305,15 @@ class DatabaseWrapper(BaseDatabaseWrapper): ) for connection in connections.all(): if connection.vendor == 'postgresql' and connection.settings_dict['NAME'] != 'postgres': - return self.__class__( + conn = self.__class__( {**self.settings_dict, 'NAME': connection.settings_dict['NAME']}, alias=self.alias, ) - return nodb_connection + try: + with conn.cursor() as cursor: + yield cursor + finally: + conn.close() @cached_property def pg_version(self): diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py index 0cb26a4341..a609f33fd6 100644 --- a/django/db/backends/postgresql/creation.py +++ b/django/db/backends/postgresql/creation.py @@ -61,7 +61,7 @@ class DatabaseCreation(BaseDatabaseCreation): 'dbname': self._quote_name(target_database_name), 'suffix': self._get_database_create_suffix(template=source_database_name), } - with self._nodb_connection.cursor() as cursor: + with self._nodb_cursor() as cursor: try: self._execute_create_test_db(cursor, test_db_params, keepdb) except Exception: |
