From 7f25344c2e69454bb69ec6100a1bc0af6cee6d70 Mon Sep 17 00:00:00 2001 From: Simon Charette Date: Wed, 20 Feb 2019 04:59:40 -0500 Subject: [PATCH] [2.2.x] Fixed #30193, Refs #28478 -- Avoided PostgreSQL connection health checks on initialization. This addressed a regression introduced by a96b9019320ed8236659ee520a7a017c1bafbc6f as identified by Ran Benita. Backport of 7071f8f2729295b0da77b6c651966dc32c71f1ab from master --- django/db/backends/postgresql/base.py | 5 +---- tests/backends/postgresql/tests.py | 6 ++++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 4376b8609c..f0d66efd2a 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -195,7 +195,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): return connection def ensure_timezone(self): - if not self.is_usable(): + if self.connection is None: return False conn_timezone_name = self.connection.get_parameter_status('TimeZone') timezone_name = self.timezone_name @@ -208,7 +208,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): def init_connection_state(self): self.connection.set_client_encoding('UTF8') - self.ensure_connection() timezone_changed = self.ensure_timezone() if timezone_changed: # Commit after setting the time zone (see #17062) @@ -248,8 +247,6 @@ class DatabaseWrapper(BaseDatabaseWrapper): self.cursor().execute('SET CONSTRAINTS ALL DEFERRED') def is_usable(self): - if self.connection is None: - return False try: # Use a psycopg cursor directly, bypassing Django's utilities. self.connection.cursor().execute("SELECT 1") diff --git a/tests/backends/postgresql/tests.py b/tests/backends/postgresql/tests.py index 6fd0ba4420..96a1501693 100644 --- a/tests/backends/postgresql/tests.py +++ b/tests/backends/postgresql/tests.py @@ -134,6 +134,12 @@ class Tests(TestCase): finally: new_connection.close() + def test_connect_no_is_usable_checks(self): + new_connection = connection.copy() + with mock.patch.object(new_connection, 'is_usable') as is_usable: + new_connection.connect() + is_usable.assert_not_called() + def _select(self, val): with connection.cursor() as cursor: cursor.execute('SELECT %s', (val,))