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.

utils.py 9.6KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. import pkgutil
  2. from importlib import import_module
  3. from pathlib import Path
  4. from threading import local
  5. from django.conf import settings
  6. from django.core.exceptions import ImproperlyConfigured
  7. from django.utils.functional import cached_property
  8. from django.utils.module_loading import import_string
  9. DEFAULT_DB_ALIAS = 'default'
  10. DJANGO_VERSION_PICKLE_KEY = '_django_version'
  11. class Error(Exception):
  12. pass
  13. class InterfaceError(Error):
  14. pass
  15. class DatabaseError(Error):
  16. pass
  17. class DataError(DatabaseError):
  18. pass
  19. class OperationalError(DatabaseError):
  20. pass
  21. class IntegrityError(DatabaseError):
  22. pass
  23. class InternalError(DatabaseError):
  24. pass
  25. class ProgrammingError(DatabaseError):
  26. pass
  27. class NotSupportedError(DatabaseError):
  28. pass
  29. class DatabaseErrorWrapper:
  30. """
  31. Context manager and decorator that reraises backend-specific database
  32. exceptions using Django's common wrappers.
  33. """
  34. def __init__(self, wrapper):
  35. """
  36. wrapper is a database wrapper.
  37. It must have a Database attribute defining PEP-249 exceptions.
  38. """
  39. self.wrapper = wrapper
  40. def __enter__(self):
  41. pass
  42. def __exit__(self, exc_type, exc_value, traceback):
  43. if exc_type is None:
  44. return
  45. for dj_exc_type in (
  46. DataError,
  47. OperationalError,
  48. IntegrityError,
  49. InternalError,
  50. ProgrammingError,
  51. NotSupportedError,
  52. DatabaseError,
  53. InterfaceError,
  54. Error,
  55. ):
  56. db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
  57. if issubclass(exc_type, db_exc_type):
  58. dj_exc_value = dj_exc_type(*exc_value.args)
  59. # Only set the 'errors_occurred' flag for errors that may make
  60. # the connection unusable.
  61. if dj_exc_type not in (DataError, IntegrityError):
  62. self.wrapper.errors_occurred = True
  63. raise dj_exc_value.with_traceback(traceback) from exc_value
  64. def __call__(self, func):
  65. # Note that we are intentionally not using @wraps here for performance
  66. # reasons. Refs #21109.
  67. def inner(*args, **kwargs):
  68. with self:
  69. return func(*args, **kwargs)
  70. return inner
  71. def load_backend(backend_name):
  72. """
  73. Return a database backend's "base" module given a fully qualified database
  74. backend name, or raise an error if it doesn't exist.
  75. """
  76. # This backend was renamed in Django 1.9.
  77. if backend_name == 'django.db.backends.postgresql_psycopg2':
  78. backend_name = 'django.db.backends.postgresql'
  79. try:
  80. return import_module('%s.base' % backend_name)
  81. except ImportError as e_user:
  82. # The database backend wasn't found. Display a helpful error message
  83. # listing all built-in database backends.
  84. backend_dir = str(Path(__file__).parent / 'backends')
  85. builtin_backends = [
  86. name for _, name, ispkg in pkgutil.iter_modules([backend_dir])
  87. if ispkg and name not in {'base', 'dummy', 'postgresql_psycopg2'}
  88. ]
  89. if backend_name not in ['django.db.backends.%s' % b for b in builtin_backends]:
  90. backend_reprs = map(repr, sorted(builtin_backends))
  91. raise ImproperlyConfigured(
  92. "%r isn't an available database backend.\n"
  93. "Try using 'django.db.backends.XXX', where XXX is one of:\n"
  94. " %s" % (backend_name, ", ".join(backend_reprs))
  95. ) from e_user
  96. else:
  97. # If there's some other error, this must be an error in Django
  98. raise
  99. class ConnectionDoesNotExist(Exception):
  100. pass
  101. class ConnectionHandler:
  102. def __init__(self, databases=None):
  103. """
  104. databases is an optional dictionary of database definitions (structured
  105. like settings.DATABASES).
  106. """
  107. self._databases = databases
  108. self._connections = local()
  109. @cached_property
  110. def databases(self):
  111. if self._databases is None:
  112. self._databases = settings.DATABASES
  113. if self._databases == {}:
  114. self._databases = {
  115. DEFAULT_DB_ALIAS: {
  116. 'ENGINE': 'django.db.backends.dummy',
  117. },
  118. }
  119. if DEFAULT_DB_ALIAS not in self._databases:
  120. raise ImproperlyConfigured("You must define a '%s' database." % DEFAULT_DB_ALIAS)
  121. if self._databases[DEFAULT_DB_ALIAS] == {}:
  122. self._databases[DEFAULT_DB_ALIAS]['ENGINE'] = 'django.db.backends.dummy'
  123. return self._databases
  124. def ensure_defaults(self, alias):
  125. """
  126. Put the defaults into the settings dictionary for a given connection
  127. where no settings is provided.
  128. """
  129. try:
  130. conn = self.databases[alias]
  131. except KeyError:
  132. raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
  133. conn.setdefault('ATOMIC_REQUESTS', False)
  134. conn.setdefault('AUTOCOMMIT', True)
  135. conn.setdefault('ENGINE', 'django.db.backends.dummy')
  136. if conn['ENGINE'] == 'django.db.backends.' or not conn['ENGINE']:
  137. conn['ENGINE'] = 'django.db.backends.dummy'
  138. conn.setdefault('CONN_MAX_AGE', 0)
  139. conn.setdefault('OPTIONS', {})
  140. conn.setdefault('TIME_ZONE', None)
  141. for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']:
  142. conn.setdefault(setting, '')
  143. def prepare_test_settings(self, alias):
  144. """
  145. Make sure the test settings are available in the 'TEST' sub-dictionary.
  146. """
  147. try:
  148. conn = self.databases[alias]
  149. except KeyError:
  150. raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
  151. test_settings = conn.setdefault('TEST', {})
  152. for key in ['CHARSET', 'COLLATION', 'NAME', 'MIRROR']:
  153. test_settings.setdefault(key, None)
  154. def __getitem__(self, alias):
  155. if hasattr(self._connections, alias):
  156. return getattr(self._connections, alias)
  157. self.ensure_defaults(alias)
  158. self.prepare_test_settings(alias)
  159. db = self.databases[alias]
  160. backend = load_backend(db['ENGINE'])
  161. conn = backend.DatabaseWrapper(db, alias)
  162. setattr(self._connections, alias, conn)
  163. return conn
  164. def __setitem__(self, key, value):
  165. setattr(self._connections, key, value)
  166. def __delitem__(self, key):
  167. delattr(self._connections, key)
  168. def __iter__(self):
  169. return iter(self.databases)
  170. def all(self):
  171. return [self[alias] for alias in self]
  172. def close_all(self):
  173. for alias in self:
  174. try:
  175. connection = getattr(self._connections, alias)
  176. except AttributeError:
  177. continue
  178. connection.close()
  179. class ConnectionRouter:
  180. def __init__(self, routers=None):
  181. """
  182. If routers is not specified, default to settings.DATABASE_ROUTERS.
  183. """
  184. self._routers = routers
  185. @cached_property
  186. def routers(self):
  187. if self._routers is None:
  188. self._routers = settings.DATABASE_ROUTERS
  189. routers = []
  190. for r in self._routers:
  191. if isinstance(r, str):
  192. router = import_string(r)()
  193. else:
  194. router = r
  195. routers.append(router)
  196. return routers
  197. def _router_func(action):
  198. def _route_db(self, model, **hints):
  199. chosen_db = None
  200. for router in self.routers:
  201. try:
  202. method = getattr(router, action)
  203. except AttributeError:
  204. # If the router doesn't have a method, skip to the next one.
  205. pass
  206. else:
  207. chosen_db = method(model, **hints)
  208. if chosen_db:
  209. return chosen_db
  210. instance = hints.get('instance')
  211. if instance is not None and instance._state.db:
  212. return instance._state.db
  213. return DEFAULT_DB_ALIAS
  214. return _route_db
  215. db_for_read = _router_func('db_for_read')
  216. db_for_write = _router_func('db_for_write')
  217. def allow_relation(self, obj1, obj2, **hints):
  218. for router in self.routers:
  219. try:
  220. method = router.allow_relation
  221. except AttributeError:
  222. # If the router doesn't have a method, skip to the next one.
  223. pass
  224. else:
  225. allow = method(obj1, obj2, **hints)
  226. if allow is not None:
  227. return allow
  228. return obj1._state.db == obj2._state.db
  229. def allow_migrate(self, db, app_label, **hints):
  230. for router in self.routers:
  231. try:
  232. method = router.allow_migrate
  233. except AttributeError:
  234. # If the router doesn't have a method, skip to the next one.
  235. continue
  236. allow = method(db, app_label, **hints)
  237. if allow is not None:
  238. return allow
  239. return True
  240. def allow_migrate_model(self, db, model):
  241. return self.allow_migrate(
  242. db,
  243. model._meta.app_label,
  244. model_name=model._meta.model_name,
  245. model=model,
  246. )
  247. def get_migratable_models(self, app_config, db, include_auto_created=False):
  248. """Return app models allowed to be migrated on provided db."""
  249. models = app_config.get_models(include_auto_created=include_auto_created)
  250. return [model for model in models if self.allow_migrate_model(db, model)]