Ticket #17882: 17882-second-take.patch

File 17882-second-take.patch, 4.1 KB (added by Aymeric Augustin, 13 years ago)
  • django/test/signals.py

     
    11from django.conf import settings
    2 from django.db import close_connection
     2from django.db import connections
    33from django.dispatch import Signal
    44
    55template_rendered = Signal(providing_args=["template", "context"])
    66
    77setting_changed = Signal(providing_args=["setting", "value"])
    88
    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)
     9def 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    tz = 'UTC' if USE_TZ else TIME_ZONE
     17    for conn in connections.all():
     18        tz_sql = conn.ops.set_time_zone_sql()
     19        if tz_sql:
     20            conn.cursor().execute(tz_sql, [tz])
     21setting_changed.connect(update_connections_time_zone)
  • django/db/backends/oracle/base.py

     
    274274    def savepoint_rollback_sql(self, sid):
    275275        return convert_unicode("ROLLBACK TO SAVEPOINT " + self.quote_name(sid))
    276276
     277    def set_time_zone_sql(self):
     278        return "ALTER SESSION SET TIME_ZONE = %s"
     279
    277280    def sql_flush(self, style, tables, sequences):
    278281        # Return a list of 'TRUNCATE x;', 'TRUNCATE y;',
    279282        # 'TRUNCATE z;'... style SQL statements
  • django/db/backends/__init__.py

     
    710710        """
    711711        raise NotImplementedError
    712712
     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
    713721    def sql_flush(self, style, tables, sequences):
    714722        """
    715723        Returns a list of SQL statements required to remove all data from
  • django/db/backends/postgresql_psycopg2/base.py

     
    190190                    # Set the time zone in autocommit mode (see #17062)
    191191                    self.connection.set_isolation_level(
    192192                            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])
    194195            self.connection.set_isolation_level(self.isolation_level)
    195196            self._get_pg_version()
    196197            connection_created.send(sender=self.__class__, connection=self)
  • django/db/backends/postgresql_psycopg2/operations.py

     
    7171            return name # Quoting once is enough.
    7272        return '"%s"' % name
    7373
     74    def set_time_zone_sql(self):
     75        return "SET TIME ZONE %s"
     76
    7477    def sql_flush(self, style, tables, sequences):
    7578        if tables:
    7679            # Perform a single SQL 'TRUNCATE x, y, z...;' statement.  It allows
Back to Top