summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql/creation.py
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2016-08-15 07:10:40 -0700
committerTim Graham <timograham@gmail.com>2016-08-23 15:08:20 -0400
commita3db480393d0065fc69834057f0e02a4afc65df9 (patch)
tree5595ed9baf8a4a60e3a8c16707f7690fda55c5da /django/db/backends/postgresql/creation.py
parentbc1e2d8e8edde6cc7d2657c68242a13ee65a15b8 (diff)
downloaddjango-a3db480393d0065fc69834057f0e02a4afc65df9.tar.gz
Fixed #27061 -- Added a TEST['TEMPLATE'] setting for PostgreSQL.
Diffstat (limited to 'django/db/backends/postgresql/creation.py')
-rw-r--r--django/db/backends/postgresql/creation.py31
1 files changed, 22 insertions, 9 deletions
diff --git a/django/db/backends/postgresql/creation.py b/django/db/backends/postgresql/creation.py
index 3efcfc7692..2f26b14e52 100644
--- a/django/db/backends/postgresql/creation.py
+++ b/django/db/backends/postgresql/creation.py
@@ -5,28 +5,42 @@ from django.db.backends.base.creation import BaseDatabaseCreation
class DatabaseCreation(BaseDatabaseCreation):
+ def _quote_name(self, name):
+ return self.connection.ops.quote_name(name)
+
+ def _get_database_create_suffix(self, encoding=None, template=None):
+ suffix = ""
+ if encoding:
+ suffix += " ENCODING '{}'".format(encoding)
+ if template:
+ suffix += " TEMPLATE {}".format(self._quote_name(template))
+ if suffix:
+ suffix = "WITH" + suffix
+ return suffix
+
def sql_table_creation_suffix(self):
test_settings = self.connection.settings_dict['TEST']
assert test_settings['COLLATION'] is None, (
"PostgreSQL does not support collation setting at database creation time."
)
- if test_settings['CHARSET']:
- return "WITH ENCODING '%s'" % test_settings['CHARSET']
- return ''
+ return self._get_database_create_suffix(
+ encoding=test_settings['CHARSET'],
+ template=test_settings.get('TEMPLATE'),
+ )
def _clone_test_db(self, number, verbosity, keepdb=False):
# CREATE DATABASE ... WITH TEMPLATE ... requires closing connections
# to the template database.
self.connection.close()
- qn = self.connection.ops.quote_name
source_database_name = self.connection.settings_dict['NAME']
target_database_name = self.get_test_db_clone_settings(number)['NAME']
+ suffix = self._get_database_create_suffix(template=source_database_name)
+ creation_sql = "CREATE DATABASE {} {}".format(self._quote_name(target_database_name), suffix)
with self._nodb_connection.cursor() as cursor:
try:
- cursor.execute("CREATE DATABASE %s WITH TEMPLATE %s" % (
- qn(target_database_name), qn(source_database_name)))
+ cursor.execute(creation_sql)
except Exception as e:
if keepdb:
return
@@ -35,9 +49,8 @@ class DatabaseCreation(BaseDatabaseCreation):
print("Destroying old test database for alias %s..." % (
self._get_database_display_str(verbosity, target_database_name),
))
- cursor.execute("DROP DATABASE %s" % qn(target_database_name))
- cursor.execute("CREATE DATABASE %s WITH TEMPLATE %s" % (
- qn(target_database_name), qn(source_database_name)))
+ cursor.execute("DROP DATABASE %s" % self._quote_name(target_database_name))
+ cursor.execute(creation_sql)
except Exception as e:
sys.stderr.write("Got an error cloning the test database: %s\n" % e)
sys.exit(2)