>From 881c30549a59a54766858c55d147f6ea83ca486a Mon Sep 17 00:00:00 2001
From: Mike Truty <[email protected]>
Date: Mon, 29 Aug 2011 13:05:16 -0700
Subject: [PATCH] Fix django 1.3 DeprecationWarning for DATABASES.
Django 1.3 which we migrated to, modified the settings structure so that
the database settings are no longer a set of constants. They are replaced
with a dictionary of dictionaries where the 'default' dictionary contains
the desired settings. I've switched to that format to avoid the warning. Not
sure when those constants will actually stop working.
This is the actual message that no longer shows:
/usr/local/autotest/site-packages/django/db/__init__.py:19: DeprecationWarning:
settings.DATABASE_* is deprecated; use settings.DATABASES instead.
TEST=ran dashboard generation on local autotest
Change-Id: I647f2c353b67a0ec820895c7973bfffab84f3f87
---
frontend/afe/readonly_connection.py | 21 ++++++++++-------
frontend/afe/test.py | 2 +-
frontend/settings.py | 41 ++++++++++++++++------------------
frontend/setup_test_environment.py | 7 +++--
4 files changed, 36 insertions(+), 35 deletions(-)
diff --git a/frontend/afe/readonly_connection.py
b/frontend/afe/readonly_connection.py
index b8ce334..38f8c7d 100644
--- a/frontend/afe/readonly_connection.py
+++ b/frontend/afe/readonly_connection.py
@@ -45,22 +45,25 @@ class ReadOnlyConnection(object):
def _save_django_state(self):
self._old_connection = django_db.connection.connection
- self._old_host = settings.DATABASE_HOST
- self._old_username = settings.DATABASE_USER
- self._old_password = settings.DATABASE_PASSWORD
+ _default_db = settings.DATABASES['default']
+ self._old_host = _default_db['HOST']
+ self._old_username = _default_db['USER']
+ self._old_password = _default_db['PASSWORD']
def _restore_django_state(self):
django_db.connection.connection = self._old_connection
- settings.DATABASE_HOST = self._old_host
- settings.DATABASE_USER = self._old_username
- settings.DATABASE_PASSWORD = self._old_password
+ _default_db = settings.DATABASES['default']
+ _default_db['HOST'] = self._old_host
+ _default_db['USER'] = self._old_username
+ _default_db['PASSWORD'] = self._old_password
def _get_readonly_connection(self):
- settings.DATABASE_HOST = settings.DATABASE_READONLY_HOST
- settings.DATABASE_USER = settings.DATABASE_READONLY_USER
- settings.DATABASE_PASSWORD = settings.DATABASE_READONLY_PASSWORD
+ _default_db = settings.DATABASES['default']
+ _default_db['HOST'] = _default_db['READONLY_HOST']
+ _default_db['USER'] = _default_db['READONLY_USER']
+ _default_db['PASSWORD'] = _default_db['READONLY_PASSWORD']
reload(django_db)
# cursor() causes a new connection to be created
cursor = django_db.connection.cursor()
diff --git a/frontend/afe/test.py b/frontend/afe/test.py
index 431272e..999f40e 100644
--- a/frontend/afe/test.py
+++ b/frontend/afe/test.py
@@ -55,7 +55,7 @@ class DoctestRunner(object):
doctest_paths = self._get_doctest_paths()
modules = self._get_modules()
total_errors = 0
- old_db = settings.DATABASE_NAME
+ old_db = settings.DATABASES['default']['NAME']
django.test.utils.setup_test_environment()
connection.creation.create_test_db()
try:
diff --git a/frontend/settings.py b/frontend/settings.py
index 666021a..40e27e0 100644
--- a/frontend/settings.py
+++ b/frontend/settings.py
@@ -19,29 +19,26 @@ ADMINS = (
MANAGERS = ADMINS
-DATABASE_ENGINE = 'autotest_lib.frontend.db.backends.afe'
- # 'postgresql_psycopg2', 'postgresql',
- # 'mysql', 'sqlite3' or 'ado_mssql'.
-DATABASE_PORT = '' # Set to empty string for default.
- # Not used with sqlite3.
-
-DATABASE_HOST = c.get_config_value(_section, "host")
-# Or path to database file if using sqlite3.
-DATABASE_NAME = c.get_config_value(_section, "database")
-# The following not used with sqlite3.
-DATABASE_USER = c.get_config_value(_section, "user")
-DATABASE_PASSWORD = c.get_config_value(_section, "password", default='')
-
-DATABASE_READONLY_HOST = c.get_config_value(_section, "readonly_host",
- default=DATABASE_HOST)
-DATABASE_READONLY_USER = c.get_config_value(_section, "readonly_user",
- default=DATABASE_USER)
-if DATABASE_READONLY_USER != DATABASE_USER:
- DATABASE_READONLY_PASSWORD = c.get_config_value(_section,
- "readonly_password",
- default='')
+def _get_config(config_key, default=None):
+ return c.get_config_value(_section, config_key, default=default)
+
+AUTOTEST_DEFAULT = {
+ 'ENGINE': 'autotest_lib.frontend.db.backends.afe',
+ 'PORT': '',
+ 'HOST': _get_config("host"),
+ 'NAME': _get_config("database"),
+ 'USER': _get_config("user"),
+ 'PASSWORD': _get_config("password", default=''),
+ 'READONLY_HOST': _get_config("readonly_host", default=_get_config("host")),
+ 'READONLY_USER': _get_config("readonly_user", default=_get_config("user"))}
+
+if AUTOTEST_DEFAULT['READONLY_USER'] != AUTOTEST_DEFAULT['USER']:
+ AUTOTEST_DEFAULT['READONLY_PASSWORD'] = _get_config("readonly_password",
+ default='')
else:
- DATABASE_READONLY_PASSWORD = DATABASE_PASSWORD
+ AUTOTEST_DEFAULT['READONLY_PASSWORD'] = AUTOTEST_DEFAULT['PASSWORD']
+
+DATABASES = {'default': AUTOTEST_DEFAULT}
# prefix applied to all URLs - useful if requests are coming through apache,
# and you need this app to coexist with others
diff --git a/frontend/setup_test_environment.py
b/frontend/setup_test_environment.py
index 658541c..0ca2788 100644
--- a/frontend/setup_test_environment.py
+++ b/frontend/setup_test_environment.py
@@ -7,9 +7,10 @@ import common
# system gets initialized.
# django.conf.settings.LazySettings is buggy and requires us to get something
# from it before we set stuff on it.
-getattr(settings, 'DATABASE_ENGINE')
-settings.DATABASE_ENGINE = 'autotest_lib.frontend.db.backends.afe_sqlite'
-settings.DATABASE_NAME = ':memory:'
+getattr(settings, 'DATABASES')
+settings.DATABASES['default']['ENGINE'] = (
+ 'autotest_lib.frontend.db.backends.afe_sqlite')
+settings.DATABASES['default']['NAME'] = ':memory:'
from django.db import connection
from autotest_lib.frontend.afe import readonly_connection
--
1.7.3.1
_______________________________________________
Autotest mailing list
[email protected]
http://test.kernel.org/cgi-bin/mailman/listinfo/autotest