Fixed #13798 -- Added connection argument to the connection_created signal. Thanks to liangent for the report, and Alex Gaynor for the patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@13672 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-08-30 13:21:18 +00:00
parent a5c80a28dc
commit 6909c22663
8 changed files with 35 additions and 45 deletions

View File

@ -51,7 +51,7 @@ class DatabaseWrapper(SqliteDatabaseWrapper):
self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp) self.connection.create_function("regexp", 2, _sqlite_regexp)
connection_created.send(sender=self.__class__) connection_created.send(sender=self.__class__, connection=self)
## From here on, customized for GeoDjango ## ## From here on, customized for GeoDjango ##

View File

@ -297,7 +297,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection = Database.connect(**kwargs) self.connection = Database.connect(**kwargs)
self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode] self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
self.connection.encoders[SafeString] = self.connection.encoders[str] self.connection.encoders[SafeString] = self.connection.encoders[str]
connection_created.send(sender=self.__class__) connection_created.send(sender=self.__class__, connection=self)
cursor = CursorWrapper(self.connection.cursor()) cursor = CursorWrapper(self.connection.cursor())
return cursor return cursor

View File

@ -384,7 +384,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
# Django docs specify cx_Oracle version 4.3.1 or higher, but # Django docs specify cx_Oracle version 4.3.1 or higher, but
# stmtcachesize is available only in 4.3.2 and up. # stmtcachesize is available only in 4.3.2 and up.
pass pass
connection_created.send(sender=self.__class__) connection_created.send(sender=self.__class__, connection=self)
if not cursor: if not cursor:
cursor = FormatStylePlaceholderCursor(self.connection) cursor = FormatStylePlaceholderCursor(self.connection)
return cursor return cursor

View File

@ -135,8 +135,9 @@ class DatabaseWrapper(BaseDatabaseWrapper):
if settings_dict['PORT']: if settings_dict['PORT']:
conn_string += " port=%s" % settings_dict['PORT'] conn_string += " port=%s" % settings_dict['PORT']
self.connection = Database.connect(conn_string, **settings_dict['OPTIONS']) self.connection = Database.connect(conn_string, **settings_dict['OPTIONS'])
self.connection.set_isolation_level(1) # make transactions transparent to all cursors # make transactions transparent to all cursors
connection_created.send(sender=self.__class__) self.connection.set_isolation_level(1)
connection_created.send(sender=self.__class__, connection=self)
cursor = self.connection.cursor() cursor = self.connection.cursor()
if new_connection: if new_connection:
if set_tz: if set_tz:

View File

@ -136,7 +136,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection = Database.connect(**conn_params) self.connection = Database.connect(**conn_params)
self.connection.set_client_encoding('UTF8') self.connection.set_client_encoding('UTF8')
self.connection.set_isolation_level(self.isolation_level) self.connection.set_isolation_level(self.isolation_level)
connection_created.send(sender=self.__class__) connection_created.send(sender=self.__class__, connection=self)
cursor = self.connection.cursor() cursor = self.connection.cursor()
cursor.tzinfo_factory = None cursor.tzinfo_factory = None
if new_connection: if new_connection:

View File

@ -1,3 +1,3 @@
from django.dispatch import Signal from django.dispatch import Signal
connection_created = Signal() connection_created = Signal(providing_args=["connection"])

View File

@ -176,7 +176,7 @@ class DatabaseWrapper(BaseDatabaseWrapper):
self.connection.create_function("django_extract", 2, _sqlite_extract) self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc) self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp) self.connection.create_function("regexp", 2, _sqlite_regexp)
connection_created.send(sender=self.__class__) connection_created.send(sender=self.__class__, connection=self)
return self.connection.cursor(factory=SQLiteCursorWrapper) return self.connection.cursor(factory=SQLiteCursorWrapper)
def close(self): def close(self):

View File

@ -8,6 +8,7 @@ from django.core import management
from django.core.management.color import no_style from django.core.management.color import no_style
from django.db import backend, connection, connections, DEFAULT_DB_ALIAS from django.db import backend, connection, connections, DEFAULT_DB_ALIAS
from django.db.backends.signals import connection_created from django.db.backends.signals import connection_created
from django.db.backends.postgresql import version as pg_version
from django.test import TestCase from django.test import TestCase
from regressiontests.backends import models from regressiontests.backends import models
@ -154,46 +155,34 @@ class SequenceResetTest(TestCase):
obj = models.Post.objects.create(name='New post', text='goodbye world') obj = models.Post.objects.create(name='New post', text='goodbye world')
self.assertTrue(obj.pk > 10) self.assertTrue(obj.pk > 10)
class PostgresVersionTest(TestCase):
def assert_parses(self, version_string, version):
self.assertEqual(pg_version._parse_version(version_string), version)
def connection_created_test(sender, **kwargs): def test_parsing(self):
print 'connection_created signal' self.assert_parses("PostgreSQL 8.3 beta4", (8, 3, None))
self.assert_parses("PostgreSQL 8.3", (8, 3, None))
__test__ = {'API_TESTS': """ self.assert_parses("EnterpriseDB 8.3", (8, 3, None))
# Check Postgres version parsing self.assert_parses("PostgreSQL 8.3.6", (8, 3, 6))
>>> from django.db.backends.postgresql import version as pg_version self.assert_parses("PostgreSQL 8.4beta1", (8, 4, None))
self.assert_parses("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)", (8, 3, 1))
>>> 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)")
(8, 3, 1)
>>> pg_version._parse_version("PostgreSQL 8.3.6")
(8, 3, 6)
>>> pg_version._parse_version("PostgreSQL 8.3")
(8, 3, None)
>>> pg_version._parse_version("EnterpriseDB 8.3")
(8, 3, None)
>>> pg_version._parse_version("PostgreSQL 8.3 beta4")
(8, 3, None)
>>> pg_version._parse_version("PostgreSQL 8.4beta1")
(8, 4, None)
"""}
# Unfortunately with sqlite3 the in-memory test database cannot be # Unfortunately with sqlite3 the in-memory test database cannot be
# closed, and so it cannot be re-opened during testing, and so we # closed, and so it cannot be re-opened during testing, and so we
# sadly disable this test for now. # sadly disable this test for now.
if settings.DATABASES[DEFAULT_DB_ALIAS]['ENGINE'] != 'django.db.backends.sqlite3': if settings.DATABASES[DEFAULT_DB_ALIAS]["ENGINE"] != "django.db.backends.sqlite3":
__test__['API_TESTS'] += """ class ConnectionCreatedSignalTest(TestCase):
>>> connection_created.connect(connection_created_test) def test_signal(self):
>>> connection.close() # Ensure the connection is closed data = {}
>>> cursor = connection.cursor() def receiver(sender, connection, **kwargs):
connection_created signal data["connection"] = connection
>>> connection_created.disconnect(connection_created_test)
>>> cursor = connection.cursor()
"""
if __name__ == '__main__': connection_created.connect(receiver)
unittest.main() connection.close()
cursor = connection.cursor()
self.assertTrue(data["connection"] is connection)
connection_created.disconnect(receiver)
data.clear()
cursor = connection.cursor()
self.assertTrue(data == {})