/lib/python/django/db/__init__.py

https://github.com/mozilla/moztrap-vendor-lib · Python · 96 lines · 64 code · 6 blank · 26 comment · 0 complexity · 01c73abe954fba037cdd254d363fc886 MD5 · raw file

  1. import warnings
  2. from django.core import signals
  3. from django.db.utils import (DEFAULT_DB_ALIAS,
  4. DataError, OperationalError, IntegrityError, InternalError,
  5. ProgrammingError, NotSupportedError, DatabaseError,
  6. InterfaceError, Error,
  7. load_backend, ConnectionHandler, ConnectionRouter)
  8. from django.utils.functional import cached_property
  9. __all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
  10. 'IntegrityError', 'DEFAULT_DB_ALIAS')
  11. connections = ConnectionHandler()
  12. router = ConnectionRouter()
  13. # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
  14. # for backend bits.
  15. # DatabaseWrapper.__init__() takes a dictionary, not a settings module, so
  16. # we manually create the dictionary from the settings, passing only the
  17. # settings that the database backends care about. Note that TIME_ZONE is used
  18. # by the PostgreSQL backends.
  19. # We load all these up for backwards compatibility, you should use
  20. # connections['default'] instead.
  21. class DefaultConnectionProxy(object):
  22. """
  23. Proxy for accessing the default DatabaseWrapper object's attributes. If you
  24. need to access the DatabaseWrapper object itself, use
  25. connections[DEFAULT_DB_ALIAS] instead.
  26. """
  27. def __getattr__(self, item):
  28. return getattr(connections[DEFAULT_DB_ALIAS], item)
  29. def __setattr__(self, name, value):
  30. return setattr(connections[DEFAULT_DB_ALIAS], name, value)
  31. def __delattr__(self, name):
  32. return delattr(connections[DEFAULT_DB_ALIAS], name)
  33. connection = DefaultConnectionProxy()
  34. class DefaultBackendProxy(object):
  35. """
  36. Temporary proxy class used during deprecation period of the `backend` module
  37. variable.
  38. """
  39. @cached_property
  40. def _backend(self):
  41. warnings.warn("Accessing django.db.backend is deprecated.",
  42. PendingDeprecationWarning, stacklevel=2)
  43. return load_backend(connections[DEFAULT_DB_ALIAS].settings_dict['ENGINE'])
  44. def __getattr__(self, item):
  45. return getattr(self._backend, item)
  46. def __setattr__(self, name, value):
  47. return setattr(self._backend, name, value)
  48. def __delattr__(self, name):
  49. return delattr(self._backend, name)
  50. backend = DefaultBackendProxy()
  51. def close_connection(**kwargs):
  52. warnings.warn(
  53. "close_connection is superseded by close_old_connections.",
  54. PendingDeprecationWarning, stacklevel=2)
  55. # Avoid circular imports
  56. from django.db import transaction
  57. for conn in connections:
  58. # If an error happens here the connection will be left in broken
  59. # state. Once a good db connection is again available, the
  60. # connection state will be cleaned up.
  61. transaction.abort(conn)
  62. connections[conn].close()
  63. # Register an event to reset saved queries when a Django request is started.
  64. def reset_queries(**kwargs):
  65. for conn in connections.all():
  66. conn.queries = []
  67. signals.request_started.connect(reset_queries)
  68. # Register an event to reset transaction state and close connections past
  69. # their lifetime. NB: abort() doesn't do anything outside of a transaction.
  70. def close_old_connections(**kwargs):
  71. for conn in connections.all():
  72. # Remove this when the legacy transaction management goes away.
  73. try:
  74. conn.abort()
  75. except DatabaseError:
  76. pass
  77. conn.close_if_unusable_or_obsolete()
  78. signals.request_started.connect(close_old_connections)
  79. signals.request_finished.connect(close_old_connections)