web/lib/django/db/__init__.py
changeset 29 cc9b7e14412b
parent 0 0d40e90630ef
--- a/web/lib/django/db/__init__.py	Wed May 19 17:43:59 2010 +0200
+++ b/web/lib/django/db/__init__.py	Tue May 25 02:43:45 2010 +0200
@@ -1,44 +1,67 @@
-import os
 from django.conf import settings
 from django.core import signals
 from django.core.exceptions import ImproperlyConfigured
+from django.db.utils import ConnectionHandler, ConnectionRouter, load_backend, DEFAULT_DB_ALIAS, \
+                            DatabaseError, IntegrityError
 from django.utils.functional import curry
-from django.utils.importlib import import_module
+
+__all__ = ('backend', 'connection', 'connections', 'router', 'DatabaseError',
+    'IntegrityError', 'DEFAULT_DB_ALIAS')
+
 
-__all__ = ('backend', 'connection', 'DatabaseError', 'IntegrityError')
+# 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
+    )
 
-if not settings.DATABASE_ENGINE:
-    settings.DATABASE_ENGINE = 'dummy'
+    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,
+    }
 
-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)
+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
 
-backend = load_backend(settings.DATABASE_ENGINE)
+connections = ConnectionHandler(settings.DATABASES)
+
+router = ConnectionRouter(settings.DATABASE_ROUTERS)
 
 # `connection`, `DatabaseError` and `IntegrityError` are convenient aliases
 # for backend bits.
@@ -47,36 +70,32 @@
 # 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,
-})
-DatabaseError = backend.DatabaseError
-IntegrityError = backend.IntegrityError
+# 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'])
 
 # Register an event that closes the database connection
 # when a Django request is finished.
 def close_connection(**kwargs):
-    connection.close()
+    for conn in connections.all():
+        conn.close()
 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 conn in connections.all():
+        conn.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)