Fixed #16250 -- Made test database creation support code in the PostgreSQL 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
This commit is contained in:
parent
21e0b3a243
commit
569aa34ea5
|
@ -247,7 +247,7 @@ class BaseDatabaseCreation(object):
|
||||||
verbosity=max(verbosity - 1, 0),
|
verbosity=max(verbosity - 1, 0),
|
||||||
interactive=False,
|
interactive=False,
|
||||||
database=self.connection.alias)
|
database=self.connection.alias)
|
||||||
|
|
||||||
# One effect of calling syncdb followed by flush is that the id of the
|
# One effect of calling syncdb followed by flush is that the id of the
|
||||||
# default site may or may not be 1, depending on how the sequence was
|
# default site may or may not be 1, depending on how the sequence was
|
||||||
# reset. If the sites app is loaded, then we coerce it.
|
# reset. If the sites app is loaded, then we coerce it.
|
||||||
|
@ -294,7 +294,7 @@ class BaseDatabaseCreation(object):
|
||||||
# if the database supports it because PostgreSQL doesn't allow
|
# if the database supports it because PostgreSQL doesn't allow
|
||||||
# CREATE/DROP DATABASE statements within transactions.
|
# CREATE/DROP DATABASE statements within transactions.
|
||||||
cursor = self.connection.cursor()
|
cursor = self.connection.cursor()
|
||||||
self.set_autocommit()
|
self._prepare_for_test_db_ddl()
|
||||||
try:
|
try:
|
||||||
cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
|
cursor.execute("CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
|
||||||
except Exception, e:
|
except Exception, e:
|
||||||
|
@ -339,20 +339,27 @@ class BaseDatabaseCreation(object):
|
||||||
# to do so, because it's not allowed to delete a database while being
|
# to do so, because it's not allowed to delete a database while being
|
||||||
# connected to it.
|
# connected to it.
|
||||||
cursor = self.connection.cursor()
|
cursor = self.connection.cursor()
|
||||||
self.set_autocommit()
|
self._prepare_for_test_db_ddl()
|
||||||
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
time.sleep(1) # To avoid "database is being accessed by other users" errors.
|
||||||
cursor.execute("DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name))
|
cursor.execute("DROP DATABASE %s" % self.connection.ops.quote_name(test_database_name))
|
||||||
self.connection.close()
|
self.connection.close()
|
||||||
|
|
||||||
def set_autocommit(self):
|
def set_autocommit(self):
|
||||||
"Make sure a connection is in autocommit mode."
|
"""
|
||||||
if hasattr(self.connection.connection, "autocommit"):
|
Make sure a connection is in autocommit mode. - Deprecated, not used
|
||||||
if callable(self.connection.connection.autocommit):
|
anymore by Django code. Kept for compatibility with user code that
|
||||||
self.connection.connection.autocommit(True)
|
might use it.
|
||||||
else:
|
"""
|
||||||
self.connection.connection.autocommit = True
|
pass
|
||||||
elif hasattr(self.connection.connection, "set_isolation_level"):
|
|
||||||
self.connection.connection.set_isolation_level(0)
|
def _prepare_for_test_db_ddl(self):
|
||||||
|
"""
|
||||||
|
Internal implementation - Hook for tasks that should be performed before
|
||||||
|
the ``CREATE DATABASE``/``DROP DATABASE`` clauses used by testing code
|
||||||
|
to create/ destroy test databases. Needed e.g. in PostgreSQL to rollback
|
||||||
|
and close any active transaction.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
def sql_table_creation_suffix(self):
|
def sql_table_creation_suffix(self):
|
||||||
"SQL to append to the end of the test table creation statements"
|
"SQL to append to the end of the test table creation statements"
|
||||||
|
|
|
@ -270,3 +270,6 @@ class DatabaseCreation(BaseDatabaseCreation):
|
||||||
settings_dict['NAME'],
|
settings_dict['NAME'],
|
||||||
self._test_database_user(),
|
self._test_database_user(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def set_autocommit(self):
|
||||||
|
self.connection.connection.autocommit = True
|
||||||
|
|
|
@ -76,3 +76,11 @@ class DatabaseCreation(BaseDatabaseCreation):
|
||||||
else:
|
else:
|
||||||
output = []
|
output = []
|
||||||
return output
|
return output
|
||||||
|
|
||||||
|
def set_autocommit(self):
|
||||||
|
self._prepare_for_test_db_ddl()
|
||||||
|
|
||||||
|
def _prepare_for_test_db_ddl(self):
|
||||||
|
"""Rollback and close the active transaction."""
|
||||||
|
self.connection.connection.rollback()
|
||||||
|
self.connection.connection.set_isolation_level(0)
|
||||||
|
|
|
@ -69,3 +69,6 @@ class DatabaseCreation(BaseDatabaseCreation):
|
||||||
if test_database_name and test_database_name != ":memory:":
|
if test_database_name and test_database_name != ":memory:":
|
||||||
# Remove the SQLite database file
|
# Remove the SQLite database file
|
||||||
os.remove(test_database_name)
|
os.remove(test_database_name)
|
||||||
|
|
||||||
|
def set_autocommit(self):
|
||||||
|
self.connection.connection.isolation_level = None
|
||||||
|
|
Loading…
Reference in New Issue