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 4.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import os
  2. import shutil
  3. import sys
  4. from django.db.backends.base.creation import BaseDatabaseCreation
  5. class DatabaseCreation(BaseDatabaseCreation):
  6. @staticmethod
  7. def is_in_memory_db(database_name):
  8. return database_name == ':memory:' or 'mode=memory' in database_name
  9. def _get_test_db_name(self):
  10. test_database_name = self.connection.settings_dict['TEST']['NAME'] or ':memory:'
  11. if test_database_name == ':memory:':
  12. return 'file:memorydb_%s?mode=memory&cache=shared' % self.connection.alias
  13. return test_database_name
  14. def _create_test_db(self, verbosity, autoclobber, keepdb=False):
  15. test_database_name = self._get_test_db_name()
  16. if keepdb:
  17. return test_database_name
  18. if not self.is_in_memory_db(test_database_name):
  19. # Erase the old test database
  20. if verbosity >= 1:
  21. self.log('Destroying old test database for alias %s...' % (
  22. self._get_database_display_str(verbosity, test_database_name),
  23. ))
  24. if os.access(test_database_name, os.F_OK):
  25. if not autoclobber:
  26. confirm = input(
  27. "Type 'yes' if you would like to try deleting the test "
  28. "database '%s', or 'no' to cancel: " % test_database_name
  29. )
  30. if autoclobber or confirm == 'yes':
  31. try:
  32. os.remove(test_database_name)
  33. except Exception as e:
  34. self.log('Got an error deleting the old test database: %s' % e)
  35. sys.exit(2)
  36. else:
  37. self.log('Tests cancelled.')
  38. sys.exit(1)
  39. return test_database_name
  40. def get_test_db_clone_settings(self, suffix):
  41. orig_settings_dict = self.connection.settings_dict
  42. source_database_name = orig_settings_dict['NAME']
  43. if self.is_in_memory_db(source_database_name):
  44. return orig_settings_dict
  45. else:
  46. root, ext = os.path.splitext(orig_settings_dict['NAME'])
  47. return {**orig_settings_dict, 'NAME': '{}_{}.{}'.format(root, suffix, ext)}
  48. def _clone_test_db(self, suffix, verbosity, keepdb=False):
  49. source_database_name = self.connection.settings_dict['NAME']
  50. target_database_name = self.get_test_db_clone_settings(suffix)['NAME']
  51. # Forking automatically makes a copy of an in-memory database.
  52. if not self.is_in_memory_db(source_database_name):
  53. # Erase the old test database
  54. if os.access(target_database_name, os.F_OK):
  55. if keepdb:
  56. return
  57. if verbosity >= 1:
  58. self.log('Destroying old test database for alias %s...' % (
  59. self._get_database_display_str(verbosity, target_database_name),
  60. ))
  61. try:
  62. os.remove(target_database_name)
  63. except Exception as e:
  64. self.log('Got an error deleting the old test database: %s' % e)
  65. sys.exit(2)
  66. try:
  67. shutil.copy(source_database_name, target_database_name)
  68. except Exception as e:
  69. self.log('Got an error cloning the test database: %s' % e)
  70. sys.exit(2)
  71. def _destroy_test_db(self, test_database_name, verbosity):
  72. if test_database_name and not self.is_in_memory_db(test_database_name):
  73. # Remove the SQLite database file
  74. os.remove(test_database_name)
  75. def test_db_signature(self):
  76. """
  77. Return a tuple that uniquely identifies a test database.
  78. This takes into account the special cases of ":memory:" and "" for
  79. SQLite since the databases will be distinct despite having the same
  80. TEST NAME. See https://www.sqlite.org/inmemorydb.html
  81. """
  82. test_database_name = self._get_test_db_name()
  83. sig = [self.connection.settings_dict['NAME']]
  84. if self.is_in_memory_db(test_database_name):
  85. sig.append(self.connection.alias)
  86. return tuple(sig)