Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #6064 -- Added the `connection_created` signal for when a datab…

…ase connection is created.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10182 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit da3b38cdda036425d0aae1051d4bb60af671694e 1 parent c0ee4c5
Justin Bronn jbronn authored
2  django/db/backends/mysql/base.py
View
@@ -25,6 +25,7 @@
from MySQLdb.constants import FIELD_TYPE, FLAG
from django.db.backends import *
+from django.db.backends.signals import connection_created
from django.db.backends.mysql.client import DatabaseClient
from django.db.backends.mysql.creation import DatabaseCreation
from django.db.backends.mysql.introspection import DatabaseIntrospection
@@ -277,6 +278,7 @@ def _cursor(self):
self.connection = Database.connect(**kwargs)
self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
self.connection.encoders[SafeString] = self.connection.encoders[str]
+ connection_created.send(sender=self.__class__)
cursor = CursorWrapper(self.connection.cursor())
return cursor
2  django/db/backends/oracle/base.py
View
@@ -25,6 +25,7 @@
raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e)
from django.db.backends import *
+from django.db.backends.signals import connection_created
from django.db.backends.oracle import query
from django.db.backends.oracle.client import DatabaseClient
from django.db.backends.oracle.creation import DatabaseCreation
@@ -329,6 +330,7 @@ def _cursor(self):
# Django docs specify cx_Oracle version 4.3.1 or higher, but
# stmtcachesize is available only in 4.3.2 and up.
pass
+ connection_created.send(sender=self.__class__)
if not cursor:
cursor = FormatStylePlaceholderCursor(self.connection)
return cursor
2  django/db/backends/postgresql/base.py
View
@@ -5,6 +5,7 @@
"""
from django.db.backends import *
+from django.db.backends.signals import connection_created
from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation
from django.db.backends.postgresql.introspection import DatabaseIntrospection
@@ -114,6 +115,7 @@ def _cursor(self):
conn_string += " port=%s" % settings_dict['DATABASE_PORT']
self.connection = Database.connect(conn_string, **settings_dict['DATABASE_OPTIONS'])
self.connection.set_isolation_level(1) # make transactions transparent to all cursors
+ connection_created.send(sender=self.__class__)
cursor = self.connection.cursor()
if set_tz:
cursor.execute("SET TIME ZONE %s", [settings_dict['TIME_ZONE']])
2  django/db/backends/postgresql_psycopg2/base.py
View
@@ -6,6 +6,7 @@
from django.conf import settings
from django.db.backends import *
+from django.db.backends.signals import connection_created
from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
from django.db.backends.postgresql.client import DatabaseClient
from django.db.backends.postgresql.creation import DatabaseCreation
@@ -96,6 +97,7 @@ def _cursor(self):
conn_params['port'] = settings_dict['DATABASE_PORT']
self.connection = Database.connect(**conn_params)
self.connection.set_client_encoding('UTF8')
+ connection_created.send(sender=self.__class__)
cursor = self.connection.cursor()
cursor.tzinfo_factory = None
if set_tz:
6 django/db/backends/signals.py
View
@@ -0,0 +1,6 @@
+from django.dispatch import Signal
+
+connection_created = Signal()
+from django.dispatch import Signal
+
+connection_created = Signal()
2  django/db/backends/sqlite3/base.py
View
@@ -8,6 +8,7 @@
"""
from django.db.backends import *
+from django.db.backends.signals import connection_created
from django.db.backends.sqlite3.client import DatabaseClient
from django.db.backends.sqlite3.creation import DatabaseCreation
from django.db.backends.sqlite3.introspection import DatabaseIntrospection
@@ -171,6 +172,7 @@ def _cursor(self):
self.connection.create_function("django_extract", 2, _sqlite_extract)
self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
self.connection.create_function("regexp", 2, _sqlite_regexp)
+ connection_created.send(sender=self.__class__)
return self.connection.cursor(factory=SQLiteCursorWrapper)
def close(self):
24 tests/regressiontests/backends/tests.py
View
@@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
-
-# Unit tests for specific database backends.
-
+# Unit and doctests for specific database backends.
import unittest
-
from django.db import connection
+from django.db.backends.signals import connection_created
from django.conf import settings
-
class Callproc(unittest.TestCase):
def test_dbms_session(self):
@@ -21,6 +18,23 @@ def test_dbms_session(self):
else:
return True
+def connection_created_test(sender, **kwargs):
+ print 'connection_created signal'
+
+__test__ = {'API_TESTS': ''}
+
+# Unfortunately with sqlite3 the in-memory test database cannot be
+# closed, and so it cannot be re-opened during testing, and so we
+# sadly disable this test for now.
+if settings.DATABASE_ENGINE != 'sqlite3':
+ __test__['API_TESTS'] += """
+>>> connection_created.connect(connection_created_test)
+>>> connection.close() # Ensure the connection is closed
+>>> cursor = connection.cursor()
+connection_created signal
+>>> connection_created.disconnect(connection_created_test)
+>>> cursor = connection.cursor()
+"""
if __name__ == '__main__':
unittest.main()
Please sign in to comment.
Something went wrong with that request. Please try again.