Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16250 -- Made test database creation support code in the Postg…

…reSQL DB backend compatible with psycopg2 2.4.2.

Implemented this by adding an internal hook for work that should be performed
before that point.

Also, regarding the `DatabaseCreation.set_autocommit()` method:
 * Stop using it for such tasks
 * Stop providing an implementation that tries to cover all the possible
   idioms a third party database backend DB-API 2 driver could need to activate
   autocommit. It is now left for third party backends to implement.

This can be backwards incompatible in the case of user applications that:
 * Had started using this method
 * Use a third a party database backend

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16520 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 569aa34ea5eec46ec0d899f4db682687964feec0 1 parent 21e0b3a
Ramiro Morales authored July 06, 2011
29  django/db/backends/creation.py
@@ -247,7 +247,7 @@ def create_test_db(self, verbosity=1, autoclobber=False):
247 247
             verbosity=max(verbosity - 1, 0),
248 248
             interactive=False,
249 249
             database=self.connection.alias)
250  
-        
  250
+
251 251
         # One effect of calling syncdb followed by flush is that the id of the
252 252
         # default site may or may not be 1, depending on how the sequence was
253 253
         # reset.  If the sites app is loaded, then we coerce it.
@@ -294,7 +294,7 @@ def _create_test_db(self, verbosity, autoclobber):
294 294
         # if the database supports it because PostgreSQL doesn't allow
295 295
         # CREATE/DROP DATABASE statements within transactions.
296 296
         cursor = self.connection.cursor()
297  
-        self.set_autocommit()
  297
+        self._prepare_for_test_db_ddl()
298 298
         try:
299 299
             cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
300 300
         except Exception, e:
@@ -339,20 +339,27 @@ def _destroy_test_db(self, test_database_name, verbosity):
339 339
         # to do so, because it's not allowed to delete a database while being
340 340
         # connected to it.
341 341
         cursor = self.connection.cursor()
342  
-        self.set_autocommit()
  342
+        self._prepare_for_test_db_ddl()
343 343
         time.sleep(1) # To avoid "database is being accessed by other users" errors.
344 344
         cursor.execute("DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name))
345 345
         self.connection.close()
346 346
 
347 347
     def set_autocommit(self):
348  
-        "Make sure a connection is in autocommit mode."
349  
-        if hasattr(self.connection.connection, "autocommit"):
350  
-            if callable(self.connection.connection.autocommit):
351  
-                self.connection.connection.autocommit(True)
352  
-            else:
353  
-                self.connection.connection.autocommit = True
354  
-        elif hasattr(self.connection.connection, "set_isolation_level"):
355  
-            self.connection.connection.set_isolation_level(0)
  348
+        """
  349
+        Make sure a connection is in autocommit mode. - Deprecated, not used
  350
+        anymore by Django code. Kept for compatibility with user code that
  351
+        might use it.
  352
+        """
  353
+        pass
  354
+
  355
+    def _prepare_for_test_db_ddl(self):
  356
+        """
  357
+        Internal implementation - Hook for tasks that should be performed before
  358
+        the ``CREATE DATABASE``/``DROP DATABASE`` clauses used by testing code
  359
+        to create/ destroy test databases. Needed e.g. in PostgreSQL to rollback
  360
+        and close any active transaction.
  361
+        """
  362
+        pass
356 363
 
357 364
     def sql_table_creation_suffix(self):
358 365
         "SQL to append to the end of the test table creation statements"
3  django/db/backends/oracle/creation.py
@@ -270,3 +270,6 @@ def test_db_signature(self):
270 270
             settings_dict['NAME'],
271 271
             self._test_database_user(),
272 272
         )
  273
+
  274
+    def set_autocommit(self):
  275
+        self.connection.connection.autocommit = True
8  django/db/backends/postgresql_psycopg2/creation.py
@@ -76,3 +76,11 @@ def get_index_sql(index_name, opclass=''):
76 76
         else:
77 77
             output = []
78 78
         return output
  79
+
  80
+    def set_autocommit(self):
  81
+        self._prepare_for_test_db_ddl()
  82
+
  83
+    def _prepare_for_test_db_ddl(self):
  84
+        """Rollback and close the active transaction."""
  85
+        self.connection.connection.rollback()
  86
+        self.connection.connection.set_isolation_level(0)
3  django/db/backends/sqlite3/creation.py
@@ -69,3 +69,6 @@ def _destroy_test_db(self, test_database_name, verbosity):
69 69
         if test_database_name and test_database_name != ":memory:":
70 70
             # Remove the SQLite database file
71 71
             os.remove(test_database_name)
  72
+
  73
+    def set_autocommit(self):
  74
+        self.connection.connection.isolation_level = None

0 notes on commit 569aa34

Please sign in to comment.
Something went wrong with that request. Please try again.