diff mbox series

[1/4] settings: unify database connection variables

Message ID 20211207084916.240554-2-vicamo@gmail.com
State Superseded
Headers show
Series Test SQLite configuration | expand

Commit Message

You-Sheng Yang Dec. 7, 2021, 8:49 a.m. UTC
Signed-off-by: You-Sheng Yang <vicamo@gmail.com>
---
 .github/workflows/ci.yaml                | 39 +++++++++++-------
 docker-compose-pg.yml                    | 12 +++---
 docker-compose.yml                       | 12 +++---
 docs/deployment/installation.rst         |  2 +-
 docs/development/installation.rst        | 13 +++---
 patchwork/settings/base.py               | 39 ++++++++++++++++++
 patchwork/settings/dev.py                | 28 +------------
 patchwork/settings/production.example.py | 17 --------
 tools/docker/entrypoint.sh               | 51 ++++++++++++++++--------
 tox.ini                                  |  4 +-
 10 files changed, 121 insertions(+), 96 deletions(-)
diff mbox series

Patch

diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index f4a33b3..04c67cb 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -25,13 +25,18 @@  jobs:
       matrix:
         python: [3.6, 3.7, 3.8, 3.9]
         db: [postgres, mysql]
+    env:
+      DATABASE_NAME: patchwork
+      DATABASE_USER: patchwork
+      DATABASE_PASS: patchwork
+      MYSQL_ROOT_PASSWORD: root
     services:
       postgres:
         image: postgres:latest
         env:
-          POSTGRES_DB: patchwork
-          POSTGRES_PASSWORD: patchwork
-          POSTGRES_USER: patchwork
+          POSTGRES_DB: ${{ env.DATABASE_NAME }}
+          POSTGRES_PASSWORD: ${{ env.DATABASE_PASS }}
+          POSTGRES_USER: ${{ env.DATABASE_USER }}
         ports:
           - 5432:5432
         options: >-
@@ -42,10 +47,10 @@  jobs:
       mysql:
         image: mysql:latest
         env:
-          MYSQL_DATABASE: patchwork
-          MYSQL_USER: patchwork
-          MYSQL_PASSWORD: patchwork
-          MYSQL_ROOT_PASSWORD: root
+          MYSQL_DATABASE: ${{ env.DATABASE_NAME }}
+          MYSQL_USER: ${{ env.DATABASE_USER }}
+          MYSQL_PASSWORD: ${{ env.DATABASE_PASS }}
+          MYSQL_ROOT_PASSWORD: ${{ env.MYSQL_ROOT_PASSWORD }}
         ports:
           - 3306:3306
         options: >-
@@ -64,22 +69,26 @@  jobs:
         run: python -m pip install tox tox-gh-actions codecov
       - name: Log database configuration (mysql)
         if: ${{ matrix.db == 'mysql' }}
-        run: mysql -h 127.0.0.1 -e "SELECT VERSION(), CURRENT_USER();" -uroot -proot patchwork
+        run: |
+          mysql -h 127.0.0.1 -e "SELECT VERSION(), CURRENT_USER();" \
+              -uroot -p${MYSQL_ROOT_PASSWORD} ${DATABASE_NAME}
       - name: Log database configuration (postgres)
         if: ${{ matrix.db == 'postgres' }}
-        run: psql -h 127.0.0.1 -c "SELECT VERSION(), CURRENT_USER, current_database()" -U patchwork -d patchwork
+        run: |
+          psql -h 127.0.0.1 -c "SELECT VERSION(), CURRENT_USER, current_database()" \
+              -U ${DATABASE_USER} -d ${DATABASE_NAME}
         env:
-          PGPASSWORD: patchwork
+          PGPASSWORD: ${{ env.DATABASE_PASS }}
       - name: Modify database user permissions (mysql)
         if: ${{ matrix.db == 'mysql' }}
-        run: mysql -h 127.0.0.1 -e "GRANT ALL ON \`test\\_patchwork%\`.* to 'patchwork'@'%';" -uroot -proot
+        run: |
+          mysql -h 127.0.0.1 -e "GRANT ALL ON \`test\\_${DATABASE_NAME}%\`.* to '${DATABASE_USER}'@'%';" \
+              -uroot -p${MYSQL_ROOT_PASSWORD}
       - name: Run unit tests (via tox)
         run: tox
         env:
-          PW_TEST_DB_TYPE: "${{ matrix.db }}"
-          PW_TEST_DB_USER: "patchwork"
-          PW_TEST_DB_PASS: "patchwork"
-          PW_TEST_DB_HOST: "127.0.0.1"
+          DATABASE_TYPE: "${{ matrix.db }}"
+          DATABASE_HOST: "127.0.0.1"
   docs:
     name: Build docs
     runs-on: ubuntu-latest
diff --git a/docker-compose-pg.yml b/docker-compose-pg.yml
index 44c49fb..a45240e 100644
--- a/docker-compose-pg.yml
+++ b/docker-compose-pg.yml
@@ -24,9 +24,9 @@  services:
     environment:
       - UID
       - GID
-      - PW_TEST_DB_HOST=db
-      - PW_TEST_DB_PORT=5432
-      - PW_TEST_DB_TYPE=postgres
-      - PW_TEST_DB_USER=postgres
-      - PW_TEST_DB_PASS=password
-      - PGPASSWORD=password
+      - DATABASE_TYPE=postgres
+      - DATABASE_HOST=db
+      - DATABASE_PORT=5432
+      - DATABASE_NAME=patchwork
+      - DATABASE_USER=postgres
+      - DATABASE_PASS=password
diff --git a/docker-compose.yml b/docker-compose.yml
index 1d49c51..1673823 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -5,9 +5,7 @@  services:
     volumes:
       - ./tools/docker/db/data:/var/lib/mysql
     environment:
-      - MYSQL_ROOT_PASSWORD=password
-      - MYSQL_USER=patchwork
-      - MYSQL_PASSWORD=password
+      - MYSQL_ROOT_PASSWORD=root
 
   web:
     build:
@@ -26,5 +24,9 @@  services:
     environment:
       - UID
       - GID
-      - PW_TEST_DB_HOST=db
-      - PW_TEST_DB_PORT=3306
+      # skip DATABASE_TYPE explicitly as mysql should be the default type.
+      - DATABASE_HOST=db
+      - DATABASE_PORT=3306
+      - DATABASE_USER=patchwork
+      - DATABASE_PASS=password
+      - MYSQL_ROOT_PASSWORD=root
diff --git a/docs/deployment/installation.rst b/docs/deployment/installation.rst
index 1abf187..9df31d9 100644
--- a/docs/deployment/installation.rst
+++ b/docs/deployment/installation.rst
@@ -229,7 +229,7 @@  As a reminder, these were:
 
 - ``DATABASE_NAME``
 - ``DATABASE_USER``
-- ``DATABASE_PASSWORD``
+- ``DATABASE_PASS``
 - ``DATABASE_HOST``
 - ``DATABASE_PORT``
 
diff --git a/docs/development/installation.rst b/docs/development/installation.rst
index d16177c..59c7847 100644
--- a/docs/development/installation.rst
+++ b/docs/development/installation.rst
@@ -316,7 +316,7 @@  if necessary:
 
    The ``patchwork`` username and ``password`` password are the defaults
    expected by the provided ``dev`` settings files. If using something
-   different, export the ``PW_TEST_DB_USER`` and ``PW_TEST_DB_PASS`` variables
+   different, export the ``DATABASE_USER`` and ``DATABASE_PASS`` variables
    described in the :ref:`Environment Variables <dev-envvar>` section below.
    Alternatively, you can create your own settings file with these variables
    hardcoded and change the value of ``DJANGO_SETTINGS_MODULE`` as described
@@ -470,17 +470,16 @@  __ https://django-dbbackup.readthedocs.io/en/stable/
 Environment Variables
 ---------------------
 
-The following environment variables are available to configure settings when
-using the provided ``dev`` settings file.
+The following environment variables are available to configure settings.
 
-``PW_TEST_DB_NAME=patchwork``
+``DATABASE_NAME=patchwork``
   Name of the database
 
-``PW_TEST_DB_USER=patchwork``
+``DATABASE_USER=patchwork``
   Username to access the database with
 
-``PW_TEST_DB_PASS=password``
+``DATABASE_PASS=password``
   Password to access the database with<
 
-``PW_TEST_DB_TYPE=mysql``
+``DATABASE_TYPE=mysql``
   Type of database to use. Options: ``mysql``, ``postgres``
diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
index ff14d91..d71e7dd 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_PASS', '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_PASS', 'password'),
+            'TEST': {
+                'CHARSET': 'utf8',
+            },
+        },
+    }
+
 #
 # Third-party application settings
 #
diff --git a/patchwork/settings/dev.py b/patchwork/settings/dev.py
index 2b7fc95..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('PW_TEST_DB_HOST', 'localhost'),
-        'PORT': os.getenv('PW_TEST_DB_PORT', ''),
-        'USER': os.getenv('PW_TEST_DB_USER', 'patchwork'),
-        'PASSWORD': os.getenv('PW_TEST_DB_PASS', 'password'),
-        'NAME': os.getenv('PW_TEST_DB_NAME', 'patchwork'),
-        'TEST': {
-            'CHARSET': 'utf8',
-        },
-    },
-}
-
-if os.getenv('PW_TEST_DB_TYPE', None) == 'postgres':
-    DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
-    DATABASES['default']['HOST'] = os.getenv('PW_TEST_DB_HOST', '')
-elif os.getenv('PW_TEST_DB_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@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/tools/docker/entrypoint.sh b/tools/docker/entrypoint.sh
index 5450a53..d111944 100755
--- a/tools/docker/entrypoint.sh
+++ b/tools/docker/entrypoint.sh
@@ -1,45 +1,62 @@ 
 #!/bin/bash
 set -euo pipefail
 
-PW_TEST_DB_TYPE=${PW_TEST_DB_TYPE:-mysql}
+export DATABASE_HOST=${DATABASE_HOST:-}
+export DATABASE_PORT=${DATABASE_PORT:-}
+export DATABASE_NAME=${DATABASE_NAME:-patchwork}
+export DATABASE_USER=${DATABASE_USER:-patchwork}
+export DATABASE_PASS=${DATABASE_PASS:-password}
+
+case "${DATABASE_TYPE:-}" in
+postgres)
+    export PGPORT=${DATABASE_PORT}
+    export PGPASSWORD=${DATABASE_PASS}
+    psql_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} "--username=${DATABASE_USER}" )
+    ;;
+*)
+    export DATABASE_TYPE=mysql
+    mysql_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} ${DATABASE_PORT:+--port=${DATABASE_PORT}} "--user=${DATABASE_USER}" "--password=${DATABASE_PASS}" )
+    mysql_root_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} ${DATABASE_PORT:+--port=${DATABASE_PORT}} "--user=root" "--password=${MYSQL_ROOT_PASSWORD:-}" )
+    ;;
+esac
 
 # functions
 
 test_db_connection() {
-    if [ ${PW_TEST_DB_TYPE} = "postgres" ]; then
-        echo ';' | psql -h $PW_TEST_DB_HOST -U postgres 2> /dev/null > /dev/null
+    if [ ${DATABASE_TYPE} = "postgres" ]; then
+        echo ';' | psql "${psql_args[@]}" 2> /dev/null > /dev/null
     else
-        mysqladmin -h $PW_TEST_DB_HOST -u patchwork --password=password ping > /dev/null 2> /dev/null
+        mysqladmin "${mysql_root_args[@]}" ping > /dev/null 2> /dev/null
     fi
 }
 
 test_database() {
-    if [ ${PW_TEST_DB_TYPE} = "postgres" ]; then
-        echo ';' | psql -h $PW_TEST_DB_HOST -U postgres patchwork 2> /dev/null
+    if [ ${DATABASE_TYPE} = "postgres" ]; then
+        echo ';' | psql "${psql_args[@]}" "${DATABASE_NAME}" 2> /dev/null
     else
-        echo ';' | mysql -h $PW_TEST_DB_HOST -u patchwork -ppassword patchwork 2> /dev/null
+        echo ';' | mysql "${mysql_args[@]}" "${DATABASE_NAME}" 2> /dev/null
     fi
 }
 
 reset_data_mysql() {
-    mysql -uroot -ppassword -h $PW_TEST_DB_HOST << EOF
-DROP DATABASE IF EXISTS patchwork;
-CREATE DATABASE patchwork CHARACTER SET utf8;
-GRANT ALL ON patchwork.* TO 'patchwork' IDENTIFIED BY 'password';
-GRANT ALL ON \`test\\_patchwork%\`.* to 'patchwork'@'%';
+    mysql "${mysql_root_args[@]}" << EOF
+DROP DATABASE IF EXISTS ${DATABASE_NAME};
+CREATE DATABASE ${DATABASE_NAME} CHARACTER SET utf8;
+GRANT ALL ON ${DATABASE_NAME}.* TO '${DATABASE_USER}' IDENTIFIED BY '${DATABASE_PASS}';
+GRANT ALL ON \`test\\_${DATABASE_NAME}%\`.* to '${DATABASE_USER}'@'%';
 FLUSH PRIVILEGES;
 EOF
 }
 
 reset_data_postgres() {
-    psql -h $PW_TEST_DB_HOST -U postgres <<EOF
-DROP DATABASE IF EXISTS patchwork;
-CREATE DATABASE patchwork WITH ENCODING = 'UTF8';
+    psql "${psql_args[@]}" <<EOF
+DROP DATABASE IF EXISTS ${DATABASE_NAME};
+CREATE DATABASE ${DATABASE_NAME} WITH ENCODING = 'UTF8';
 EOF
 }
 
 reset_data() {
-    if [ x${PW_TEST_DB_TYPE} = x"postgres" ]; then
+    if [ x${DATABASE_TYPE} = x"postgres" ]; then
         reset_data_postgres
     else
         reset_data_mysql
@@ -87,7 +104,7 @@  set -e
 
 # check if db is connected
 if ! test_db_connection; then
-    echo "The database seems not to be connected, or the patchwork user is broken"
+    echo "The database seems not to be connected, or the ${DATABASE_USER} user is broken"
     echo "MySQL/Postgres may still be starting. Waiting 5 seconds."
     sleep 5
     if ! test_db_connection; then
diff --git a/tox.ini b/tox.ini
index 0ed500a..aeb715e 100644
--- a/tox.ini
+++ b/tox.ini
@@ -25,8 +25,8 @@  setenv =
     py{37,38,39}: PYTHONWARNINGS = once
 passenv =
     http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy NO_PROXY
-    PW_TEST_DB_TYPE PW_TEST_DB_USER PW_TEST_DB_PASS PW_TEST_DB_HOST
-    PW_TEST_DB_PORT PW_TEST_DB_NAME DJANGO_TEST_PROCESSES
+    DATABASE_TYPE DATABASE_USER DATABASE_PASS DATABASE_HOST
+    DATABASE_PORT DATABASE_NAME DJANGO_TEST_PROCESSES
 commands =
     python {toxinidir}/manage.py test --noinput --parallel -- {posargs:patchwork}