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.
 
 
 
 

44 lines
1.8 KiB

  1. import sys
  2. from django.db.backends.base.creation import BaseDatabaseCreation
  3. class DatabaseCreation(BaseDatabaseCreation):
  4. def sql_table_creation_suffix(self):
  5. test_settings = self.connection.settings_dict['TEST']
  6. assert test_settings['COLLATION'] is None, (
  7. "PostgreSQL does not support collation setting at database creation time."
  8. )
  9. if test_settings['CHARSET']:
  10. return "WITH ENCODING '%s'" % test_settings['CHARSET']
  11. return ''
  12. def _clone_test_db(self, number, verbosity, keepdb=False):
  13. # CREATE DATABASE ... WITH TEMPLATE ... requires closing connections
  14. # to the template database.
  15. self.connection.close()
  16. qn = self.connection.ops.quote_name
  17. source_database_name = self.connection.settings_dict['NAME']
  18. target_database_name = self.get_test_db_clone_settings(number)['NAME']
  19. with self._nodb_connection.cursor() as cursor:
  20. try:
  21. cursor.execute("CREATE DATABASE %s WITH TEMPLATE %s" % (
  22. qn(target_database_name), qn(source_database_name)))
  23. except Exception as e:
  24. if keepdb:
  25. return
  26. try:
  27. if verbosity >= 1:
  28. print("Destroying old test database for alias %s..." % (
  29. self._get_database_display_str(verbosity, target_database_name),
  30. ))
  31. cursor.execute("DROP DATABASE %s" % qn(target_database_name))
  32. cursor.execute("CREATE DATABASE %s WITH TEMPLATE %s" % (
  33. qn(target_database_name), qn(source_database_name)))
  34. except Exception as e:
  35. sys.stderr.write("Got an error cloning the test database: %s\n" % e)
  36. sys.exit(2)