From da3b38cdda036425d0aae1051d4bb60af671694e Mon Sep 17 00:00:00 2001 From: Justin Bronn Date: Sun, 29 Mar 2009 23:15:58 +0000 Subject: [PATCH] Fixed #6064 -- Added the `connection_created` signal for when a database connection is created. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10182 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/backends/mysql/base.py | 2 ++ django/db/backends/oracle/base.py | 2 ++ django/db/backends/postgresql/base.py | 2 ++ .../db/backends/postgresql_psycopg2/base.py | 2 ++ django/db/backends/signals.py | 6 +++++ django/db/backends/sqlite3/base.py | 2 ++ tests/regressiontests/backends/tests.py | 24 +++++++++++++++---- 7 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 django/db/backends/signals.py diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 06f477db84..ea80f7874e 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -25,6 +25,7 @@ from MySQLdb.converters import conversions 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 @@ class DatabaseWrapper(BaseDatabaseWrapper): 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 diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 0a4ab3d0b1..8f96049cc4 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -25,6 +25,7 @@ except ImportError, e: 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 @@ class DatabaseWrapper(BaseDatabaseWrapper): # 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 diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 9050cfd9e0..afb7432c84 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -5,6 +5,7 @@ Requires psycopg 1: http://initd.org/projects/psycopg1 """ 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 @@ class DatabaseWrapper(BaseDatabaseWrapper): 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']]) diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py index a5d6419bc6..4d6a1e2f5d 100644 --- a/django/db/backends/postgresql_psycopg2/base.py +++ b/django/db/backends/postgresql_psycopg2/base.py @@ -6,6 +6,7 @@ Requires psycopg 2: http://initd.org/projects/psycopg2 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 @@ class DatabaseWrapper(BaseDatabaseWrapper): 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: diff --git a/django/db/backends/signals.py b/django/db/backends/signals.py new file mode 100644 index 0000000000..e194d0438e --- /dev/null +++ b/django/db/backends/signals.py @@ -0,0 +1,6 @@ +from django.dispatch import Signal + +connection_created = Signal() +from django.dispatch import Signal + +connection_created = Signal() diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index b6263df7c7..8a4c072896 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -8,6 +8,7 @@ standard library. """ 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 @@ class DatabaseWrapper(BaseDatabaseWrapper): 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): diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py index 5dec9feee6..15bfe1f579 100644 --- a/tests/regressiontests/backends/tests.py +++ b/tests/regressiontests/backends/tests.py @@ -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 @@ class Callproc(unittest.TestCase): 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()