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 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import sys
  2. import time
  3. from django.apps import apps
  4. from django.conf import settings
  5. from django.core import serializers
  6. from django.db import router
  7. from django.utils.six import StringIO
  8. from django.utils.six.moves import input
  9. # The prefix to put on the default database name when creating
  10. # the test database.
  11. TEST_DATABASE_PREFIX = 'test_'
  12. class BaseDatabaseCreation(object):
  13. """
  14. This class encapsulates all backend-specific differences that pertain to
  15. creation and destruction of the test database.
  16. """
  17. def __init__(self, connection):
  18. self.connection = connection
  19. @property
  20. def _nodb_connection(self):
  21. """
  22. Used to be defined here, now moved to DatabaseWrapper.
  23. """
  24. return self.connection._nodb_connection
  25. def create_test_db(self, verbosity=1, autoclobber=False, serialize=True, keepdb=False):
  26. """
  27. Creates a test database, prompting the user for confirmation if the
  28. database already exists. Returns the name of the test database created.
  29. """
  30. # Don't import django.core.management if it isn't needed.
  31. from django.core.management import call_command
  32. test_database_name = self._get_test_db_name()
  33. if verbosity >= 1:
  34. action = 'Creating'
  35. if keepdb:
  36. action = "Using existing"
  37. print("%s test database for alias %s..." % (
  38. action,
  39. self._get_database_display_str(verbosity, test_database_name),
  40. ))
  41. # We could skip this call if keepdb is True, but we instead
  42. # give it the keepdb param. This is to handle the case
  43. # where the test DB doesn't exist, in which case we need to
  44. # create it, then just not destroy it. If we instead skip
  45. # this, we will get an exception.
  46. self._create_test_db(verbosity, autoclobber, keepdb)
  47. self.connection.close()
  48. settings.DATABASES[self.connection.alias]["NAME"] = test_database_name
  49. self.connection.settings_dict["NAME"] = test_database_name
  50. # We report migrate messages at one level lower than that requested.
  51. # This ensures we don't get flooded with messages during testing
  52. # (unless you really ask to be flooded).
  53. call_command(
  54. 'migrate',
  55. verbosity=max(verbosity - 1, 0),
  56. interactive=False,
  57. database=self.connection.alias,
  58. run_syncdb=True,
  59. )
  60. # We then serialize the current state of the database into a string
  61. # and store it on the connection. This slightly horrific process is so people
  62. # who are testing on databases without transactions or who are using
  63. # a TransactionTestCase still get a clean database on every test run.
  64. if serialize:
  65. self.connection._test_serialized_contents = self.serialize_db_to_string()
  66. call_command('createcachetable', database=self.connection.alias)
  67. # Ensure a connection for the side effect of initializing the test database.
  68. self.connection.ensure_connection()
  69. return test_database_name
  70. def set_as_test_mirror(self, primary_settings_dict):
  71. """
  72. Set this database up to be used in testing as a mirror of a primary database
  73. whose settings are given
  74. """
  75. self.connection.settings_dict['NAME'] = primary_settings_dict['NAME']
  76. def serialize_db_to_string(self):
  77. """
  78. Serializes all data in the database into a JSON string.
  79. Designed only for test runner usage; will not handle large
  80. amounts of data.
  81. """
  82. # Build list of all apps to serialize
  83. from django.db.migrations.loader import MigrationLoader
  84. loader = MigrationLoader(self.connection)
  85. app_list = []
  86. for app_config in apps.get_app_configs():
  87. if (
  88. app_config.models_module is not None and
  89. app_config.label in loader.migrated_apps and
  90. app_config.name not in settings.TEST_NON_SERIALIZED_APPS
  91. ):
  92. app_list.append((app_config, None))
  93. # Make a function to iteratively return every object
  94. def get_objects():
  95. for model in serializers.sort_dependencies(app_list):
  96. if (model._meta.can_migrate(self.connection) and
  97. router.allow_migrate_model(self.connection.alias, model)):
  98. queryset = model._default_manager.using(self.connection.alias).order_by(model._meta.pk.name)
  99. for obj in queryset.iterator():
  100. yield obj
  101. # Serialize to a string
  102. out = StringIO()
  103. serializers.serialize("json", get_objects(), indent=None, stream=out)
  104. return out.getvalue()
  105. def deserialize_db_from_string(self, data):
  106. """
  107. Reloads the database with data from a string generated by
  108. the serialize_db_to_string method.
  109. """
  110. data = StringIO(data)
  111. for obj in serializers.deserialize("json", data, using=self.connection.alias):
  112. obj.save()
  113. def _get_database_display_str(self, verbosity, database_name):
  114. """
  115. Return display string for a database for use in various actions.
  116. """
  117. return "'%s'%s" % (
  118. self.connection.alias,
  119. (" ('%s')" % database_name) if verbosity >= 2 else '',
  120. )
  121. def _get_test_db_name(self):
  122. """
  123. Internal implementation - returns the name of the test DB that will be
  124. created. Only useful when called from create_test_db() and
  125. _create_test_db() and when no external munging is done with the 'NAME'
  126. settings.
  127. """
  128. if self.connection.settings_dict['TEST']['NAME']:
  129. return self.connection.settings_dict['TEST']['NAME']
  130. return TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']
  131. def _create_test_db(self, verbosity, autoclobber, keepdb=False):
  132. """
  133. Internal implementation - creates the test db tables.
  134. """
  135. suffix = self.sql_table_creation_suffix()
  136. test_database_name = self._get_test_db_name()
  137. qn = self.connection.ops.quote_name
  138. # Create the test database and connect to it.
  139. with self._nodb_connection.cursor() as cursor:
  140. try:
  141. cursor.execute(
  142. "CREATE DATABASE %s %s" % (qn(test_database_name), suffix))
  143. except Exception as e:
  144. # if we want to keep the db, then no need to do any of the below,
  145. # just return and skip it all.
  146. if keepdb:
  147. return test_database_name
  148. sys.stderr.write(
  149. "Got an error creating the test database: %s\n" % e)
  150. if not autoclobber:
  151. confirm = input(
  152. "Type 'yes' if you would like to try deleting the test "
  153. "database '%s', or 'no' to cancel: " % test_database_name)
  154. if autoclobber or confirm == 'yes':
  155. try:
  156. if verbosity >= 1:
  157. print("Destroying old test database for alias %s..." % (
  158. self._get_database_display_str(verbosity, test_database_name),
  159. ))
  160. cursor.execute(
  161. "DROP DATABASE %s" % qn(test_database_name))
  162. cursor.execute(
  163. "CREATE DATABASE %s %s" % (qn(test_database_name),
  164. suffix))
  165. except Exception as e:
  166. sys.stderr.write(
  167. "Got an error recreating the test database: %s\n" % e)
  168. sys.exit(2)
  169. else:
  170. print("Tests cancelled.")
  171. sys.exit(1)
  172. return test_database_name
  173. def clone_test_db(self, number, verbosity=1, autoclobber=False, keepdb=False):
  174. """
  175. Clone a test database.
  176. """
  177. source_database_name = self.connection.settings_dict['NAME']
  178. if verbosity >= 1:
  179. action = 'Cloning test database'
  180. if keepdb:
  181. action = 'Using existing clone'
  182. print("%s for alias %s..." % (
  183. action,
  184. self._get_database_display_str(verbosity, source_database_name),
  185. ))
  186. # We could skip this call if keepdb is True, but we instead
  187. # give it the keepdb param. See create_test_db for details.
  188. self._clone_test_db(number, verbosity, keepdb)
  189. def get_test_db_clone_settings(self, number):
  190. """
  191. Return a modified connection settings dict for the n-th clone of a DB.
  192. """
  193. # When this function is called, the test database has been created
  194. # already and its name has been copied to settings_dict['NAME'] so
  195. # we don't need to call _get_test_db_name.
  196. orig_settings_dict = self.connection.settings_dict
  197. new_settings_dict = orig_settings_dict.copy()
  198. new_settings_dict['NAME'] = '{}_{}'.format(orig_settings_dict['NAME'], number)
  199. return new_settings_dict
  200. def _clone_test_db(self, number, verbosity, keepdb=False):
  201. """
  202. Internal implementation - duplicate the test db tables.
  203. """
  204. raise NotImplementedError(
  205. "The database backend doesn't support cloning databases. "
  206. "Disable the option to run tests in parallel processes.")
  207. def destroy_test_db(self, old_database_name=None, verbosity=1, keepdb=False, number=None):
  208. """
  209. Destroy a test database, prompting the user for confirmation if the
  210. database already exists.
  211. """
  212. self.connection.close()
  213. if number is None:
  214. test_database_name = self.connection.settings_dict['NAME']
  215. else:
  216. test_database_name = self.get_test_db_clone_settings(number)['NAME']
  217. if verbosity >= 1:
  218. action = 'Destroying'
  219. if keepdb:
  220. action = 'Preserving'
  221. print("%s test database for alias %s..." % (
  222. action,
  223. self._get_database_display_str(verbosity, test_database_name),
  224. ))
  225. # if we want to preserve the database
  226. # skip the actual destroying piece.
  227. if not keepdb:
  228. self._destroy_test_db(test_database_name, verbosity)
  229. # Restore the original database name
  230. if old_database_name is not None:
  231. settings.DATABASES[self.connection.alias]["NAME"] = old_database_name
  232. self.connection.settings_dict["NAME"] = old_database_name
  233. def _destroy_test_db(self, test_database_name, verbosity):
  234. """
  235. Internal implementation - remove the test db tables.
  236. """
  237. # Remove the test database to clean up after
  238. # ourselves. Connect to the previous database (not the test database)
  239. # to do so, because it's not allowed to delete a database while being
  240. # connected to it.
  241. with self.connection._nodb_connection.cursor() as cursor:
  242. # Wait to avoid "database is being accessed by other users" errors.
  243. time.sleep(1)
  244. cursor.execute("DROP DATABASE %s"
  245. % self.connection.ops.quote_name(test_database_name))
  246. def sql_table_creation_suffix(self):
  247. """
  248. SQL to append to the end of the test table creation statements.
  249. """
  250. return ''
  251. def test_db_signature(self):
  252. """
  253. Returns a tuple with elements of self.connection.settings_dict (a
  254. DATABASES setting value) that uniquely identify a database
  255. accordingly to the RDBMS particularities.
  256. """
  257. settings_dict = self.connection.settings_dict
  258. return (
  259. settings_dict['HOST'],
  260. settings_dict['PORT'],
  261. settings_dict['ENGINE'],
  262. settings_dict['NAME']
  263. )