diff --git a/django/db/backends/creation.py b/django/db/backends/creation.py
index e4c870c..ecd1070 100644
|
a
|
b
|
import warnings
|
| 6 | 6 | from django.conf import settings |
| 7 | 7 | from django.db.utils import load_backend |
| 8 | 8 | from django.utils.encoding import force_bytes |
| | 9 | from django.utils.functional import cached_property |
| 9 | 10 | from django.utils.six.moves import input |
| 10 | 11 | |
| 11 | 12 | from .utils import truncate_name |
| … |
… |
class BaseDatabaseCreation(object):
|
| 29 | 30 | def __init__(self, connection): |
| 30 | 31 | self.connection = connection |
| 31 | 32 | |
| | 33 | @cached_property |
| | 34 | def nodb_connection(self): |
| | 35 | """ |
| | 36 | Alternative connection to be used when there is no need to access |
| | 37 | the main database, specifically for test db creation/deletion. |
| | 38 | This also prevents the production database from being exposed to |
| | 39 | potential child threads while (or after) the test database is destroyed. |
| | 40 | Refs #10868 and #17786. |
| | 41 | """ |
| | 42 | settings_dict = self.connection.settings_dict.copy() |
| | 43 | settings_dict['NAME'] = None |
| | 44 | backend = load_backend(settings_dict['ENGINE']) |
| | 45 | nodb_connection = backend.DatabaseWrapper( |
| | 46 | settings_dict, |
| | 47 | alias='__no_db__', |
| | 48 | allow_thread_sharing=False) |
| | 49 | return nodb_connection |
| | 50 | |
| 32 | 51 | @classmethod |
| 33 | 52 | def _digest(cls, *args): |
| 34 | 53 | """ |
| … |
… |
class BaseDatabaseCreation(object):
|
| 386 | 405 | qn = self.connection.ops.quote_name |
| 387 | 406 | |
| 388 | 407 | # Create the test database and connect to it. |
| 389 | | cursor = self.connection.cursor() |
| | 408 | cursor = self.nodb_connection.cursor() |
| 390 | 409 | try: |
| 391 | 410 | cursor.execute( |
| 392 | 411 | "CREATE DATABASE %s %s" % (qn(test_database_name), suffix)) |
| … |
… |
class BaseDatabaseCreation(object):
|
| 431 | 450 | print("Destroying test database for alias '%s'%s..." % ( |
| 432 | 451 | self.connection.alias, test_db_repr)) |
| 433 | 452 | |
| 434 | | # Temporarily use a new connection and a copy of the settings dict. |
| 435 | | # This prevents the production database from being exposed to potential |
| 436 | | # child threads while (or after) the test database is destroyed. |
| 437 | | # Refs #10868 and #17786. |
| 438 | | settings_dict = self.connection.settings_dict.copy() |
| 439 | | settings_dict['NAME'] = old_database_name |
| 440 | | backend = load_backend(settings_dict['ENGINE']) |
| 441 | | new_connection = backend.DatabaseWrapper( |
| 442 | | settings_dict, |
| 443 | | alias='__destroy_test_db__', |
| 444 | | allow_thread_sharing=False) |
| 445 | | new_connection.creation._destroy_test_db(test_database_name, verbosity) |
| | 453 | self._destroy_test_db(test_database_name, verbosity) |
| 446 | 454 | |
| 447 | 455 | def _destroy_test_db(self, test_database_name, verbosity): |
| 448 | 456 | """ |
| … |
… |
class BaseDatabaseCreation(object):
|
| 452 | 460 | # ourselves. Connect to the previous database (not the test database) |
| 453 | 461 | # to do so, because it's not allowed to delete a database while being |
| 454 | 462 | # connected to it. |
| 455 | | cursor = self.connection.cursor() |
| | 463 | cursor = self.nodb_connection.cursor() |
| 456 | 464 | # Wait to avoid "database is being accessed by other users" errors. |
| 457 | 465 | time.sleep(1) |
| 458 | 466 | cursor.execute("DROP DATABASE %s" |
| 459 | 467 | % self.connection.ops.quote_name(test_database_name)) |
| 460 | | self.connection.close() |
| 461 | 468 | |
| 462 | 469 | def set_autocommit(self): |
| 463 | 470 | """ |
diff --git a/django/db/backends/postgresql_psycopg2/base.py b/django/db/backends/postgresql_psycopg2/base.py
index b22c653..cb23c33 100644
|
a
|
b
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
| 101 | 101 | |
| 102 | 102 | def get_connection_params(self): |
| 103 | 103 | settings_dict = self.settings_dict |
| 104 | | if not settings_dict['NAME']: |
| | 104 | # None may be used to connect to the default 'postgres' db |
| | 105 | if settings_dict['NAME'] == '': |
| 105 | 106 | from django.core.exceptions import ImproperlyConfigured |
| 106 | 107 | raise ImproperlyConfigured( |
| 107 | 108 | "settings.DATABASES is improperly configured. " |
| 108 | 109 | "Please supply the NAME value.") |
| 109 | 110 | conn_params = { |
| 110 | | 'database': settings_dict['NAME'], |
| | 111 | 'database': settings_dict['NAME'] or 'postgres', |
| 111 | 112 | } |
| 112 | 113 | conn_params.update(settings_dict['OPTIONS']) |
| 113 | 114 | if 'autocommit' in conn_params: |