diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
a
|
b
|
|
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 | 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 | 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 | | 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 |
356 | 355 | |
357 | 356 | def sql_table_creation_suffix(self): |
358 | 357 | "SQL to append to the end of the test table creation statements" |
diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
a
|
b
|
|
76 | 76 | else: |
77 | 77 | output = [] |
78 | 78 | 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) |