diff mbox series

[v4,6/8] docker: drop database maintenance stuff

Message ID 20211209033843.205325-7-vicamo@gmail.com
State Accepted
Headers show
Series Test SQLite configuration | expand

Commit Message

You-Sheng Yang Dec. 9, 2021, 3:38 a.m. UTC
Trying to re-create database inside a client docker could be risky and
error prone.

1. The previous handling process may fail service startup when an empty
database was 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.

2. Checking the existence of a patchwork specific table is also invalid
under the empty database scenario, because migrations might still remain
to be done.

As a result, we shall test if the database exist and do migrations
always, so this change removes database maintenance functions.

Signed-off-by: You-Sheng Yang <vicamo@gmail.com>
---
 docker-compose-pg.yml                         |  2 +
 docker-compose.yml                            |  3 +-
 docs/development/installation.rst             | 10 ++-
 ...se-maintenance-stuff-e3317975c1c53ade.yaml |  5 ++
 tools/docker/entrypoint.sh                    | 67 +++----------------
 5 files changed, 25 insertions(+), 62 deletions(-)
 create mode 100644 releasenotes/notes/docker-drop-database-maintenance-stuff-e3317975c1c53ade.yaml
diff mbox series

Patch

diff --git a/docker-compose-pg.yml b/docker-compose-pg.yml
index e703b96..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:
diff --git a/docker-compose.yml b/docker-compose.yml
index b91b7ef..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
 
@@ -28,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/docs/development/installation.rst b/docs/development/installation.rst
index c5c0269..877c2f6 100644
--- a/docs/development/installation.rst
+++ b/docs/development/installation.rst
@@ -91,6 +91,10 @@  To run unit tests against the system Python packages, run:
 
 .. code-block:: shell
 
+   # For MySQL database:
+   $ docker-compose exec -T -- db sh -c \
+         "exec mysql -uroot -p\"\${MYSQL_ROOT_PASSWORD}\" -e \"GRANT ALL ON \\\`test\\_\${MYSQL_DATABASE}%\\\`.* to '\${MYSQL_USER}'@'%'; FLUSH PRIVILEGES;\""
+
    $ docker-compose run --rm web python manage.py test
 
 To run unit tests for multiple versions using ``tox``, run:
@@ -99,12 +103,12 @@  To run unit tests for multiple versions using ``tox``, run:
 
    $ docker-compose run --rm web tox
 
-To reset the database before any of these commands, add ``--reset`` to the
-command line after ``web`` and before any other arguments:
+To reset the database, stop the db container and purge the database files:
 
 .. code-block:: shell
 
-   $ docker-compose run --rm web --reset tox
+   $ docker-compose stop db
+   $ sudo rm -rf tools/docker/db
 
 Any local edits to the project files made locally are immediately visible to
 the Docker container, and so should be picked up by the Django auto-reloader.
diff --git a/releasenotes/notes/docker-drop-database-maintenance-stuff-e3317975c1c53ade.yaml b/releasenotes/notes/docker-drop-database-maintenance-stuff-e3317975c1c53ade.yaml
new file mode 100644
index 0000000..9cf0fcb
--- /dev/null
+++ b/releasenotes/notes/docker-drop-database-maintenance-stuff-e3317975c1c53ade.yaml
@@ -0,0 +1,5 @@ 
+---
+fixes:
+  - |
+    No longer perform database recreation inside development container startup.
+    Users should provide a ready-to-use database, empty or previously populated.
diff --git a/tools/docker/entrypoint.sh b/tools/docker/entrypoint.sh
index 7e0af5a..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,19 +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
-
-if [ $# -eq 0 ]; then
-    # we probably ran with --reset and nothing else
-    # just exit cleanly
-    exit 0
-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
 
 exec "$@"