summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/creation.py
diff options
context:
space:
mode:
Diffstat (limited to 'django/db/backends/postgresql/creation.py')
-rw-r--r--django/db/backends/postgresql/creation.py47
1 files changed, 27 insertions, 20 deletions
diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py
index eb8ac3bcf5..70c3eda566 100644
--- a/django/db/backends/postgresql/creation.py
+++ b/django/db/backends/postgresql/creation.py
@@ -8,7 +8,6 @@ from django.db.backends.utils import strip_quotes
class DatabaseCreation(BaseDatabaseCreation):
-
def _quote_name(self, name):
return self.connection.ops.quote_name(name)
@@ -21,32 +20,35 @@ class DatabaseCreation(BaseDatabaseCreation):
return suffix and "WITH" + suffix
def sql_table_creation_suffix(self):
- test_settings = self.connection.settings_dict['TEST']
- if test_settings.get('COLLATION') is not None:
+ test_settings = self.connection.settings_dict["TEST"]
+ if test_settings.get("COLLATION") is not None:
raise ImproperlyConfigured(
- 'PostgreSQL does not support collation setting at database '
- 'creation time.'
+ "PostgreSQL does not support collation setting at database "
+ "creation time."
)
return self._get_database_create_suffix(
- encoding=test_settings['CHARSET'],
- template=test_settings.get('TEMPLATE'),
+ encoding=test_settings["CHARSET"],
+ template=test_settings.get("TEMPLATE"),
)
def _database_exists(self, cursor, database_name):
- cursor.execute('SELECT 1 FROM pg_catalog.pg_database WHERE datname = %s', [strip_quotes(database_name)])
+ cursor.execute(
+ "SELECT 1 FROM pg_catalog.pg_database WHERE datname = %s",
+ [strip_quotes(database_name)],
+ )
return cursor.fetchone() is not None
def _execute_create_test_db(self, cursor, parameters, keepdb=False):
try:
- if keepdb and self._database_exists(cursor, parameters['dbname']):
+ if keepdb and self._database_exists(cursor, parameters["dbname"]):
# If the database should be kept and it already exists, don't
# try to create a new one.
return
super()._execute_create_test_db(cursor, parameters, keepdb)
except Exception as e:
- if getattr(e.__cause__, 'pgcode', '') != errorcodes.DUPLICATE_DATABASE:
+ if getattr(e.__cause__, "pgcode", "") != errorcodes.DUPLICATE_DATABASE:
# All errors except "database already exists" cancel tests.
- self.log('Got an error creating the test database: %s' % e)
+ self.log("Got an error creating the test database: %s" % e)
sys.exit(2)
elif not keepdb:
# If the database should be kept, ignore "database already
@@ -58,11 +60,11 @@ class DatabaseCreation(BaseDatabaseCreation):
# to the template database.
self.connection.close()
- source_database_name = self.connection.settings_dict['NAME']
- target_database_name = self.get_test_db_clone_settings(suffix)['NAME']
+ source_database_name = self.connection.settings_dict["NAME"]
+ target_database_name = self.get_test_db_clone_settings(suffix)["NAME"]
test_db_params = {
- 'dbname': self._quote_name(target_database_name),
- 'suffix': self._get_database_create_suffix(template=source_database_name),
+ "dbname": self._quote_name(target_database_name),
+ "suffix": self._get_database_create_suffix(template=source_database_name),
}
with self._nodb_cursor() as cursor:
try:
@@ -70,11 +72,16 @@ class DatabaseCreation(BaseDatabaseCreation):
except Exception:
try:
if verbosity >= 1:
- self.log('Destroying old test database for alias %s...' % (
- self._get_database_display_str(verbosity, target_database_name),
- ))
- cursor.execute('DROP DATABASE %(dbname)s' % test_db_params)
+ self.log(
+ "Destroying old test database for alias %s..."
+ % (
+ self._get_database_display_str(
+ verbosity, target_database_name
+ ),
+ )
+ )
+ cursor.execute("DROP DATABASE %(dbname)s" % test_db_params)
self._execute_create_test_db(cursor, test_db_params, keepdb)
except Exception as e:
- self.log('Got an error cloning the test database: %s' % e)
+ self.log("Got an error cloning the test database: %s" % e)
sys.exit(2)