From b49e93db2d2edf3d50e4f9411b3fdd07bc21d05b Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 19 Oct 2023 10:53:35 +0100 Subject: [PATCH 1/5] Update Docker scripts Reduce unnecessary output when commits or bundle are already up to date. --- docker/bootstrap | 2 +- docker/setup | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/bootstrap b/docker/bootstrap index 4d878accd3..637b5012ae 100755 --- a/docker/bootstrap +++ b/docker/bootstrap @@ -44,5 +44,5 @@ notice_msg "Getting latest alavetelitheme commits..." alaveteli=`pwd` alavetelitheme="$themes_dir/alavetelitheme" [ ! -d $alavetelitheme ] && git clone https://github.com/mysociety/alavetelitheme.git $alavetelitheme >/dev/null -cd $alavetelitheme && git pull && cd $alaveteli +cd $alavetelitheme && git pull --quiet && cd $alaveteli success_msg 'done' diff --git a/docker/setup b/docker/setup index a107e5032a..072cc81fee 100755 --- a/docker/setup +++ b/docker/setup @@ -18,7 +18,7 @@ success_msg() { printf "\033[32m%s\033[0m\n" "$*"; } data_reset="${1:-false}" notice_msg 'Installing Ruby gems...' -bundle install +bundle check >/dev/null || bundle install success_msg 'done' if [ -L config/general.yml ]; then From 5411aecda0b3074982abcd33378091c8923f99d6 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 19 Oct 2023 10:54:36 +0100 Subject: [PATCH 2/5] Update Docker scripts Remove exit on error (EG `set -e`) as we want to be able to catch errors and handle them in sub-scripts. --- docker/reset | 2 -- docker/setup | 2 -- 2 files changed, 4 deletions(-) diff --git a/docker/reset b/docker/reset index 1d12dec910..6dbd9e6319 100755 --- a/docker/reset +++ b/docker/reset @@ -1,7 +1,5 @@ #!/bin/sh -set -e - cd "$(dirname "$0")/.." . docker/env diff --git a/docker/setup b/docker/setup index 072cc81fee..d58693fe7b 100755 --- a/docker/setup +++ b/docker/setup @@ -1,7 +1,5 @@ #!/bin/sh -set -e - cd "$(dirname "$0")/.." if [ -z "$DOCKER" ]; then From 7a749465c1b6f8589e96d4ed69f792103a240eba Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 19 Oct 2023 10:56:57 +0100 Subject: [PATCH 3/5] Update Docker scripts Use uppercase variables. --- docker/reset | 6 +++--- docker/setup | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/docker/reset b/docker/reset index 6dbd9e6319..723934a978 100755 --- a/docker/reset +++ b/docker/reset @@ -6,15 +6,15 @@ cd "$(dirname "$0")/.." docker compose down -db_volume="$(basename $(pwd))_postgres" -if docker volume ls | grep $db_volume >/dev/null; then +DB_VOLUME="$(basename $(pwd))_postgres" +if docker volume ls | grep $DB_VOLUME >/dev/null; then while true do read -r -p 'Do you also want to reset your development database? ' choice case "$choice" in n|N) break;; y|Y) - docker volume rm $db_volume >/dev/null + docker volume rm $DB_VOLUME >/dev/null data_reset=true break;; *) echo 'Response not valid';; diff --git a/docker/setup b/docker/setup index d58693fe7b..57823f74d2 100755 --- a/docker/setup +++ b/docker/setup @@ -20,9 +20,9 @@ bundle check >/dev/null || bundle install success_msg 'done' if [ -L config/general.yml ]; then - theme=$(basename -s .yml $(readlink config/general.yml) | sed 's/^general-//') - notice_msg "Switching to $theme..." - bundle exec script/switch-theme.rb $theme 2>/dev/null + THEME=$(basename -s .yml $(readlink config/general.yml) | sed 's/^general-//') + notice_msg "Switching to $THEME..." + bundle exec script/switch-theme.rb $THEME 2>/dev/null bin/rails assets:clean >/dev/null success_msg 'done' fi From 16f7446aab97c7231b3fa28cc3bb898481e6b6a5 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 19 Oct 2023 10:57:35 +0100 Subject: [PATCH 4/5] Update Docker scripts Use named `--reset-data` argument. This is assumed true when data can't be loaded from the database, otherwise it is false. --- docker/reset | 5 +++-- docker/setup | 34 ++++++++++++++++++++++++++++------ 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/docker/reset b/docker/reset index 723934a978..c821ebba2c 100755 --- a/docker/reset +++ b/docker/reset @@ -6,6 +6,7 @@ cd "$(dirname "$0")/.." docker compose down +ARGS=() DB_VOLUME="$(basename $(pwd))_postgres" if docker volume ls | grep $DB_VOLUME >/dev/null; then while true @@ -15,7 +16,7 @@ if docker volume ls | grep $DB_VOLUME >/dev/null; then n|N) break;; y|Y) docker volume rm $DB_VOLUME >/dev/null - data_reset=true + ARGS+=("--reset_data") break;; *) echo 'Response not valid';; esac @@ -24,4 +25,4 @@ fi docker compose build --pull -./docker/setup $data_reset +./docker/setup "${ARGS[@]}" diff --git a/docker/setup b/docker/setup index 57823f74d2..47d5379ac5 100755 --- a/docker/setup +++ b/docker/setup @@ -13,8 +13,6 @@ error_msg() { printf "\033[31m%s\033[0m\n" "$*"; } notice_msg() { printf "\033[33m%s\033[0m " "$*"; } success_msg() { printf "\033[32m%s\033[0m\n" "$*"; } -data_reset="${1:-false}" - notice_msg 'Installing Ruby gems...' bundle check >/dev/null || bundle install success_msg 'done' @@ -27,19 +25,43 @@ if [ -L config/general.yml ]; then success_msg 'done' fi -if $data_reset; then - notice_msg 'Migrating development and test databases...' +# check to see if the database has ever been seeded, if not then set the +# RESET_DATA_FLAG regardless of arguments passed to the script +bin/rails runner 'User.find(1)' 2>/dev/null +RESET_DATA_FLAG=$? +for arg in "$@"; do + case $arg in + --reset-data) + RESET_DATA_FLAG=1 + shift + ;; + *);; + esac +done + +notice_msg 'Migrating development and test databases...' +if [ $RESET_DATA_FLAG -eq 1 ]; then bin/rails db:migrate db:seed >/dev/null bin/rails db:migrate RAILS_ENV=test >/dev/null success_msg 'done' +else + error_msg 'skipped' +fi - notice_msg 'Loading sample data...' +notice_msg 'Loading sample data...' +if [ $RESET_DATA_FLAG -eq 1 ]; then bundle exec script/load-sample-data > /dev/null success_msg 'done' +else + error_msg 'skipped' +fi - notice_msg 'Rebuilding Xapian index...' +notice_msg 'Rebuilding Xapian index...' +if [ $RESET_DATA_FLAG -eq 1 ]; then bundle exec script/destroy-and-rebuild-xapian-index > /dev/null success_msg 'done' +else + error_msg 'skipped' fi success_msg 'Setup finished' From 1eba1082e293f508976468e3ad7ce2b75fa8ed46 Mon Sep 17 00:00:00 2001 From: Graeme Porteous Date: Thu, 19 Oct 2023 11:19:43 +0100 Subject: [PATCH 5/5] Update Docker scripts Remove external requests on setup. Eventually this will be removed when we remove external requests from core but for now ensure a fresh Docker environment works. Fixes #7823 --- docker/setup | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docker/setup b/docker/setup index 47d5379ac5..48846199ff 100755 --- a/docker/setup +++ b/docker/setup @@ -56,6 +56,14 @@ else error_msg 'skipped' fi +notice_msg 'Removing external requests...' +if [ $RESET_DATA_FLAG -eq 1 ]; then + bin/rails runner 'InfoRequest.external.destroy_all' + success_msg 'done' +else + error_msg 'skipped' +fi + notice_msg 'Rebuilding Xapian index...' if [ $RESET_DATA_FLAG -eq 1 ]; then bundle exec script/destroy-and-rebuild-xapian-index > /dev/null