2013-07-29 21:50:58 +08:00
|
|
|
from importlib import import_module
|
2009-12-22 23:18:51 +08:00
|
|
|
import os
|
2012-10-08 03:16:01 +08:00
|
|
|
import pkgutil
|
2011-12-16 21:40:19 +08:00
|
|
|
from threading import local
|
2013-03-06 18:12:24 +08:00
|
|
|
import warnings
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango18Warning, RemovedInDjango19Warning
|
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
|
2012-12-08 18:13:52 +08:00
|
|
|
from django.utils._os import upath
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2009-12-22 23:18:51 +08:00
|
|
|
|
2010-12-09 02:31:57 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
DEFAULT_DB_ALIAS = 'default'
|
|
|
|
|
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-28 02:24:23 +08:00
|
|
|
class Error(Exception if six.PY3 else StandardError):
|
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
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseErrorWrapper(object):
|
|
|
|
"""
|
|
|
|
Context manager and decorator that re-throws backend-specific database
|
|
|
|
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-06-04 19:31:06 +08:00
|
|
|
dj_exc_value.__cause__ = exc_value
|
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
|
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
|
|
|
six.reraise(dj_exc_type, dj_exc_value, traceback)
|
|
|
|
|
|
|
|
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):
|
2011-03-31 01:59:44 +08:00
|
|
|
# Look for a fully qualified database backend name
|
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
|
|
|
|
# listing all possible (built-in) database backends.
|
2012-12-08 18:13:52 +08:00
|
|
|
backend_dir = os.path.join(os.path.dirname(upath(__file__)), 'backends')
|
2009-12-22 23:18:51 +08:00
|
|
|
try:
|
2012-10-08 03:16:01 +08:00
|
|
|
builtin_backends = [
|
|
|
|
name for _, name, ispkg in pkgutil.iter_modules([backend_dir])
|
|
|
|
if ispkg and name != 'dummy']
|
2011-03-31 01:59:44 +08:00
|
|
|
except EnvironmentError:
|
2012-10-07 06:56:28 +08:00
|
|
|
builtin_backends = []
|
|
|
|
if backend_name not in ['django.db.backends.%s' % b for b in
|
|
|
|
builtin_backends]:
|
|
|
|
backend_reprs = map(repr, sorted(builtin_backends))
|
2011-12-25 21:24:39 +08:00
|
|
|
error_msg = ("%r isn't an available database backend.\n"
|
2012-10-07 06:56:28 +08:00
|
|
|
"Try using 'django.db.backends.XXX', where XXX "
|
2011-12-25 21:24:39 +08:00
|
|
|
"is one of:\n %s\nError was: %s" %
|
|
|
|
(backend_name, ", ".join(backend_reprs), e_user))
|
|
|
|
raise ImproperlyConfigured(error_msg)
|
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
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class ConnectionDoesNotExist(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
class ConnectionHandler(object):
|
2013-05-21 17:35:05 +08:00
|
|
|
def __init__(self, databases=None):
|
|
|
|
"""
|
|
|
|
databases is an optional dictionary of database definitions (structured
|
|
|
|
like settings.DATABASES).
|
|
|
|
"""
|
|
|
|
self._databases = databases
|
|
|
|
self._connections = local()
|
|
|
|
|
|
|
|
@cached_property
|
|
|
|
def databases(self):
|
|
|
|
if self._databases is None:
|
|
|
|
self._databases = settings.DATABASES
|
|
|
|
if self._databases == {}:
|
|
|
|
self._databases = {
|
2012-10-29 06:02:41 +08:00
|
|
|
DEFAULT_DB_ALIAS: {
|
|
|
|
'ENGINE': 'django.db.backends.dummy',
|
|
|
|
},
|
|
|
|
}
|
2013-05-21 17:35:05 +08:00
|
|
|
if DEFAULT_DB_ALIAS not in self._databases:
|
|
|
|
raise ImproperlyConfigured("You must define a '%s' database" % DEFAULT_DB_ALIAS)
|
|
|
|
return self._databases
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def ensure_defaults(self, alias):
|
|
|
|
"""
|
|
|
|
Puts the defaults into the settings dictionary for a given connection
|
|
|
|
where no settings is provided.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
conn = self.databases[alias]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2013-03-06 18:12:24 +08:00
|
|
|
conn.setdefault('ATOMIC_REQUESTS', False)
|
|
|
|
if settings.TRANSACTIONS_MANAGED:
|
|
|
|
warnings.warn(
|
|
|
|
"TRANSACTIONS_MANAGED is deprecated. Use AUTOCOMMIT instead.",
|
2014-02-27 05:48:20 +08:00
|
|
|
RemovedInDjango18Warning, stacklevel=2)
|
2013-03-06 18:12:24 +08:00
|
|
|
conn.setdefault('AUTOCOMMIT', 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', {})
|
2011-11-18 21:01:06 +08:00
|
|
|
conn.setdefault('TIME_ZONE', 'UTC' if settings.USE_TZ else settings.TIME_ZONE)
|
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
|
|
|
|
|
|
|
TEST_SETTING_RENAMES = {
|
|
|
|
'CREATE': 'CREATE_DB',
|
|
|
|
'USER_CREATE': 'CREATE_USER',
|
|
|
|
'PASSWD': 'PASSWORD',
|
|
|
|
}
|
2014-06-12 03:26:17 +08:00
|
|
|
TEST_SETTING_RENAMES_REVERSE = {v: k for k, v in TEST_SETTING_RENAMES.items()}
|
2014-01-20 08:45:29 +08:00
|
|
|
|
|
|
|
def prepare_test_settings(self, alias):
|
|
|
|
"""
|
|
|
|
Makes sure the test settings are available in the 'TEST' sub-dictionary.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
conn = self.databases[alias]
|
|
|
|
except KeyError:
|
|
|
|
raise ConnectionDoesNotExist("The connection %s doesn't exist" % alias)
|
|
|
|
|
2014-06-12 03:26:17 +08:00
|
|
|
test_dict_set = 'TEST' in conn
|
2014-01-20 08:45:29 +08:00
|
|
|
test_settings = conn.setdefault('TEST', {})
|
2014-06-12 03:26:17 +08:00
|
|
|
old_test_settings = {}
|
2014-01-20 08:45:29 +08:00
|
|
|
for key, value in six.iteritems(conn):
|
|
|
|
if key.startswith('TEST_'):
|
|
|
|
new_key = key[5:]
|
|
|
|
new_key = self.TEST_SETTING_RENAMES.get(new_key, new_key)
|
2014-06-12 03:26:17 +08:00
|
|
|
old_test_settings[new_key] = value
|
|
|
|
|
|
|
|
if old_test_settings:
|
|
|
|
if test_dict_set:
|
|
|
|
if test_settings != old_test_settings:
|
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"Connection '%s' has mismatched TEST and TEST_* "
|
|
|
|
"database settings." % alias)
|
|
|
|
else:
|
|
|
|
test_settings.update(old_test_settings)
|
|
|
|
for key, _ in six.iteritems(old_test_settings):
|
|
|
|
warnings.warn("In Django 1.9 the %s connection setting will be moved "
|
|
|
|
"to a %s entry in the TEST setting" %
|
|
|
|
(self.TEST_SETTING_RENAMES_REVERSE.get(key, key), key),
|
|
|
|
RemovedInDjango19Warning, stacklevel=2)
|
|
|
|
|
2014-03-09 18:24:23 +08:00
|
|
|
for key in list(conn.keys()):
|
2014-03-09 16:23:47 +08:00
|
|
|
if key.startswith('TEST_'):
|
|
|
|
del conn[key]
|
2014-01-20 08:45:29 +08:00
|
|
|
# Check that they didn't just use the old name with 'TEST_' removed
|
|
|
|
for key, new_key in six.iteritems(self.TEST_SETTING_RENAMES):
|
|
|
|
if key in test_settings:
|
|
|
|
warnings.warn("Test setting %s was renamed to %s; specified value (%s) ignored" %
|
|
|
|
(key, new_key, test_settings[key]), stacklevel=2)
|
|
|
|
for key in ['CHARSET', 'COLLATION', 'NAME', 'MIRROR']:
|
|
|
|
test_settings.setdefault(key, None)
|
2009-12-22 23:18:51 +08:00
|
|
|
|
|
|
|
def __getitem__(self, alias):
|
2011-12-16 21:40:19 +08:00
|
|
|
if hasattr(self._connections, alias):
|
|
|
|
return getattr(self._connections, 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'])
|
|
|
|
conn = backend.DatabaseWrapper(db, alias)
|
2011-12-16 21:40:19 +08:00
|
|
|
setattr(self._connections, alias, conn)
|
2009-12-22 23:18:51 +08:00
|
|
|
return conn
|
|
|
|
|
2011-12-16 21:40:19 +08:00
|
|
|
def __setitem__(self, key, value):
|
|
|
|
setattr(self._connections, key, value)
|
|
|
|
|
2013-03-01 00:05:25 +08:00
|
|
|
def __delitem__(self, key):
|
|
|
|
delattr(self._connections, key)
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.databases)
|
|
|
|
|
|
|
|
def all(self):
|
|
|
|
return [self[alias] for alias in self]
|
2010-01-22 22:30:06 +08:00
|
|
|
|
2010-01-29 23:45:55 +08:00
|
|
|
|
2010-01-22 22:30:06 +08:00
|
|
|
class ConnectionRouter(object):
|
2013-05-21 18:21:31 +08:00
|
|
|
def __init__(self, routers=None):
|
|
|
|
"""
|
|
|
|
If routers is not specified, will default to settings.DATABASE_ROUTERS.
|
|
|
|
"""
|
|
|
|
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:
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(r, six.string_types):
|
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
|
2010-01-22 22:30:06 +08:00
|
|
|
try:
|
|
|
|
return hints['instance']._state.db or DEFAULT_DB_ALIAS
|
|
|
|
except KeyError:
|
|
|
|
return DEFAULT_DB_ALIAS
|
|
|
|
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
|
|
|
|
2013-07-30 19:08:59 +08:00
|
|
|
def allow_migrate(self, db, model):
|
2010-01-25 20:23:30 +08:00
|
|
|
for router in self.routers:
|
2010-01-27 15:56:53 +08:00
|
|
|
try:
|
2013-07-30 19:08:59 +08:00
|
|
|
try:
|
|
|
|
method = router.allow_migrate
|
|
|
|
except AttributeError:
|
2014-02-10 01:47:40 +08:00
|
|
|
method = router.allow_syncdb
|
2014-02-09 19:40:23 +08:00
|
|
|
warnings.warn(
|
2014-02-10 01:47:40 +08:00
|
|
|
'Router.allow_syncdb has been deprecated and will stop working in Django 1.9. '
|
2014-02-09 19:40:23 +08:00
|
|
|
'Rename the method to allow_migrate.',
|
2014-02-27 05:48:20 +08:00
|
|
|
RemovedInDjango19Warning, stacklevel=2)
|
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(db, model)
|
|
|
|
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
|
|
|
|
2013-12-30 04:21:23 +08:00
|
|
|
def get_migratable_models(self, app_config, db, include_auto_created=False):
|
2013-10-16 23:58:21 +08:00
|
|
|
"""
|
|
|
|
Return app models allowed to be synchronized on provided db.
|
|
|
|
"""
|
2013-12-30 04:21:23 +08:00
|
|
|
models = app_config.get_models(include_auto_created=include_auto_created)
|
|
|
|
return [model for model in models if self.allow_migrate(db, model)]
|