From 914ae34f1210ad0363f966e6edd0b24f5444c211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phan=20Sainl=C3=A9ger?= Date: Mon, 2 Feb 2026 17:57:41 +0100 Subject: [PATCH] [IMP] centralize common functions in lib/common.sh Extract shared utility functions into a dedicated library file: - query_postgres_container: Execute SQL queries in postgres container - copy_database: Copy database using pgm - copy_filestore: Copy Odoo filestore directory - exec_python_script_in_odoo_shell: Run Python scripts in Odoo shell Benefits: - Single source of truth for utility functions - Easier maintenance and testing - Consistent behavior across all scripts - Reduced code duplication Also introduces readonly constants DATASTORE_PATH and FILESTORE_SUBPATH to avoid hardcoded paths scattered throughout the codebase. --- lib/common.sh | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++ upgrade.sh | 51 +++--------------------------------------- 2 files changed, 64 insertions(+), 48 deletions(-) create mode 100644 lib/common.sh diff --git a/lib/common.sh b/lib/common.sh new file mode 100644 index 0000000..dfdfcda --- /dev/null +++ b/lib/common.sh @@ -0,0 +1,61 @@ +#!/bin/bash +# +# Common functions for Odoo migration scripts +# Source this file from other scripts: source "$(dirname "$0")/lib/common.sh" +# + +set -euo pipefail + +readonly DATASTORE_PATH="/srv/datastore/data" +readonly FILESTORE_SUBPATH="var/lib/odoo/filestore" + +query_postgres_container() { + local query="$1" + local db_name="$2" + + if [[ -z "$query" ]]; then + return 0 + fi + + local result + if ! result=$(docker exec -u 70 "$POSTGRES_SERVICE_NAME" psql -d "$db_name" -t -A -c "$query"); then + printf "Failed to execute SQL query: %s\n" "$query" >&2 + printf "Error: %s\n" "$result" >&2 + return 1 + fi + echo "$result" +} + +copy_database() { + local from_db="$1" + local to_service="$2" + local to_db="$3" + + docker exec -u 70 "$POSTGRES_SERVICE_NAME" pgm cp -f "$from_db" "${to_db}@${to_service}" +} + +copy_filestore() { + local from_service="$1" + local from_db="$2" + local to_service="$3" + local to_db="$4" + + local src_path="${DATASTORE_PATH}/${from_service}/${FILESTORE_SUBPATH}/${from_db}" + local dst_path="${DATASTORE_PATH}/${to_service}/${FILESTORE_SUBPATH}/${to_db}" + + sudo mkdir -p "$dst_path" + sudo rm -rf "$dst_path" + sudo cp -a "$src_path" "$dst_path" + echo "Filestore ${from_service}/${from_db} copied to ${to_service}/${to_db}." +} + +exec_python_script_in_odoo_shell() { + local service_name="$1" + local db_name="$2" + local python_script="$3" + + compose --debug run "$service_name" shell -d "$db_name" --no-http --stop-after-init < "$python_script" +} + +export DATASTORE_PATH FILESTORE_SUBPATH +export -f query_postgres_container copy_database copy_filestore exec_python_script_in_odoo_shell diff --git a/upgrade.sh b/upgrade.sh index b1f505c..bc94363 100755 --- a/upgrade.sh +++ b/upgrade.sh @@ -1,6 +1,9 @@ #!/bin/bash set -euo pipefail +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "${SCRIPT_DIR}/lib/common.sh" + #################### # USAGE & ARGUMENTS #################### @@ -78,54 +81,6 @@ echo "Postgres service name .... $POSTGRES_SERVICE_NAME" -# Function to launch an SQL request to the postgres container -query_postgres_container(){ - local QUERY="$1" - local DB_NAME="$2" - if [[ -z "$QUERY" ]]; then - return 0 - fi - local result - if ! result=$(docker exec -u 70 "$POSTGRES_SERVICE_NAME" psql -d "$DB_NAME" -t -A -c "$QUERY"); then - printf "Failed to execute SQL query: %s\n" "$query" >&2 - printf "Error: %s\n" "$result" >&2 - exit 1 - fi - echo "$result" -} -export -f query_postgres_container - -# Function to copy the postgres databases -copy_database(){ - local FROM_DB="$1" - local TO_SERVICE="$2" - local TO_DB="$3" - docker exec -u 70 "$POSTGRES_SERVICE_NAME" pgm cp -f "$FROM_DB" "$TO_DB"@"$TO_SERVICE" -} -export -f copy_database - -# Function to copy the filetores -copy_filestore(){ - local FROM_SERVICE="$1" - local FROM_DB="$2" - local TO_SERVICE="$3" - local TO_DB="$4" - sudo mkdir -p /srv/datastore/data/"$TO_SERVICE"/var/lib/odoo/filestore/"$TO_DB" || exit 1 - sudo rm -rf /srv/datastore/data/"$TO_SERVICE"/var/lib/odoo/filestore/"$TO_DB" || exit 1 - sudo cp -a /srv/datastore/data/"$FROM_SERVICE"/var/lib/odoo/filestore/"$FROM_DB" /srv/datastore/data/"$TO_SERVICE"/var/lib/odoo/filestore/"$TO_DB" || exit 1 - echo "Filestore $FROM_SERVICE/$FROM_DB copied." -} -export -f copy_filestore - -# Function to launch python scripts in Odoo Shell -exec_python_script_in_odoo_shell(){ - local SERVICE_NAME="$1" - local DB_NAME="$2" - local PYTHON_SCRIPT="$3" - compose --debug run "$SERVICE_NAME" shell -d "$DB_NAME" --no-http --stop-after-init < "$PYTHON_SCRIPT" -} -export -f exec_python_script_in_odoo_shell - ############################################## # CHECKS ALL NEEDED COMPONENTS ARE AVAILABLE # ##############################################