summaryrefslogtreecommitdiff
path: root/django/db/__init__.py
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2009-12-22 15:18:51 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2009-12-22 15:18:51 +0000
commitff60c5f9de3e8690d1e86f3e9e3f7248a15397c8 (patch)
treea4cb0ebdd55fcaf8c8855231b6ad3e1a7bf45bee /django/db/__init__.py
parent7ef212af149540aa2da577a960d0d87029fd1514 (diff)
Fixed #1142 -- Added multiple database support.
This monster of a patch is the result of Alex Gaynor's 2009 Google Summer of Code project. Congratulations to Alex for a job well done. Big thanks also go to: * Justin Bronn for keeping GIS in line with the changes, * Karen Tracey and Jani Tiainen for their help testing Oracle support * Brett Hoerner, Jon Loyens, and Craig Kimmerer for their feedback. * Malcolm Treddinick for his guidance during the GSoC submission process. * Simon Willison for driving the original design process * Cal Henderson for complaining about ponies he wanted. ... and everyone else too numerous to mention that helped to bring this feature into fruition. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11952 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/db/__init__.py')
-rw-r--r--django/db/__init__.py113
1 files changed, 66 insertions, 47 deletions
diff --git a/django/db/__init__.py b/django/db/__init__.py
index 61c7a4fc74..2971b67dbc 100644
--- a/django/db/__init__.py
+++ b/django/db/__init__.py
@@ -1,44 +1,66 @@
-import os
from django.conf import settings
from django.core import signals
from django.core.exceptions import ImproperlyConfigured
+from django.db.utils import ConnectionHandler, load_backend
from django.utils.functional import curry
-from django.utils.importlib import import_module
-__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
+__all__ = ('backend', 'connection', 'connections', 'DatabaseError',
+ 'IntegrityError', 'DEFAULT_DB_ALIAS')
-if not settings.DATABASE_ENGINE:
- settings.DATABASE_ENGINE = 'dummy'
+DEFAULT_DB_ALIAS = 'default'
-def load_backend(backend_name):
- try:
- # Most of the time, the database backend will be one of the official
- # backends that ships with Django, so look there first.
- return import_module('.base', 'django.db.backends.%s' % backend_name)
- except ImportError, e:
- # If the import failed, we might be looking for a database backend
- # distributed external to Django. So we'll try that next.
- try:
- return import_module('.base', backend_name)
- except ImportError, e_user:
- # The database backend wasn't found. Display a helpful error message
- # listing all possible (built-in) database backends.
- backend_dir = os.path.join(__path__[0], 'backends')
- try:
- available_backends = [f for f in os.listdir(backend_dir)
- if os.path.isdir(os.path.join(backend_dir, f))
- and not f.startswith('.')]
- except EnvironmentError:
- available_backends = []
- available_backends.sort()
- if backend_name not in available_backends:
- error_msg = "%r isn't an available database backend. Available options are: %s\nError was: %s" % \
- (backend_name, ", ".join(map(repr, available_backends)), e_user)
- raise ImproperlyConfigured(error_msg)
+# For backwards compatibility - Port any old database settings over to
+# the new values.
+if not settings.DATABASES:
+ import warnings
+ warnings.warn(
+ "settings.DATABASE_* is deprecated; use settings.DATABASES instead.",
+ PendingDeprecationWarning
+ )
+
+ settings.DATABASES[DEFAULT_DB_ALIAS] = {
+ 'ENGINE': settings.DATABASE_ENGINE,
+ 'HOST': settings.DATABASE_HOST,
+ 'NAME': settings.DATABASE_NAME,
+ 'OPTIONS': settings.DATABASE_OPTIONS,
+ 'PASSWORD': settings.DATABASE_PASSWORD,
+ 'PORT': settings.DATABASE_PORT,
+ 'USER': settings.DATABASE_USER,
+ 'TEST_CHARSET': settings.TEST_DATABASE_CHARSET,
+ 'TEST_COLLATION': settings.TEST_DATABASE_COLLATION,
+ 'TEST_NAME': settings.TEST_DATABASE_NAME,
+ }
+
+if DEFAULT_DB_ALIAS not in settings.DATABASES:
+ raise ImproperlyConfigured("You must default a '%s' database" % DEFAULT_DB_ALIAS)
+
+for alias, database in settings.DATABASES.items():
+ if database['ENGINE'] in ("postgresql", "postgresql_psycopg2", "sqlite3", "mysql", "oracle"):
+ import warnings
+ if 'django.contrib.gis' in settings.INSTALLED_APPS:
+ warnings.warn(
+ "django.contrib.gis is now implemented as a full database backend. "
+ "Modify ENGINE in the %s database configuration to select "
+ "a backend from 'django.contrib.gis.db.backends'" % alias,
+ PendingDeprecationWarning
+ )
+ if database['ENGINE'] == 'postgresql_psycopg2':
+ full_engine = 'django.contrib.gis.db.backends.postgis'
+ elif database['ENGINE'] == 'sqlite3':
+ full_engine = 'django.contrib.gis.db.backends.spatialite'
else:
- raise # If there's some other error, this must be an error in Django itself.
+ full_engine = 'django.contrib.gis.db.backends.%s' % database['ENGINE']
+ else:
+ warnings.warn(
+ "Short names for ENGINE in database configurations are deprecated. "
+ "Prepend %s.ENGINE with 'django.db.backends.'" % alias,
+ PendingDeprecationWarning
+ )
+ full_engine = "django.db.backends.%s" % database['ENGINE']
+ database['ENGINE'] = full_engine
+
+connections = ConnectionHandler(settings.DATABASES)
-backend = load_backend(settings.DATABASE_ENGINE)
# `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
# for backend bits.
@@ -47,15 +69,10 @@ backend = load_backend(settings.DATABASE_ENGINE)
# we manually create the dictionary from the settings, passing only the
# settings that the database backends care about. Note that TIME_ZONE is used
# by the PostgreSQL backends.
-connection = backend.DatabaseWrapper({
- 'DATABASE_HOST': settings.DATABASE_HOST,
- 'DATABASE_NAME': settings.DATABASE_NAME,
- 'DATABASE_OPTIONS': settings.DATABASE_OPTIONS,
- 'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
- 'DATABASE_PORT': settings.DATABASE_PORT,
- 'DATABASE_USER': settings.DATABASE_USER,
- 'TIME_ZONE': settings.TIME_ZONE,
-})
+# we load all these up for backwards compatibility, you should use
+# connections['default'] instead.
+connection = connections[DEFAULT_DB_ALIAS]
+backend = load_backend(connection.settings_dict['ENGINE'])
DatabaseError = backend.DatabaseError
IntegrityError = backend.IntegrityError
@@ -68,15 +85,17 @@ signals.request_finished.connect(close_connection)
# Register an event that resets connection.queries
# when a Django request is started.
def reset_queries(**kwargs):
- connection.queries = []
+ for connection in connections.all():
+ connection.queries = []
signals.request_started.connect(reset_queries)
-# Register an event that rolls back the connection
+# Register an event that rolls back the connections
# when a Django request has an exception.
def _rollback_on_exception(**kwargs):
from django.db import transaction
- try:
- transaction.rollback_unless_managed()
- except DatabaseError:
- pass
+ for conn in connections:
+ try:
+ transaction.rollback_unless_managed(using=conn)
+ except DatabaseError:
+ pass
signals.got_request_exception.connect(_rollback_on_exception)