2012-10-08 03:16:01 +08:00
|
|
|
import pkgutil
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2019-04-12 21:15:18 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2017-10-22 23:30:42 +08:00
|
|
|
# For backwards compatibility with Django < 3.2
|
|
|
|
from django.utils.connection import ConnectionDoesNotExist # NOQA: F401
|
|
|
|
from django.utils.connection import BaseConnectionHandler
|
2013-05-21 17:35:05 +08:00
|
|
|
from django.utils.functional import cached_property
|
2014-01-21 04:15:14 +08:00
|
|
|
from django.utils.module_loading import import_string
|
2010-12-09 02:31:57 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
DEFAULT_DB_ALIAS = 'default'
|
2014-06-06 19:10:20 +08:00
|
|
|
DJANGO_VERSION_PICKLE_KEY = '_django_version'
|
2010-01-22 22:30:06 +08:00
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
2016-12-01 18:38:01 +08:00
|
|
|
class Error(Exception):
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class InterfaceError(Error):
|
2010-01-29 23:45:55 +08:00
|
|
|
pass
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
|
|
|
class DatabaseError(Error):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class DataError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OperationalError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
class IntegrityError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
class InternalError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class ProgrammingError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class NotSupportedError(DatabaseError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DatabaseErrorWrapper:
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Context manager and decorator that reraises backend-specific database
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
exceptions using Django's common wrappers.
|
|
|
|
"""
|
|
|
|
|
2013-02-18 18:37:26 +08:00
|
|
|
def __init__(self, wrapper):
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
"""
|
2013-02-18 18:37:26 +08:00
|
|
|
wrapper is a database wrapper.
|
|
|
|
|
|
|
|
It must have a Database attribute defining PEP-249 exceptions.
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
"""
|
2013-02-18 18:37:26 +08:00
|
|
|
self.wrapper = wrapper
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
if exc_type is None:
|
|
|
|
return
|
|
|
|
for dj_exc_type in (
|
|
|
|
DataError,
|
|
|
|
OperationalError,
|
|
|
|
IntegrityError,
|
|
|
|
InternalError,
|
|
|
|
ProgrammingError,
|
|
|
|
NotSupportedError,
|
|
|
|
DatabaseError,
|
|
|
|
InterfaceError,
|
|
|
|
Error,
|
2013-10-18 17:02:43 +08:00
|
|
|
):
|
2013-02-18 18:37:26 +08:00
|
|
|
db_exc_type = getattr(self.wrapper.Database, dj_exc_type.__name__)
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
if issubclass(exc_type, db_exc_type):
|
2013-07-01 18:02:17 +08:00
|
|
|
dj_exc_value = dj_exc_type(*exc_value.args)
|
2013-02-18 18:37:26 +08:00
|
|
|
# Only set the 'errors_occurred' flag for errors that may make
|
|
|
|
# the connection unusable.
|
|
|
|
if dj_exc_type not in (DataError, IntegrityError):
|
|
|
|
self.wrapper.errors_occurred = True
|
2017-04-25 14:29:54 +08:00
|
|
|
raise dj_exc_value.with_traceback(traceback) from exc_value
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
|
|
|
|
def __call__(self, func):
|
2013-09-15 15:12:16 +08:00
|
|
|
# Note that we are intentionally not using @wraps here for performance
|
|
|
|
# reasons. Refs #21109.
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
def inner(*args, **kwargs):
|
|
|
|
with self:
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
return inner
|
|
|
|
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def load_backend(backend_name):
|
2015-08-05 22:08:56 +08:00
|
|
|
"""
|
|
|
|
Return a database backend's "base" module given a fully qualified database
|
|
|
|
backend name, or raise an error if it doesn't exist.
|
|
|
|
"""
|
|
|
|
# This backend was renamed in Django 1.9.
|
|
|
|
if backend_name == 'django.db.backends.postgresql_psycopg2':
|
|
|
|
backend_name = 'django.db.backends.postgresql'
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
2013-07-29 21:50:58 +08:00
|
|
|
return import_module('%s.base' % backend_name)
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e_user:
|
2011-03-31 01:59:44 +08:00
|
|
|
# The database backend wasn't found. Display a helpful error message
|
2017-01-24 21:33:26 +08:00
|
|
|
# listing all built-in database backends.
|
2021-01-04 14:16:13 +08:00
|
|
|
import django.db.backends
|
2017-01-24 21:33:26 +08:00
|
|
|
builtin_backends = [
|
2021-01-04 14:16:13 +08:00
|
|
|
name for _, name, ispkg in pkgutil.iter_modules(django.db.backends.__path__)
|
2021-01-07 03:15:14 +08:00
|
|
|
if ispkg and name not in {'base', 'dummy'}
|
2017-01-24 21:33:26 +08:00
|
|
|
]
|
|
|
|
if backend_name not in ['django.db.backends.%s' % b for b in builtin_backends]:
|
2012-10-07 06:56:28 +08:00
|
|
|
backend_reprs = map(repr, sorted(builtin_backends))
|
2017-01-24 21:33:26 +08:00
|
|
|
raise ImproperlyConfigured(
|
2020-11-16 04:34:35 +08:00
|
|
|
"%r isn't an available database backend or couldn't be "
|
|
|
|
"imported. Check the above exception. To use one of the "
|
|
|
|
"built-in backends, use 'django.db.backends.XXX', where XXX "
|
|
|
|
"is one of:\n"
|
2017-01-24 21:33:26 +08:00
|
|
|
" %s" % (backend_name, ", ".join(backend_reprs))
|
|
|
|
) from e_user
|
2011-03-31 01:59:44 +08:00
|
|
|
else:
|
2011-12-25 21:24:39 +08:00
|
|
|
# If there's some other error, this must be an error in Django
|
|
|
|
raise
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2017-10-22 23:30:42 +08:00
|
|
|
class ConnectionHandler(BaseConnectionHandler):
|
|
|
|
settings_name = 'DATABASES'
|
|
|
|
# Connections needs to still be an actual thread local, as it's truly
|
|
|
|
# thread-critical. Database backends should use @async_unsafe to protect
|
|
|
|
# their code from async contexts, but this will give those contexts
|
|
|
|
# separate connections in case it's needed as well. There's no cleanup
|
|
|
|
# after async contexts, though, so we don't allow that if we can help it.
|
|
|
|
thread_critical = True
|
|
|
|
|
|
|
|
def configure_settings(self, databases):
|
|
|
|
databases = super().configure_settings(databases)
|
|
|
|
if databases == {}:
|
|
|
|
databases[DEFAULT_DB_ALIAS] = {'ENGINE': 'django.db.backends.dummy'}
|
|
|
|
elif DEFAULT_DB_ALIAS not in databases:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
f"You must define a '{DEFAULT_DB_ALIAS}' database."
|
|
|
|
)
|
|
|
|
elif databases[DEFAULT_DB_ALIAS] == {}:
|
|
|
|
databases[DEFAULT_DB_ALIAS]['ENGINE'] = 'django.db.backends.dummy'
|
|
|
|
return databases
|
2013-05-21 17:35:05 +08:00
|
|
|
|
2017-10-22 23:30:42 +08:00
|
|
|
@property
|
2013-05-21 17:35:05 +08:00
|
|
|
def databases(self):
|
2017-10-22 23:30:42 +08:00
|
|
|
return self.settings
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def ensure_defaults(self, alias):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Put the defaults into the settings dictionary for a given connection
|
2009-12-22 23:18:51 +08:00
|
|
|
where no settings is provided.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
conn = self.databases[alias]
|
|
|
|
except KeyError:
|
2017-10-22 23:30:42 +08:00
|
|
|
raise self.exception_class(f"The connection '{alias}' doesn't exist.")
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
conn.setdefault('ATOMIC_REQUESTS', False)
|
|
|
|
conn.setdefault('AUTOCOMMIT', True)
|
2009-12-24 13:57:43 +08:00
|
|
|
conn.setdefault('ENGINE', 'django.db.backends.dummy')
|
2010-01-11 08:54:27 +08:00
|
|
|
if conn['ENGINE'] == 'django.db.backends.' or not conn['ENGINE']:
|
2009-12-24 13:57:43 +08:00
|
|
|
conn['ENGINE'] = 'django.db.backends.dummy'
|
2013-05-09 21:42:14 +08:00
|
|
|
conn.setdefault('CONN_MAX_AGE', 0)
|
2009-12-22 23:18:51 +08:00
|
|
|
conn.setdefault('OPTIONS', {})
|
2015-05-03 03:56:53 +08:00
|
|
|
conn.setdefault('TIME_ZONE', None)
|
2011-03-31 01:59:44 +08:00
|
|
|
for setting in ['NAME', 'USER', 'PASSWORD', 'HOST', 'PORT']:
|
2009-12-22 23:18:51 +08:00
|
|
|
conn.setdefault(setting, '')
|
2014-01-20 08:45:29 +08:00
|
|
|
|
|
|
|
def prepare_test_settings(self, alias):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Make sure the test settings are available in the 'TEST' sub-dictionary.
|
2014-01-20 08:45:29 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
conn = self.databases[alias]
|
|
|
|
except KeyError:
|
2017-10-22 23:30:42 +08:00
|
|
|
raise self.exception_class(f"The connection '{alias}' doesn't exist.")
|
2014-01-20 08:45:29 +08:00
|
|
|
|
|
|
|
test_settings = conn.setdefault('TEST', {})
|
2019-11-13 11:49:09 +08:00
|
|
|
default_test_settings = [
|
|
|
|
('CHARSET', None),
|
|
|
|
('COLLATION', None),
|
|
|
|
('MIGRATE', True),
|
|
|
|
('MIRROR', None),
|
|
|
|
('NAME', None),
|
|
|
|
]
|
|
|
|
for key, value in default_test_settings:
|
|
|
|
test_settings.setdefault(key, value)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2017-10-22 23:30:42 +08:00
|
|
|
def create_connection(self, alias):
|
2009-12-22 23:18:51 +08:00
|
|
|
self.ensure_defaults(alias)
|
2014-01-20 08:45:29 +08:00
|
|
|
self.prepare_test_settings(alias)
|
2009-12-22 23:18:51 +08:00
|
|
|
db = self.databases[alias]
|
|
|
|
backend = load_backend(db['ENGINE'])
|
2017-10-22 23:30:42 +08:00
|
|
|
return backend.DatabaseWrapper(db, alias)
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2014-12-29 01:10:12 +08:00
|
|
|
def close_all(self):
|
|
|
|
for alias in self:
|
|
|
|
try:
|
|
|
|
connection = getattr(self._connections, alias)
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
connection.close()
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class ConnectionRouter:
|
2013-05-21 18:21:31 +08:00
|
|
|
def __init__(self, routers=None):
|
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
If routers is not specified, default to settings.DATABASE_ROUTERS.
|
2013-05-21 18:21:31 +08:00
|
|
|
"""
|
|
|
|
self._routers = routers
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def routers(self):
|
|
|
|
if self._routers is None:
|
|
|
|
self._routers = settings.DATABASE_ROUTERS
|
2013-05-25 02:45:03 +08:00
|
|
|
routers = []
|
2013-05-21 18:21:31 +08:00
|
|
|
for r in self._routers:
|
2016-12-29 23:27:49 +08:00
|
|
|
if isinstance(r, str):
|
2014-01-21 04:15:14 +08:00
|
|
|
router = import_string(r)()
|
2010-01-22 22:30:06 +08:00
|
|
|
else:
|
|
|
|
router = r
|
2013-05-25 02:45:03 +08:00
|
|
|
routers.append(router)
|
|
|
|
return routers
|
2010-01-22 22:30:06 +08:00
|
|
|
|
|
|
|
def _router_func(action):
|
|
|
|
def _route_db(self, model, **hints):
|
|
|
|
chosen_db = None
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = getattr(router, action)
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
2010-12-09 02:37:00 +08:00
|
|
|
chosen_db = method(model, **hints)
|
2010-12-09 02:31:57 +08:00
|
|
|
if chosen_db:
|
|
|
|
return chosen_db
|
2014-06-30 21:06:28 +08:00
|
|
|
instance = hints.get('instance')
|
|
|
|
if instance is not None and instance._state.db:
|
|
|
|
return instance._state.db
|
|
|
|
return DEFAULT_DB_ALIAS
|
2010-01-22 22:30:06 +08:00
|
|
|
return _route_db
|
|
|
|
|
|
|
|
db_for_read = _router_func('db_for_read')
|
|
|
|
db_for_write = _router_func('db_for_write')
|
|
|
|
|
|
|
|
def allow_relation(self, obj1, obj2, **hints):
|
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2010-12-09 02:31:57 +08:00
|
|
|
method = router.allow_relation
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
|
|
|
pass
|
2010-12-09 02:31:57 +08:00
|
|
|
else:
|
|
|
|
allow = method(obj1, obj2, **hints)
|
|
|
|
if allow is not None:
|
|
|
|
return allow
|
2010-01-22 22:30:06 +08:00
|
|
|
return obj1._state.db == obj2._state.db
|
2010-01-25 20:23:30 +08:00
|
|
|
|
2015-02-19 15:27:58 +08:00
|
|
|
def allow_migrate(self, db, app_label, **hints):
|
2010-01-25 20:23:30 +08:00
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2014-11-19 00:27:38 +08:00
|
|
|
method = router.allow_migrate
|
2010-01-27 15:56:53 +08:00
|
|
|
except AttributeError:
|
|
|
|
# If the router doesn't have a method, skip to the next one.
|
2015-02-19 15:27:58 +08:00
|
|
|
continue
|
|
|
|
|
2015-08-12 21:10:39 +08:00
|
|
|
allow = method(db, app_label, **hints)
|
2015-02-19 15:27:58 +08:00
|
|
|
|
|
|
|
if allow is not None:
|
|
|
|
return allow
|
2010-01-25 20:23:30 +08:00
|
|
|
return True
|
2013-10-16 23:58:21 +08:00
|
|
|
|
2015-02-19 15:27:58 +08:00
|
|
|
def allow_migrate_model(self, db, model):
|
|
|
|
return self.allow_migrate(
|
|
|
|
db,
|
|
|
|
model._meta.app_label,
|
|
|
|
model_name=model._meta.model_name,
|
|
|
|
model=model,
|
|
|
|
)
|
|
|
|
|
2013-12-30 04:21:23 +08:00
|
|
|
def get_migratable_models(self, app_config, db, include_auto_created=False):
|
2017-01-25 07:04:12 +08:00
|
|
|
"""Return app models allowed to be migrated on provided db."""
|
2013-12-30 04:21:23 +08:00
|
|
|
models = app_config.get_models(include_auto_created=include_auto_created)
|
2015-02-19 15:27:58 +08:00
|
|
|
return [model for model in models if self.allow_migrate_model(db, model)]
|