diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 7674f5c879..6f1f4b9618 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -710,6 +710,14 @@ class BaseDatabaseOperations(object): """ raise NotImplementedError + def set_time_zone_sql(self): + """ + Returns the SQL that will set the connection's time zone. + + Returns '' if the backend doesn't support time zones. + """ + return '' + def sql_flush(self, style, tables, sequences): """ Returns a list of SQL statements required to remove all data from diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index 802ebf2b80..0d25129313 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -190,7 +190,8 @@ class DatabaseWrapper(BaseDatabaseWrapper): # Set the time zone in autocommit mode (see #17062) self.connection.set_isolation_level( psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) - self.connection.cursor().execute("SET TIME ZONE %s", [tz]) + self.connection.cursor().execute( + self.ops.set_time_zone_sql(), [tz]) self.connection.set_isolation_level(self.isolation_level) self._get_pg_version() connection_created.send(sender=self.__class__, connection=self) diff --git a/django/db/backends/postgresql_psycopg2/operations.py b/django/db/backends/postgresql_psycopg2/operations.py index 949a05c0b6..395cd92047 100644 --- a/django/db/backends/postgresql_psycopg2/operations.py +++ b/django/db/backends/postgresql_psycopg2/operations.py @@ -71,6 +71,9 @@ class DatabaseOperations(BaseDatabaseOperations): return name # Quoting once is enough. return '"%s"' % name + def set_time_zone_sql(self): + return "SET TIME ZONE %s" + def sql_flush(self, style, tables, sequences): if tables: # Perform a single SQL 'TRUNCATE x, y, z...;' statement. It allows diff --git a/django/test/signals.py b/django/test/signals.py index d6797395cf..01d55817a2 100644 --- a/django/test/signals.py +++ b/django/test/signals.py @@ -1,14 +1,23 @@ from django.conf import settings -from django.db import close_connection +from django.db import connections from django.dispatch import Signal template_rendered = Signal(providing_args=["template", "context"]) setting_changed = Signal(providing_args=["setting", "value"]) -# Close the database connection to re-establish it with the proper time zone. -def close_connection_on_time_zone_change(**kwargs): - if (kwargs['setting'] == 'USE_TZ' - or (kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ)): - close_connection() -setting_changed.connect(close_connection_on_time_zone_change) +def update_connections_time_zone(**kwargs): + if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC': + USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE + elif kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ: + USE_TZ, TIME_ZONE = settings.USE_TZ, kwargs['value'] + else: # no need to change the database connnections' time zones + return + + tz = 'UTC' if USE_TZ else TIME_ZONE + for conn in connections.all(): + tz_sql = conn.ops.set_time_zone_sql() + if tz_sql: + conn.cursor().execute(tz_sql, [tz]) + +setting_changed.connect(update_connections_time_zone)