2015-02-10 05:00:09 +08:00
|
|
|
import sys
|
|
|
|
|
2017-04-25 12:01:25 +08:00
|
|
|
from psycopg2 import errorcodes
|
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.creation import BaseDatabaseCreation
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2011-04-02 16:39:08 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
2012-09-08 03:40:59 +08:00
|
|
|
|
2016-08-15 22:10:40 +08:00
|
|
|
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
|
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def sql_table_creation_suffix(self):
|
2014-01-20 08:45:29 +08:00
|
|
|
test_settings = self.connection.settings_dict['TEST']
|
2014-09-04 20:15:09 +08:00
|
|
|
assert test_settings['COLLATION'] is None, (
|
|
|
|
"PostgreSQL does not support collation setting at database creation time."
|
|
|
|
)
|
2016-08-15 22:10:40 +08:00
|
|
|
return self._get_database_create_suffix(
|
|
|
|
encoding=test_settings['CHARSET'],
|
|
|
|
template=test_settings.get('TEMPLATE'),
|
|
|
|
)
|
2015-02-10 05:00:09 +08:00
|
|
|
|
2017-04-11 01:04:00 +08:00
|
|
|
def _execute_create_test_db(self, cursor, parameters, keepdb=False):
|
|
|
|
try:
|
|
|
|
super()._execute_create_test_db(cursor, parameters, keepdb)
|
|
|
|
except Exception as e:
|
2017-04-25 12:01:25 +08:00
|
|
|
if getattr(e.__cause__, 'pgcode', '') != errorcodes.DUPLICATE_DATABASE:
|
|
|
|
# All errors except "database already exists" cancel tests.
|
2017-04-11 01:04:00 +08:00
|
|
|
sys.stderr.write('Got an error creating the test database: %s\n' % e)
|
|
|
|
sys.exit(2)
|
|
|
|
elif not keepdb:
|
|
|
|
# If the database should be kept, ignore "database already
|
|
|
|
# exists".
|
|
|
|
raise e
|
|
|
|
|
2017-09-11 23:32:40 +08:00
|
|
|
def _clone_test_db(self, suffix, verbosity, keepdb=False):
|
2015-02-10 05:00:09 +08:00
|
|
|
# CREATE DATABASE ... WITH TEMPLATE ... requires closing connections
|
|
|
|
# to the template database.
|
|
|
|
self.connection.close()
|
|
|
|
|
|
|
|
source_database_name = self.connection.settings_dict['NAME']
|
2017-09-11 23:32:40 +08:00
|
|
|
target_database_name = self.get_test_db_clone_settings(suffix)['NAME']
|
2017-04-11 01:04:00 +08:00
|
|
|
test_db_params = {
|
|
|
|
'dbname': self._quote_name(target_database_name),
|
|
|
|
'suffix': self._get_database_create_suffix(template=source_database_name),
|
|
|
|
}
|
2015-02-10 05:00:09 +08:00
|
|
|
with self._nodb_connection.cursor() as cursor:
|
|
|
|
try:
|
2017-04-11 01:04:00 +08:00
|
|
|
self._execute_create_test_db(cursor, test_db_params, keepdb)
|
|
|
|
except Exception as e:
|
2015-02-10 05:00:09 +08:00
|
|
|
try:
|
|
|
|
if verbosity >= 1:
|
2015-07-30 18:56:54 +08:00
|
|
|
print("Destroying old test database for alias %s..." % (
|
2015-11-14 00:28:20 +08:00
|
|
|
self._get_database_display_str(verbosity, target_database_name),
|
2015-07-30 18:56:54 +08:00
|
|
|
))
|
2017-04-11 01:04:00 +08:00
|
|
|
cursor.execute('DROP DATABASE %(dbname)s' % test_db_params)
|
|
|
|
self._execute_create_test_db(cursor, test_db_params, keepdb)
|
2015-02-10 05:00:09 +08:00
|
|
|
except Exception as e:
|
|
|
|
sys.stderr.write("Got an error cloning the test database: %s\n" % e)
|
|
|
|
sys.exit(2)
|