Ticket #13798: django-connection-created.diff

File django-connection-created.diff, 8.9 KB (added by Alex Gaynor, 14 years ago)
  • django/contrib/gis/db/backends/spatialite/base.py

    diff --git a/django/contrib/gis/db/backends/spatialite/base.py b/django/contrib/gis/db/backends/spatialite/base.py
    index d419dab..729fc15 100644
    a b class DatabaseWrapper(SqliteDatabaseWrapper):  
    5151            self.connection.create_function("django_extract", 2, _sqlite_extract)
    5252            self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
    5353            self.connection.create_function("regexp", 2, _sqlite_regexp)
    54             connection_created.send(sender=self.__class__)
     54            connection_created.send(sender=self.__class__, connection=self)
    5555
    5656            ## From here on, customized for GeoDjango ##
    5757
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index e94e24b..a39c41f 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    297297            self.connection = Database.connect(**kwargs)
    298298            self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
    299299            self.connection.encoders[SafeString] = self.connection.encoders[str]
    300             connection_created.send(sender=self.__class__)
     300            connection_created.send(sender=self.__class__, connection=self)
    301301        cursor = CursorWrapper(self.connection.cursor())
    302302        return cursor
    303303
  • django/db/backends/oracle/base.py

    diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py
    index 369e65b..0cf26c4 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    384384                # Django docs specify cx_Oracle version 4.3.1 or higher, but
    385385                # stmtcachesize is available only in 4.3.2 and up.
    386386                pass
    387             connection_created.send(sender=self.__class__)
     387            connection_created.send(sender=self.__class__, connection=self)
    388388        if not cursor:
    389389            cursor = FormatStylePlaceholderCursor(self.connection)
    390390        return cursor
  • django/db/backends/postgresql/base.py

    diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py
    index a1c858b..f84ad1d 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    135135            if settings_dict['PORT']:
    136136                conn_string += " port=%s" % settings_dict['PORT']
    137137            self.connection = Database.connect(conn_string, **settings_dict['OPTIONS'])
    138             self.connection.set_isolation_level(1) # make transactions transparent to all cursors
    139             connection_created.send(sender=self.__class__)
     138            # make transactions transparent to all cursors
     139            self.connection.set_isolation_level(1)
     140            connection_created.send(sender=self.__class__, connection=self)
    140141        cursor = self.connection.cursor()
    141142        if new_connection:
    142143            if set_tz:
  • 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 29b7e7f..ce4e483 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    136136            self.connection = Database.connect(**conn_params)
    137137            self.connection.set_client_encoding('UTF8')
    138138            self.connection.set_isolation_level(self.isolation_level)
    139             connection_created.send(sender=self.__class__)
     139            connection_created.send(sender=self.__class__, connection=self)
    140140        cursor = self.connection.cursor()
    141141        cursor.tzinfo_factory = None
    142142        if new_connection:
  • django/db/backends/signals.py

    diff --git a/django/db/backends/signals.py b/django/db/backends/signals.py
    index a8079d0..fa411d7 100644
    a b  
    11from django.dispatch import Signal
    22
    3 connection_created = Signal()
     3connection_created = Signal(["connection"])
  • django/db/backends/sqlite3/base.py

    diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
    index bc97f5c..1ab2557 100644
    a b class DatabaseWrapper(BaseDatabaseWrapper):  
    176176            self.connection.create_function("django_extract", 2, _sqlite_extract)
    177177            self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
    178178            self.connection.create_function("regexp", 2, _sqlite_regexp)
    179             connection_created.send(sender=self.__class__)
     179            connection_created.send(sender=self.__class__, connection=self)
    180180        return self.connection.cursor(factory=SQLiteCursorWrapper)
    181181
    182182    def close(self):
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index 6a26a60..6855162 100644
    a b  
    11# -*- coding: utf-8 -*-
    22# Unit and doctests for specific database backends.
    33import datetime
    4 import models
    54import unittest
     5
     6from django.conf import settings
    67from django.db import backend, connection, DEFAULT_DB_ALIAS
    78from django.db.backends.signals import connection_created
    8 from django.conf import settings
     9from django.db.backends.postgresql import version as pg_version
    910from django.test import TestCase
    1011
    11 class Callproc(unittest.TestCase):
     12import models
    1213
     14
     15class Callproc(unittest.TestCase):
    1316    def test_dbms_session(self):
    1417        # If the backend is Oracle, test that we can call a standard
    1518        # stored procedure through our cursor wrapper.
    class ParameterHandlingTest(TestCase):  
    8891        self.assertRaises(Exception, cursor.executemany, query, [(1,2,3),])
    8992        self.assertRaises(Exception, cursor.executemany, query, [(1,),])
    9093
    91 
    92 def connection_created_test(sender, **kwargs):
    93     print 'connection_created signal'
    94 
    95 __test__ = {'API_TESTS': """
    96 # Check Postgres version parsing
    97 >>> from django.db.backends.postgresql import version as pg_version
    98 
    99 >>> pg_version._parse_version("PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)")
    100 (8, 3, 1)
    101 
    102 >>> pg_version._parse_version("PostgreSQL 8.3.6")
    103 (8, 3, 6)
    104 
    105 >>> pg_version._parse_version("PostgreSQL 8.3")
    106 (8, 3, None)
    107 
    108 >>> pg_version._parse_version("EnterpriseDB 8.3")
    109 (8, 3, None)
    110 
    111 >>> pg_version._parse_version("PostgreSQL 8.3 beta4")
    112 (8, 3, None)
    113 
    114 >>> pg_version._parse_version("PostgreSQL 8.4beta1")
    115 (8, 4, None)
    116 
    117 """}
    118 
    119 # Unfortunately with sqlite3 the in-memory test database cannot be
    120 # closed, and so it cannot be re-opened during testing, and so we
    121 # sadly disable this test for now.
    122 if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3':
    123     __test__['API_TESTS'] += """
    124 >>> connection_created.connect(connection_created_test)
    125 >>> connection.close() # Ensure the connection is closed
    126 >>> cursor = connection.cursor()
    127 connection_created signal
    128 >>> connection_created.disconnect(connection_created_test)
    129 >>> cursor = connection.cursor()
    130 """
    131 
    132 if __name__ == '__main__':
    133     unittest.main()
     94class PostgresVersionTest(TestCase):
     95    def assert_parses(self, version_string, version):
     96        self.assertEqual(pg_version._parse_version(version_string), version)
     97   
     98    def test_parsing(self):
     99        self.assert_parses(
     100            "PostgreSQL 8.3.1 on i386-apple-darwin9.2.2, compiled by GCC i686-apple-darwin9-gcc-4.0.1 (GCC) 4.0.1 (Apple Inc. build 5478)",
     101            (8, 3, 1)
     102        )
     103       
     104        self.assert_parses(
     105            "PostgreSQL 8.3.6",
     106            (8, 3, 6)
     107        )
     108       
     109        self.assert_parses(
     110            "PostgreSQL 8.3",
     111            (8, 3, None)
     112        )
     113       
     114        self.assert_parses(
     115            "EnterpriseDB 8.3",
     116            (8, 3, None)
     117        )
     118       
     119        self.assert_parses(
     120            "PostgreSQL 8.3 beta4",
     121            (8, 3, None)
     122        )
     123       
     124        self.assert_parses(
     125            "PostgreSQL 8.4beta1",
     126            (8, 4, None)
     127        )
     128
     129# Unfortunately with sqlite3 the in-memory test database cannot be closed, and
     130# so it cannot be re-opened during testing, and so we sadly disable this test
     131# for now.
     132if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] != "django.db.backends.sqlite3":
     133    class ConnectionCreatedSignalTest(TestCase):
     134        def test_signal(self):
     135            data = {}
     136            def receiver(sender, connection, **kwargs):
     137                data["connection"] = connection
     138
     139            connection_created.connect(receiver)
     140            connection.close()
     141            cursor = connection.cursor()
     142            self.assertTrue(data["connection"] is connection)
     143           
     144            connection_created.disconnect(receiver)
     145            data.clear()
     146            cursor = connection.cursor()
     147            self.assertTrue(data == {})
Back to Top