You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

creation.py 2.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import subprocess
  2. import sys
  3. from django.db.backends.base.creation import BaseDatabaseCreation
  4. from .client import DatabaseClient
  5. class DatabaseCreation(BaseDatabaseCreation):
  6. def sql_table_creation_suffix(self):
  7. suffix = []
  8. test_settings = self.connection.settings_dict['TEST']
  9. if test_settings['CHARSET']:
  10. suffix.append('CHARACTER SET %s' % test_settings['CHARSET'])
  11. if test_settings['COLLATION']:
  12. suffix.append('COLLATE %s' % test_settings['COLLATION'])
  13. return ' '.join(suffix)
  14. def _clone_test_db(self, number, verbosity, keepdb=False):
  15. qn = self.connection.ops.quote_name
  16. source_database_name = self.connection.settings_dict['NAME']
  17. target_database_name = self.get_test_db_clone_settings(number)['NAME']
  18. with self._nodb_connection.cursor() as cursor:
  19. try:
  20. cursor.execute("CREATE DATABASE %s" % qn(target_database_name))
  21. except Exception as e:
  22. if keepdb:
  23. return
  24. try:
  25. if verbosity >= 1:
  26. print("Destroying old test database for alias %s..." % (
  27. self._get_database_display_str(verbosity, target_database_name),
  28. ))
  29. cursor.execute("DROP DATABASE %s" % qn(target_database_name))
  30. cursor.execute("CREATE DATABASE %s" % qn(target_database_name))
  31. except Exception as e:
  32. sys.stderr.write("Got an error recreating the test database: %s\n" % e)
  33. sys.exit(2)
  34. dump_cmd = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
  35. dump_cmd[0] = 'mysqldump'
  36. dump_cmd[-1] = source_database_name
  37. load_cmd = DatabaseClient.settings_to_cmd_args(self.connection.settings_dict)
  38. load_cmd[-1] = target_database_name
  39. dump_proc = subprocess.Popen(dump_cmd, stdout=subprocess.PIPE)
  40. load_proc = subprocess.Popen(load_cmd, stdin=dump_proc.stdout, stdout=subprocess.PIPE)
  41. dump_proc.stdout.close() # allow dump_proc to receive a SIGPIPE if load_proc exits.
  42. load_proc.communicate()