Ticket #16250: 16250-3.diff

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

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

  • django/db/backends/creation.py

    diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
    a b  
    247247            verbosity=max(verbosity - 1, 0),
    248248            interactive=False,
    249249            database=self.connection.alias)
    250        
     250
    251251        # One effect of calling syncdb followed by flush is that the id of the
    252252        # default site may or may not be 1, depending on how the sequence was
    253253        # reset.  If the sites app is loaded, then we coerce it.
     
    294294        # if the database supports it because PostgreSQL doesn't allow
    295295        # CREATE/DROP DATABASE statements within transactions.
    296296        cursor = self.connection.cursor()
    297         self.set_autocommit()
     297        self.prepare_for_test_db_ddl()
    298298        try:
    299299            cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
    300300        except Exception, e:
     
    339339        # to do so, because it's not allowed to delete a database while being
    340340        # connected to it.
    341341        cursor = self.connection.cursor()
    342         self.set_autocommit()
     342        self.prepare_for_test_db_ddl()
    343343        time.sleep(1) # To avoid "database is being accessed by other users" errors.
    344344        cursor.execute("DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name))
    345345        self.connection.close()
    346346
    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)
     347    def prepare_for_test_db_ddl(self):
     348        """
     349        Hook for actions needed before the ``CREATE DATABASE``/``DROP DATABASE``
     350        clauses we execute on the test database.
     351        This is needed e.g. in PostgreSQL to rollback and close any active
     352        transaction.
     353        """
     354        pass
    356355
    357356    def sql_table_creation_suffix(self):
    358357        "SQL to append to the end of the test table creation statements"
  • django/db/backends/postgresql_psycopg2/creation.py

    diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
    a b  
    7676        else:
    7777            output = []
    7878        return output
     79
     80    def prepare_for_test_db_ddl(self):
     81        """Rollback and close the active transaction."""
     82        self.connection.connection.rollback()
     83        self.connection.connection.set_isolation_level(0)
Back to Top