From: You-Sheng Yang Date: Thu, 9 Dec 2021 03:38:41 +0000 (+0800) Subject: docker: Drop database maintenance stuff X-Git-Tag: v3.1.0~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6996a92eeb39ef2ca488d41249d9b2d02ebdd89;p=thirdparty%2Fpatchwork.git docker: Drop database maintenance stuff 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 Signed-off-by: Stephen Finucane [stephenfin] Removed release note, since this is not end-user focused --- diff --git a/docker-compose-pg.yml b/docker-compose-pg.yml index e703b96b..9129f323 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 b91b7ef8..d8244364 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 c5c02694..877c2f67 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/tools/docker/entrypoint.sh b/tools/docker/entrypoint.sh index 7e0af5a4..d1bad825 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[@]}" < /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 "$@"