This commit is contained in:
oliver committed 2026-10-13 07:28:31 -03:00
1 parent 01f4ae8eea
commit ef641e9fe2
3 files changed
+69 -5

No files matched your search

+47 -2
View File
@@ -38,10 +38,39 @@ echo "=================================================="
# Function: Create PostgreSQL user
# -----------------------------
# Run a query as postgres admin. Returns psql's exit status so callers can tell
# "query returned nothing" apart from "could not connect".
pg_admin() {
PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql -h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" -d postgres -tAc "$1"
}
# Wait until Postgres accepts connections (default: up to 60s).
wait_for_postgres() {
local tries="${1:-30}"
local i
for ((i = 1; i <= tries; i++)); do
if pg_admin "SELECT 1;" >/dev/null 2>&1; then
return 0
fi
echo "Postgres not reachable yet ($i/$tries), retrying..."
sleep 2
done
return 1
}
check_and_create_db() {
echo "check and create"
echo "Connecting as $POSTGRES_ADMIN_USER to $POSTGRES_HOST:$POSTGRES_PORT"
# -----------------------------
# Never guess: if Postgres cannot be queried we must NOT assume the DB is
# missing, because a restore wipes the filestore.
# -----------------------------
if ! wait_for_postgres; then
echo "ERROR: cannot connect to Postgres at $POSTGRES_HOST:$POSTGRES_PORT - skipping DB check, NO restore performed"
return 1
fi
# -----------------------------
# Check if user exists
# -----------------------------
@@ -54,10 +83,26 @@ fi
# -----------------------------
# Check if database exists
# -----------------------------
DB_EXISTS=$(PGPASSWORD="$POSTGRES_ADMIN_PASSWORD" psql -h "$POSTGRES_HOST" -U "$POSTGRES_ADMIN_USER" -p "$POSTGRES_PORT" -d postgres -tAc "SELECT 1 FROM pg_database WHERE datname='$UUID';" | grep -q 1 && echo "yes" || echo "no")
DB_RESULT=$(pg_admin "SELECT 1 FROM pg_database WHERE datname='$UUID';")
DB_RC=$?
if [ $DB_RC -ne 0 ]; then
echo "ERROR: DB existence query failed (rc=$DB_RC) - NO restore performed"
return 1
fi
if [ "$DB_RESULT" = "1" ]; then
DB_EXISTS="yes"
else
DB_EXISTS="no"
fi
if [ "$DB_EXISTS" = "no" ]; then
/4server/sbin/ODOO_19/restore $UUID default.zip
FILESTORE_HOST="/4server/data/$UUID/odoo/odoo-web-data/filestore"
if [ -d "$FILESTORE_HOST" ] && [ -n "$(ls -A "$FILESTORE_HOST" 2>/dev/null)" ]; then
echo "ERROR: database $UUID not found but a filestore exists at $FILESTORE_HOST - refusing automatic restore of default.zip (would delete the filestore). Restore manually if intended."
return 1
fi
/4server/sbin/ODOO_19/restore $UUID default.zip
fi
}