Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #17882 (again) -- Updated the database connections' time zone w…

…hen time-zone-related settings are changed in tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17709 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 3ef55dfaa03b3f5be3ee3632dfc59b8f752d2dec 1 parent 4b14546
Aymeric Augustin authored March 13, 2012
8  django/db/backends/__init__.py
@@ -710,6 +710,14 @@ def savepoint_rollback_sql(self, sid):
710 710
         """
711 711
         raise NotImplementedError
712 712
 
  713
+    def set_time_zone_sql(self):
  714
+        """
  715
+        Returns the SQL that will set the connection's time zone.
  716
+
  717
+        Returns '' if the backend doesn't support time zones.
  718
+        """
  719
+        return ''
  720
+
713 721
     def sql_flush(self, style, tables, sequences):
714 722
         """
715 723
         Returns a list of SQL statements required to remove all data from
3  django/db/backends/postgresql_psycopg2/base.py
@@ -190,7 +190,8 @@ def _cursor(self):
190 190
                     # Set the time zone in autocommit mode (see #17062)
191 191
                     self.connection.set_isolation_level(
192 192
                             psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)
193  
-                    self.connection.cursor().execute("SET TIME ZONE %s", [tz])
  193
+                    self.connection.cursor().execute(
  194
+                            self.ops.set_time_zone_sql(), [tz])
194 195
             self.connection.set_isolation_level(self.isolation_level)
195 196
             self._get_pg_version()
196 197
             connection_created.send(sender=self.__class__, connection=self)
3  django/db/backends/postgresql_psycopg2/operations.py
@@ -71,6 +71,9 @@ def quote_name(self, name):
71 71
             return name # Quoting once is enough.
72 72
         return '"%s"' % name
73 73
 
  74
+    def set_time_zone_sql(self):
  75
+        return "SET TIME ZONE %s"
  76
+
74 77
     def sql_flush(self, style, tables, sequences):
75 78
         if tables:
76 79
             # Perform a single SQL 'TRUNCATE x, y, z...;' statement.  It allows
23  django/test/signals.py
... ...
@@ -1,14 +1,23 @@
1 1
 from django.conf import settings
2  
-from django.db import close_connection
  2
+from django.db import connections
3 3
 from django.dispatch import Signal
4 4
 
5 5
 template_rendered = Signal(providing_args=["template", "context"])
6 6
 
7 7
 setting_changed = Signal(providing_args=["setting", "value"])
8 8
 
9  
-# Close the database connection to re-establish it with the proper time zone.
10  
-def close_connection_on_time_zone_change(**kwargs):
11  
-    if (kwargs['setting'] == 'USE_TZ'
12  
-        or (kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ)):
13  
-        close_connection()
14  
-setting_changed.connect(close_connection_on_time_zone_change)
  9
+def update_connections_time_zone(**kwargs):
  10
+    if kwargs['setting'] == 'USE_TZ' and settings.TIME_ZONE != 'UTC':
  11
+        USE_TZ, TIME_ZONE = kwargs['value'], settings.TIME_ZONE
  12
+    elif kwargs['setting'] == 'TIME_ZONE' and not settings.USE_TZ:
  13
+        USE_TZ, TIME_ZONE = settings.USE_TZ, kwargs['value']
  14
+    else:   # no need to change the database connnections' time zones
  15
+        return
  16
+
  17
+    tz = 'UTC' if USE_TZ else TIME_ZONE
  18
+    for conn in connections.all():
  19
+        tz_sql = conn.ops.set_time_zone_sql()
  20
+        if tz_sql:
  21
+            conn.cursor().execute(tz_sql, [tz])
  22
+
  23
+setting_changed.connect(update_connections_time_zone)

0 notes on commit 3ef55df

Please sign in to comment.
Something went wrong with that request. Please try again.