Code

Ticket #6064: 6064-using-signals-2.diff

File 6064-using-signals-2.diff, 6.1 KB (added by mdh, 6 years ago)

Updated patch to apply against trunk as of r9477.

Line 
1Index: django/db/backends/postgresql/base.py
2===================================================================
3--- django/db/backends/postgresql/base.py       (revision 9477)
4+++ django/db/backends/postgresql/base.py       (working copy)
5@@ -5,6 +5,7 @@
6 """
7 
8 from django.db.backends import *
9+from django.db.backends.signals import connection_created
10 from django.db.backends.postgresql.client import DatabaseClient
11 from django.db.backends.postgresql.creation import DatabaseCreation
12 from django.db.backends.postgresql.introspection import DatabaseIntrospection
13@@ -113,6 +114,7 @@
14                 conn_string += " port=%s" % settings.DATABASE_PORT
15             self.connection = Database.connect(conn_string, **self.options)
16             self.connection.set_isolation_level(1) # make transactions transparent to all cursors
17+            connection_created.send(sender=self.__class__)
18         cursor = self.connection.cursor()
19         if set_tz:
20             cursor.execute("SET TIME ZONE %s", [settings.TIME_ZONE])
21Index: django/db/backends/sqlite3/base.py
22===================================================================
23--- django/db/backends/sqlite3/base.py  (revision 9477)
24+++ django/db/backends/sqlite3/base.py  (working copy)
25@@ -7,6 +7,7 @@
26 """
27 
28 from django.db.backends import *
29+from django.db.backends.signals import connection_created
30 from django.db.backends.sqlite3.client import DatabaseClient
31 from django.db.backends.sqlite3.creation import DatabaseCreation
32 from django.db.backends.sqlite3.introspection import DatabaseIntrospection
33@@ -147,6 +148,7 @@
34             self.connection.create_function("django_extract", 2, _sqlite_extract)
35             self.connection.create_function("django_date_trunc", 2, _sqlite_date_trunc)
36             self.connection.create_function("regexp", 2, _sqlite_regexp)
37+            connection_created.send(sender=self.__class__)
38         return self.connection.cursor(factory=SQLiteCursorWrapper)
39 
40     def close(self):
41Index: django/db/backends/mysql/base.py
42===================================================================
43--- django/db/backends/mysql/base.py    (revision 9477)
44+++ django/db/backends/mysql/base.py    (working copy)
45@@ -25,6 +25,7 @@
46 from MySQLdb.constants import FIELD_TYPE, FLAG
47 
48 from django.db.backends import *
49+from django.db.backends.signals import connection_created
50 from django.db.backends.mysql.client import DatabaseClient
51 from django.db.backends.mysql.creation import DatabaseCreation
52 from django.db.backends.mysql.introspection import DatabaseIntrospection
53@@ -262,6 +263,7 @@
54             self.connection = Database.connect(**kwargs)
55             self.connection.encoders[SafeUnicode] = self.connection.encoders[unicode]
56             self.connection.encoders[SafeString] = self.connection.encoders[str]
57+            connection_created.send(sender=self.__class__)
58         cursor = CursorWrapper(self.connection.cursor())
59         return cursor
60 
61Index: django/db/backends/oracle/base.py
62===================================================================
63--- django/db/backends/oracle/base.py   (revision 9477)
64+++ django/db/backends/oracle/base.py   (working copy)
65@@ -17,6 +17,7 @@
66     raise ImproperlyConfigured("Error loading cx_Oracle module: %s" % e)
67 
68 from django.db.backends import *
69+from django.db.backends.signals import connection_created
70 from django.db.backends.oracle import query
71 from django.db.backends.oracle.client import DatabaseClient
72 from django.db.backends.oracle.creation import DatabaseCreation
73@@ -274,6 +275,7 @@
74                 # Django docs specify cx_Oracle version 4.3.1 or higher, but
75                 # stmtcachesize is available only in 4.3.2 and up.
76                 pass
77+            connection_created.send(sender=self.__class__)
78         if not cursor:
79             cursor = FormatStylePlaceholderCursor(self.connection)
80         # Default arraysize of 1 is highly sub-optimal.
81Index: django/db/backends/signals.py
82===================================================================
83--- django/db/backends/signals.py       (revision 0)
84+++ django/db/backends/signals.py       (revision 0)
85@@ -0,0 +1,3 @@
86+from django.dispatch import Signal
87+
88+connection_created = Signal()
89Index: django/db/backends/postgresql_psycopg2/base.py
90===================================================================
91--- django/db/backends/postgresql_psycopg2/base.py      (revision 9477)
92+++ django/db/backends/postgresql_psycopg2/base.py      (working copy)
93@@ -5,6 +5,7 @@
94 """
95 
96 from django.db.backends import *
97+from django.db.backends.signals import connection_created
98 from django.db.backends.postgresql.operations import DatabaseOperations as PostgresqlDatabaseOperations
99 from django.db.backends.postgresql.client import DatabaseClient
100 from django.db.backends.postgresql.creation import DatabaseCreation
101@@ -84,6 +85,7 @@
102             self.connection = Database.connect(conn_string, **self.options)
103             self.connection.set_isolation_level(1) # make transactions transparent to all cursors
104             self.connection.set_client_encoding('UTF8')
105+            connection_created.send(sender=self.__class__)
106         cursor = self.connection.cursor()
107         cursor.tzinfo_factory = None
108         if set_tz:
109Index: tests/regressiontests/backends/tests.py
110===================================================================
111--- tests/regressiontests/backends/tests.py     (revision 0)
112+++ tests/regressiontests/backends/tests.py     (revision 0)
113@@ -0,0 +1,21 @@
114+from django.db import connection
115+from django.db.backends.signals import connection_created
116+from django.conf import settings
117+
118+def connection_created_test(sender, **kwargs):
119+    print 'connection_created signal'
120+
121+__test__ = {'API_TESTS': ''}
122+
123+# Unfortunately with sqlite3 the in-memory test database cannot be
124+# closed, and so it cannot be re-opened during testing, and so we
125+# sadly disable this test for now.
126+if settings.DATABASE_ENGINE != 'sqlite3':
127+    __test__['API_TESTS'] += """
128+>>> connection_created.connect(connection_created_test)
129+>>> connection.close() # Ensure the connection is closed
130+>>> cursor = connection.cursor()
131+connection_created signal
132+>>> connection_created.disconnect(connection_created_test)
133+>>> cursor = connection.cursor()
134+"""