]> git.ipfire.org Git - thirdparty/patchwork.git/commitdiff
docker: Drop database maintenance stuff
authorYou-Sheng Yang <vicamo@gmail.com>
Thu, 9 Dec 2021 03:38:41 +0000 (11:38 +0800)
committerStephen Finucane <stephen@that.guru>
Wed, 23 Feb 2022 13:43:59 +0000 (13:43 +0000)
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>
Signed-off-by: Stephen Finucane <stephen@that.guru>
[stephenfin] Removed release note, since this is not end-user focused

docker-compose-pg.yml
docker-compose.yml
docs/development/installation.rst
tools/docker/entrypoint.sh

index e703b96bbffdac1fd6dc14007f1258c3cd8b76c1..9129f323531898a2714fb9ba552176c70a099d38 100644 (file)
@@ -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:
index b91b7ef8a2b87dff122d8b6f623675246e063646..d8244364768da43e5e0e32deb646a18f6360436c 100644 (file)
@@ -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
index c5c026946785cdd46b485f2789ec1dec3739b5f3..877c2f67de8a007fdaff3c639f24b49e5a8384e3 100644 (file)
@@ -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.
index 7e0af5a4b6656c92935c09123d3ca67545ac04df..d1bad8259857ad60bea7e264d5f5526bd7a5891e 100755 (executable)
@@ -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 "$@"