Ticket #17213: 17213.diff

File 17213.diff, 4.1 KB (added by Calvin Spealman, 13 years ago)

Patch that adds a way to flag when the connection shoudl set once-only things again, and a test for it.

  • django/db/backends/postgresql_psycopg2/base.py

    diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
    index 74342e9..ebee623 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    109109        self.introspection = DatabaseIntrospection(self)
    110110        self.validation = BaseDatabaseValidation(self)
    111111        self._pg_version = None
     112        self.force_db_set = True
    112113
    113114    def check_constraints(self, table_names=None):
    114115        """
    class DatabaseWrapper(BaseDatabaseWrapper):  
    143144    pg_version = property(_get_pg_version)
    144145
    145146    def _cursor(self):
    146         new_connection = False
     147        force_db_set = self.force_db_set or self.connection is None
    147148        set_tz = False
    148149        settings_dict = self.settings_dict
    149         if self.connection is None:
    150             new_connection = True
     150        if force_db_set:
    151151            set_tz = settings_dict.get('TIME_ZONE')
    152152            if settings_dict['NAME'] == '':
    153153                from django.core.exceptions import ImproperlyConfigured
    class DatabaseWrapper(BaseDatabaseWrapper):  
    172172            connection_created.send(sender=self.__class__, connection=self)
    173173        cursor = self.connection.cursor()
    174174        cursor.tzinfo_factory = None
    175         if new_connection:
     175        if force_db_set:
    176176            if set_tz:
    177                 cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])
     177                self._set_tz(cursor, settings_dict['TIME_ZONE'])
    178178            self._get_pg_version()
     179            self.force_db_set = False
    179180        return CursorWrapper(cursor)
    180181
     182    def _set_tz(self, cursor, tz):
     183        cursor.execute("SET TIME ZONE %s", [tz])
     184
    181185    def _enter_transaction_management(self, managed):
    182186        """
    183187        Switch the isolation level when needing transaction support, so that
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index f2bd71d..e105367 100644
    a b class BackendTestCase(TestCase):  
    308308        self.assertTrue(hasattr(connection.ops, 'connection'))
    309309        self.assertEqual(connection, connection.ops.connection)
    310310
     311    @unittest.skipUnless(connection.vendor == 'postgresql',
     312                         "SET TIME ZIME needs executed again if TIME_ZONE changes")
     313    def test_cursor_force_db_set(self):
     314        _set_tz = connection._set_tz
     315        def mock_set_tz(*args, **kwargs):
     316            mock_set_tz.called = (args, kwargs)
     317        mock_set_tz.called = False
     318        connection._set_tz = mock_set_tz
     319        try:
     320            connection.cursor()
     321            self.assert_(mock_set_tz.called)
     322            self.assertEqual(settings.TIME_ZONE, mock_set_tz.called[0][1])
     323            mock_set_tz.called = False
     324
     325            connection.cursor()
     326            self.assertFalse(mock_set_tz.called)
     327            mock_set_tz.called = False
     328
     329            connection.force_db_set = True
     330            connection.cursor()
     331            self.assert_(mock_set_tz.called)
     332            self.assertEqual(settings.TIME_ZONE, mock_set_tz.called[0][1])
     333            mock_set_tz.called = False
     334
     335        finally:
     336            connection._set_tz = _set_tz
     337
    311338
    312339# We don't make these tests conditional because that means we would need to
    313340# check and differentiate between:
  • tests/regressiontests/model_regress/tests.py

    diff --git a/tests/regressiontests/model_regress/tests.py b/tests/regressiontests/model_regress/tests.py
    index 07ad8fa..0476193 100644
    a b class ModelTests(TestCase):  
    148148
    149149    @skipUnlessDBFeature("supports_timezones")
    150150    def test_timezones(self):
    151         # Saving an updating with timezone-aware datetime Python objects.
     151        # Saving and updating with timezone-aware datetime Python objects.
    152152        # Regression test for #10443.
    153153        # The idea is that all these creations and saving should work without
    154154        # crashing. It's not rocket science.
Back to Top