imp: factorize query_postgres_container() in upgrade.sh

This commit is contained in:
Stéphan Sainléger
2024-11-26 10:08:48 +01:00
parent 53318a1a51
commit 116d00091b
7 changed files with 34 additions and 128 deletions

View File

@@ -21,7 +21,7 @@ FINALE_DB_NAME="ou${FINAL_VERSION}"
FINALE_SERVICE_NAME="${FINALE_DB_NAME}"
# Service postgres name
POSTGRES_SERVICE_NAME="lokavaluto_postgres_1"
export POSTGRES_SERVICE_NAME="lokavaluto_postgres_1"
#############################################
# DISPLAYS ALL INPUTS PARAMETERS
@@ -43,6 +43,25 @@ echo "Finale service name ...... $FINALE_SERVICE_NAME"
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
##############################################
# CHECKS ALL NEEDED COMPONENTS ARE AVAILABLE #
##############################################