2015-02-10 05:00:09 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.creation import BaseDatabaseCreation
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2015-02-10 05:00:09 +08:00
|
|
|
from .client import DatabaseClient
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
class DatabaseCreation(BaseDatabaseCreation):
|
2014-08-13 00:14:06 +08:00
|
|
|
|
2008-08-11 20:11:25 +08:00
|
|
|
def sql_table_creation_suffix(self):
|
|
|
|
suffix = []
|
2014-01-20 08:45:29 +08:00
|
|
|
test_settings = self.connection.settings_dict['TEST']
|
|
|
|
if test_settings['CHARSET']:
|
|
|
|
suffix.append('CHARACTER SET %s' % test_settings['CHARSET'])
|
|
|
|
if test_settings['COLLATION']:
|
|
|
|
suffix.append('COLLATE %s' % test_settings['COLLATION'])
|
2008-08-11 20:11:25 +08:00
|
|
|
return ' '.join(suffix)
|
2015-02-10 05:00:09 +08:00
|
|
|
|
2017-04-14 02:20:01 +08:00
|
|
|
def _execute_create_test_db(self, cursor, parameters, keepdb=False):
|
|
|
|
try:
|
2018-10-26 07:37:41 +08:00
|
|
|
super()._execute_create_test_db(cursor, parameters, keepdb)
|
2017-04-14 02:20:01 +08:00
|
|
|
except Exception as e:
|
|
|
|
if len(e.args) < 1 or e.args[0] != 1007:
|
|
|
|
# All errors except "database exists" (1007) cancel tests.
|
2018-07-20 06:05:33 +08:00
|
|
|
self.log('Got an error creating the test database: %s' % e)
|
2017-04-14 02:20:01 +08:00
|
|
|
sys.exit(2)
|
|
|
|
else:
|
|
|
|
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
|
|
|
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-14 02:20:01 +08:00
|
|
|
test_db_params = {
|
|
|
|
'dbname': self.connection.ops.quote_name(target_database_name),
|
|
|
|
'suffix': self.sql_table_creation_suffix(),
|
|
|
|
}
|
2015-02-10 05:00:09 +08:00
|
|
|
with self._nodb_connection.cursor() as cursor:
|
|
|
|
try:
|
2017-04-14 02:20:01 +08:00
|
|
|
self._execute_create_test_db(cursor, test_db_params, keepdb)
|
2017-01-25 22:14:05 +08:00
|
|
|
except Exception:
|
2018-10-26 07:37:41 +08:00
|
|
|
if keepdb:
|
|
|
|
# If the database should be kept, skip everything else.
|
|
|
|
return
|
2015-02-10 05:00:09 +08:00
|
|
|
try:
|
|
|
|
if verbosity >= 1:
|
2018-08-22 21:13:58 +08:00
|
|
|
self.log('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-14 02:20:01 +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:
|
2018-07-20 06:05:33 +08:00
|
|
|
self.log('Got an error recreating the test database: %s' % e)
|
2015-02-10 05:00:09 +08:00
|
|
|
sys.exit(2)
|
2018-10-26 07:37:41 +08:00
|
|
|
self._clone_db(source_database_name, target_database_name)
|
2015-02-10 05:00:09 +08:00
|
|
|
|
2018-10-26 07:37:41 +08:00
|
|
|
def _clone_db(self, source_database_name, target_database_name):
|
2018-10-30 02:45:30 +08:00
|
|
|
dump_args = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)[1:]
|
|
|
|
dump_args[-1] = source_database_name
|
|
|
|
dump_cmd = ['mysqldump', '--routines', '--events'] + dump_args
|
2015-02-10 05:00:09 +08:00
|
|
|
load_cmd = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
|
|
|
|
load_cmd[-1] = target_database_name
|
|
|
|
|
2018-10-02 22:23:21 +08:00
|
|
|
with subprocess.Popen(dump_cmd, stdout=subprocess.PIPE) as dump_proc:
|
|
|
|
with subprocess.Popen(load_cmd, stdin=dump_proc.stdout, stdout=subprocess.DEVNULL):
|
|
|
|
# Allow dump_proc to receive a SIGPIPE if the load process exits.
|
|
|
|
dump_proc.stdout.close()
|