From f07a654c22e6e471e8bbe23c66b4468dd7fde0de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Sainl=C3=A9ger?= Date: Mon, 2 Feb 2026 21:59:47 +0100 Subject: [PATCH] [IMP] factor out user confirmation prompts into reusable function Add confirm_or_exit() function to lib/common.sh to eliminate duplicated confirmation dialog code in prepare_db.sh. Before: Two 10-line case statements with identical logic After: Two single-line function calls The function provides consistent behavior: - Displays the question with Y/N options - Returns 0 on Y/y (continue execution) - Exits with error on any other input This follows DRY principle and ensures all confirmation prompts behave identically across the codebase. --- lib/common.sh | 17 ++++++++++++++++- prepare_db.sh | 40 +++++----------------------------------- 2 files changed, 21 insertions(+), 36 deletions(-) diff --git a/lib/common.sh b/lib/common.sh index 4329eea..24a46b9 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -28,6 +28,21 @@ log_warn() { printf "[WARN] %s\n" "$*" >&2; } log_error() { printf "[ERROR] %s\n" "$*" >&2; } log_step() { printf "\n===== %s =====\n" "$*"; } +confirm_or_exit() { + local message="$1" + local choice + echo "" + echo "$message" + echo "Y - Yes, continue" + echo "N - No, cancel" + read -r -n 1 -p "Your choice: " choice + echo "" + case "$choice" in + [Yy]) return 0 ;; + *) log_error "Cancelled by user."; exit 1 ;; + esac +} + query_postgres_container() { local query="$1" local db_name="$2" @@ -76,6 +91,6 @@ exec_python_script_in_odoo_shell() { } export DATASTORE_PATH FILESTORE_SUBPATH -export -f log_info log_warn log_error log_step +export -f log_info log_warn log_error log_step confirm_or_exit export -f check_required_commands export -f query_postgres_container copy_database copy_filestore exec_python_script_in_odoo_shell diff --git a/prepare_db.sh b/prepare_db.sh index e0be9f7..2ae01c0 100755 --- a/prepare_db.sh +++ b/prepare_db.sh @@ -59,45 +59,15 @@ EOF echo "Retrieve missing addons..." missing_addons=$(query_postgres_container "$SQL_MISSING_ADDONS" "$DB_NAME") -echo " -==== ADD-ONS CHECK ==== -Installed add-ons not available in final Odoo version: -" +log_step "ADD-ONS CHECK" +echo "Installed add-ons not available in final Odoo version:" echo "$missing_addons" +confirm_or_exit "Do you accept to migrate with these add-ons still installed?" - -echo " -Do you accept to migrate the database with all these add-ons still installed? (Y/N/R)" -echo "Y - Yes, let's go on with the upgrade." -echo "N - No, stop the upgrade" -read -n 1 -p "Your choice: " choice -case "$choice" in - [Yy] ) echo " -Let's go on!";; - [Nn] ) echo " -Upgrade cancelled!"; exit 1;; - * ) echo " -Please answer by Y or N.";; -esac - - -# Check the views PYTHON_SCRIPT=pre_migration_view_checking.py echo "Check views with script $PYTHON_SCRIPT ..." -exec_python_script_in_odoo_shell "$DB_NAME" "$DB_NAME" "$PYTHON_SCRIPT" || exit 1 +exec_python_script_in_odoo_shell "$DB_NAME" "$DB_NAME" "$PYTHON_SCRIPT" -echo " -Do you accept to migrate the database with the current views states? (Y/N/R)" -echo "Y - Yes, let's go on with the upgrade." -echo "N - No, stop the upgrade" -read -n 1 -p "Your choice: " choice -case "$choice" in - [Yy] ) echo " -Upgrade confirmed!";; - [Nn] ) echo " -Upgrade cancelled!"; exit 1;; - * ) echo " -Please answer by Y or N.";; -esac +confirm_or_exit "Do you accept to migrate with the current views state?" echo "Database successfully prepared!"