[PATCH v4 2/8] settings: move database settings into base

You-Sheng Yang vicamo at gmail.com
Thu Dec 9 14:38:37 AEDT 2021


Signed-off-by: You-Sheng Yang <vicamo at gmail.com>
---
 patchwork/settings/base.py                    | 39 +++++++++++++++++++
 patchwork/settings/dev.py                     | 28 +------------
 patchwork/settings/production.example.py      | 17 --------
 ...e-settings-into-base-f0d05cf75170d55f.yaml |  5 +++
 4 files changed, 46 insertions(+), 43 deletions(-)
 create mode 100644 releasenotes/notes/settings-move-database-settings-into-base-f0d05cf75170d55f.yaml

diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
index ff14d91..5e200f0 100644
--- a/patchwork/settings/base.py
+++ b/patchwork/settings/base.py
@@ -105,6 +105,45 @@ STATICFILES_DIRS = [
     os.path.join(ROOT_DIR, 'htdocs'),
 ]
 
+# Database
+#
+# If you're using a postgres database, connecting over a local unix-domain
+# socket, then the following setting should work for you. Otherwise,
+# see https://docs.djangoproject.com/en/2.2/ref/settings/#databases
+
+if os.getenv('DATABASE_TYPE', None) == 'postgres':
+    DATABASES = {
+        'default': {
+            'ENGINE': 'django.db.backends.postgresql_psycopg2',
+            'HOST': os.environ.get('DATABASE_HOST', 'localhost'),
+            'PORT': os.environ.get('DATABASE_PORT', ''),
+            'NAME': os.environ.get('DATABASE_NAME', 'patchwork'),
+            'USER': os.environ.get('DATABASE_USER', 'patchwork'),
+            'PASSWORD': os.environ.get('DATABASE_PASSWORD', 'password'),
+        },
+    }
+elif os.getenv('DATABASE_TYPE', None) == 'sqlite3':
+    DATABASES = {
+        'default': {
+            'ENGINE': 'django.db.backends.sqlite3',
+            'NAME': os.environ.get('DATABASE_NAME', ''),
+        },
+    }
+else:
+    DATABASES = {
+        'default': {
+            'ENGINE': 'django.db.backends.mysql',
+            'HOST': os.getenv('DATABASE_HOST', 'localhost'),
+            'PORT': os.getenv('DATABASE_PORT', ''),
+            'NAME': os.getenv('DATABASE_NAME', 'patchwork'),
+            'USER': os.getenv('DATABASE_USER', 'patchwork'),
+            'PASSWORD': os.getenv('DATABASE_PASSWORD', 'password'),
+            'TEST': {
+                'CHARSET': 'utf8',
+            },
+        },
+    }
+
 #
 # Third-party application settings
 #
diff --git a/patchwork/settings/dev.py b/patchwork/settings/dev.py
index 4487494..cb4cb19 100644
--- a/patchwork/settings/dev.py
+++ b/patchwork/settings/dev.py
@@ -7,8 +7,6 @@ Design based on:
     http://www.revsys.com/blog/2014/nov/21/recommended-django-project-layout/
 """
 
-import os
-
 from .base import *  # noqa
 
 try:
@@ -36,30 +34,8 @@ SECRET_KEY = '00000000000000000000000000000000000000000000000000'  # noqa
 
 DEBUG = True
 
-DATABASES = {
-    'default': {
-        'ENGINE': 'django.db.backends.mysql',
-        'HOST': os.getenv('DATABASE_HOST', 'localhost'),
-        'PORT': os.getenv('DATABASE_PORT', ''),
-        'USER': os.getenv('DATABASE_USER', 'patchwork'),
-        'PASSWORD': os.getenv('DATABASE_PASSWORD', 'password'),
-        'NAME': os.getenv('DATABASE_NAME', 'patchwork'),
-        'TEST': {
-            'CHARSET': 'utf8',
-        },
-    },
-}
-
-if os.getenv('DATABASE_TYPE', None) == 'postgres':
-    DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
-    DATABASES['default']['HOST'] = os.getenv('DATABASE_HOST', '')
-elif os.getenv('DATABASE_TYPE', None) == 'sqlite':
-    DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
-    DATABASES['default']['NAME'] = '/dev/shm/patchwork.test.db.sqlite3'
-    del DATABASES['default']['HOST']
-    del DATABASES['default']['PORT']
-    del DATABASES['default']['USER']
-    del DATABASES['default']['PASSWORD']
+if DATABASES['default']['ENGINE'] == 'mysql':  # noqa: F405
+    DATABASES['default']['TEST'] = {'CHARSET': 'utf8'}  # noqa: F405
 
 EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
 
diff --git a/patchwork/settings/production.example.py b/patchwork/settings/production.example.py
index caaf429..ff05629 100644
--- a/patchwork/settings/production.example.py
+++ b/patchwork/settings/production.example.py
@@ -48,23 +48,6 @@ ADMINS = (
     # ('Jeremy Kerr', 'jk at ozlabs.org'),
 )
 
-# Database
-#
-# If you're using a postgres database, connecting over a local unix-domain
-# socket, then the following setting should work for you. Otherwise,
-# see https://docs.djangoproject.com/en/2.2/ref/settings/#databases
-
-DATABASES = {
-    'default': {
-        'ENGINE': 'django.db.backends.postgresql_psycopg2',
-        'NAME': os.environ.get('DATABASE_NAME', ''),
-        'USER': os.environ.get('DATABASE_USER', ''),
-        'PASSWORD': os.environ.get('DATABASE_PASSWORD', ''),
-        'HOST': os.environ.get('DATABASE_HOST', ''),
-        'PORT': os.environ.get('DATABASE_PORT', ''),
-    },
-}
-
 #
 # Static files settings
 # https://docs.djangoproject.com/en/2.2/ref/settings/#static-files
diff --git a/releasenotes/notes/settings-move-database-settings-into-base-f0d05cf75170d55f.yaml b/releasenotes/notes/settings-move-database-settings-into-base-f0d05cf75170d55f.yaml
new file mode 100644
index 0000000..c313437
--- /dev/null
+++ b/releasenotes/notes/settings-move-database-settings-into-base-f0d05cf75170d55f.yaml
@@ -0,0 +1,5 @@
+---
+fixes:
+  - |
+    Move database configurations into patchwork.settings.base. DATABASE_HOST
+    has a common default value 'localhost' from now on.
-- 
2.32.0



More information about the Patchwork mailing list