[PATCH v2 2/4] docker: drop database maintenance stuff

You-Sheng Yang vicamo at gmail.com
Wed Dec 8 00:15:01 AEDT 2021


Trying to re-create database inside a client docker could be risky and
error prone. The previous handling process may fail service startup when
an empty database was previously created. `test_database()` will always
succeed as it tests only the existence of the database, not any table
inside. Without migrations being called on such empty database,
patchwork cannot work normally. Checking the existence of a patchwork
specific table is also invalid under the empty database scenario. And
even the inspecting table does exist, migration process might be still
necessary.

As a result, we shall test if the database exist and do migration
always. This change removes database maintenance functions and those
marked deprecated long ago.

Signed-off-by: You-Sheng Yang <vicamo at gmail.com>
---
 docker-compose-pg.yml      |  5 ++-
 docker-compose.yml         |  4 +-
 tools/docker/Dockerfile    |  1 +
 tools/docker/entrypoint.sh | 79 +++++---------------------------------
 4 files changed, 16 insertions(+), 73 deletions(-)

diff --git a/docker-compose-pg.yml b/docker-compose-pg.yml
index 7398929..9129f32 100644
--- a/docker-compose-pg.yml
+++ b/docker-compose-pg.yml
@@ -5,6 +5,8 @@ services:
     volumes:
       - ./tools/docker/db/postdata:/var/lib/postgresql/data
     environment:
+      - POSTGRES_DB=patchwork
+      - POSTGRES_USER=patchwork
       - POSTGRES_PASSWORD=password
 
   web:
@@ -16,7 +18,6 @@ services:
         - GID
     depends_on:
       - db
-    command: python3 manage.py runserver 0.0.0.0:8000
     volumes:
       - .:/home/patchwork/patchwork/
     ports:
@@ -28,5 +29,5 @@ services:
       - DATABASE_HOST=db
       - DATABASE_PORT=5432
       - DATABASE_NAME=patchwork
-      - DATABASE_USER=postgres
+      - DATABASE_USER=patchwork
       - DATABASE_PASSWORD=password
diff --git a/docker-compose.yml b/docker-compose.yml
index 103f19f..d824436 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -6,6 +6,7 @@ services:
       - ./tools/docker/db/data:/var/lib/mysql
     environment:
       - MYSQL_ROOT_PASSWORD=root
+      - MYSQL_DATABASE=patchwork
       - MYSQL_USER=patchwork
       - MYSQL_PASSWORD=password
 
@@ -18,7 +19,6 @@ services:
         - GID
     depends_on:
       - db
-    command: python3 manage.py runserver 0.0.0.0:8000
     volumes:
       - .:/home/patchwork/patchwork/
     ports:
@@ -29,6 +29,6 @@ services:
       # skip DATABASE_TYPE explicitly as mysql should be the default type.
       - DATABASE_HOST=db
       - DATABASE_PORT=3306
+      - DATABASE_NAME=patchwork
       - DATABASE_USER=patchwork
       - DATABASE_PASSWORD=password
-      - MYSQL_ROOT_PASSWORD=root
diff --git a/tools/docker/Dockerfile b/tools/docker/Dockerfile
index fe0cd59..71167e0 100644
--- a/tools/docker/Dockerfile
+++ b/tools/docker/Dockerfile
@@ -61,5 +61,6 @@ RUN pip install -r /opt/requirements-dev.txt
 
 COPY tools/docker/entrypoint.sh /usr/local/bin/entrypoint.sh
 ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
+CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]
 USER patchwork
 WORKDIR /home/patchwork/patchwork
diff --git a/tools/docker/entrypoint.sh b/tools/docker/entrypoint.sh
index faa36b3..d1bad82 100755
--- a/tools/docker/entrypoint.sh
+++ b/tools/docker/entrypoint.sh
@@ -16,20 +16,11 @@ postgres)
 *)
     export DATABASE_TYPE=mysql
     mysql_args=( ${DATABASE_HOST:+--host=${DATABASE_HOST}} ${DATABASE_PORT:+--port=${DATABASE_PORT}} "--user=${DATABASE_USER}" "--password=${DATABASE_PASSWORD}" )
-    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 [ ${DATABASE_TYPE} = "postgres" ]; then
-        echo ';' | psql "${psql_args[@]}" 2> /dev/null > /dev/null
-    else
-        mysqladmin "${mysql_root_args[@]}" ping > /dev/null 2> /dev/null
-    fi
-}
-
 test_database() {
     if [ ${DATABASE_TYPE} = "postgres" ]; then
         echo ';' | psql "${psql_args[@]}" "${DATABASE_NAME}" 2> /dev/null
@@ -38,37 +29,6 @@ test_database() {
     fi
 }
 
-reset_data_mysql() {
-    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_PASSWORD}';
-GRANT ALL ON \`test\\_${DATABASE_NAME}%\`.* to '${DATABASE_USER}'@'%';
-FLUSH PRIVILEGES;
-EOF
-}
-
-reset_data_postgres() {
-    psql "${psql_args[@]}" <<EOF
-DROP DATABASE IF EXISTS ${DATABASE_NAME};
-CREATE DATABASE ${DATABASE_NAME} WITH ENCODING = 'UTF8';
-EOF
-}
-
-reset_data() {
-    if [ x${DATABASE_TYPE} = x"postgres" ]; then
-        reset_data_postgres
-    else
-        reset_data_mysql
-    fi
-
-    # load initial data
-    python manage.py migrate #> /dev/null
-    python manage.py loaddata default_tags #> /dev/null
-    python manage.py loaddata default_states #> /dev/null
-    python manage.py loaddata default_projects #> /dev/null
-}
-
 # the script begins!
 
 # check if patchwork is mounted. Checking if we exist is a
@@ -103,20 +63,20 @@ done
 set -e
 
 # check if db is connected
-if ! test_db_connection; then
+if ! test_database; then
     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
+    if ! test_database; then
         echo "Still cannot connect to database."
         echo "Maybe you are starting the db for the first time. Waiting up to 60 seconds."
         for i in {0..9}; do
             sleep 5
-            if test_db_connection; then
+            if test_database; then
                 break
             fi
         done
-        if ! test_db_connection; then
+        if ! test_database; then
             echo "Still cannot connect to database. Giving up."
             echo "Are you using docker-compose? If not, have you set up the link correctly?"
             exit 1
@@ -124,29 +84,10 @@ if ! test_db_connection; then
     fi
 fi
 
-# rebuild db
-# do this on --reset or if the db doesn't exist
-if [[ "$1" == "--reset" ]]; then
-    shift
-    reset_data
-elif ! test_database; then
-    reset_data
-fi
+# load initial data
+python manage.py migrate #> /dev/null
+python manage.py loaddata default_tags #> /dev/null
+python manage.py loaddata default_states #> /dev/null
+python manage.py loaddata default_projects #> /dev/null
 
-# TODO(stephenfin): Deprecated the --test, --tox, --quick-test and --quick-tox
-# flags in a future release
-if [ $# -eq 0 ]; then
-    # we probably ran with --reset and nothing else
-    # just exit cleanly
-    exit 0
-elif [ "$1" == "--shell" ]; then
-    exec bash
-elif [ "$1" == "--test" ] || [ "$1" == "--quick-test" ]; then
-    shift
-    python manage.py test $@
-elif [ "$1" == "--tox" ] || [ "$1" == "--quick-tox" ]; then
-    shift
-    tox $@
-else # run whatever CMD is set to
-    $@
-fi
+exec "$@"
-- 
2.32.0



More information about the Patchwork mailing list