Code

Ticket #16250: 16250-3.diff

File 16250-3.diff, 2.8 KB (added by ramiro, 3 years ago)

Drop .set_autocommit(), replace it with a method that explicitly states what we use it for.

Line 
1diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
2--- a/django/db/backends/creation.py
3+++ b/django/db/backends/creation.py
4@@ -247,7 +247,7 @@
5             verbosity=max(verbosity - 1, 0),
6             interactive=False,
7             database=self.connection.alias)
8-       
9+
10         # One effect of calling syncdb followed by flush is that the id of the
11         # default site may or may not be 1, depending on how the sequence was
12         # reset.  If the sites app is loaded, then we coerce it.
13@@ -294,7 +294,7 @@
14         # if the database supports it because PostgreSQL doesn't allow
15         # CREATE/DROP DATABASE statements within transactions.
16         cursor = self.connection.cursor()
17-        self.set_autocommit()
18+        self.prepare_for_test_db_ddl()
19         try:
20             cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
21         except Exception, e:
22@@ -339,20 +339,19 @@
23         # to do so, because it's not allowed to delete a database while being
24         # connected to it.
25         cursor = self.connection.cursor()
26-        self.set_autocommit()
27+        self.prepare_for_test_db_ddl()
28         time.sleep(1) # To avoid "database is being accessed by other users" errors.
29         cursor.execute("DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name))
30         self.connection.close()
31 
32-    def set_autocommit(self):
33-        "Make sure a connection is in autocommit mode."
34-        if hasattr(self.connection.connection, "autocommit"):
35-            if callable(self.connection.connection.autocommit):
36-                self.connection.connection.autocommit(True)
37-            else:
38-                self.connection.connection.autocommit = True
39-        elif hasattr(self.connection.connection, "set_isolation_level"):
40-            self.connection.connection.set_isolation_level(0)
41+    def prepare_for_test_db_ddl(self):
42+        """
43+        Hook for actions needed before the ``CREATE DATABASE``/``DROP DATABASE``
44+        clauses we execute on the test database.
45+        This is needed e.g. in PostgreSQL to rollback and close any active
46+        transaction.
47+        """
48+        pass
49 
50     def sql_table_creation_suffix(self):
51         "SQL to append to the end of the test table creation statements"
52diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
53--- a/django/db/backends/postgresql_psycopg2/creation.py
54+++ b/django/db/backends/postgresql_psycopg2/creation.py
55@@ -76,3 +76,8 @@
56         else:
57             output = []
58         return output
59+
60+    def prepare_for_test_db_ddl(self):
61+        """Rollback and close the active transaction."""
62+        self.connection.connection.rollback()
63+        self.connection.connection.set_isolation_level(0)