This addressed a regression introduced bya96b901932
as identified by Ran Benita. Backport of7071f8f272
from master
This commit is contained in:
parent
9278acfd12
commit
7f25344c2e
|
@ -195,7 +195,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
return connection
|
return connection
|
||||||
|
|
||||||
def ensure_timezone(self):
|
def ensure_timezone(self):
|
||||||
if not self.is_usable():
|
if self.connection is None:
|
||||||
return False
|
return False
|
||||||
conn_timezone_name = self.connection.get_parameter_status('TimeZone')
|
conn_timezone_name = self.connection.get_parameter_status('TimeZone')
|
||||||
timezone_name = self.timezone_name
|
timezone_name = self.timezone_name
|
||||||
|
@ -208,7 +208,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
def init_connection_state(self):
|
def init_connection_state(self):
|
||||||
self.connection.set_client_encoding('UTF8')
|
self.connection.set_client_encoding('UTF8')
|
||||||
|
|
||||||
self.ensure_connection()
|
|
||||||
timezone_changed = self.ensure_timezone()
|
timezone_changed = self.ensure_timezone()
|
||||||
if timezone_changed:
|
if timezone_changed:
|
||||||
# Commit after setting the time zone (see #17062)
|
# Commit after setting the time zone (see #17062)
|
||||||
|
@ -248,8 +247,6 @@ class DatabaseWrapper(BaseDatabaseWrapper):
|
||||||
self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
|
self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
|
||||||
|
|
||||||
def is_usable(self):
|
def is_usable(self):
|
||||||
if self.connection is None:
|
|
||||||
return False
|
|
||||||
try:
|
try:
|
||||||
# Use a psycopg cursor directly, bypassing Django's utilities.
|
# Use a psycopg cursor directly, bypassing Django's utilities.
|
||||||
self.connection.cursor().execute("SELECT 1")
|
self.connection.cursor().execute("SELECT 1")
|
||||||
|
|
|
@ -134,6 +134,12 @@ class Tests(TestCase):
|
||||||
finally:
|
finally:
|
||||||
new_connection.close()
|
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):
|
def _select(self, val):
|
||||||
with connection.cursor() as cursor:
|
with connection.cursor() as cursor:
|
||||||
cursor.execute('SELECT %s', (val,))
|
cursor.execute('SELECT %s', (val,))
|
||||||
|
|
Loading…
Reference in New Issue