db-init: rename cloned source database to target's instance code (#347 fast-path correctness)

This commit is contained in:
OdooSky Bot
2026-05-02 22:37:23 +03:00
parent 52c91c6833
commit f6cf121966

View File

@@ -69,6 +69,7 @@ spec:
- | - |
set -eu set -eu
DBNAME="{{ .Values.instance.code }}" DBNAME="{{ .Values.instance.code }}"
CLONE_FROM="{{ .Values.instance.cloneFromCode | default "" }}"
echo "── ensuring database $DBNAME exists ──" echo "── ensuring database $DBNAME exists ──"
# Wait for PG to accept connections (max 60s) # Wait for PG to accept connections (max 60s)
for i in $(seq 1 30); do for i in $(seq 1 30); do
@@ -76,6 +77,25 @@ spec:
echo "(waiting for postgres, $i/30)" echo "(waiting for postgres, $i/30)"
sleep 2 sleep 2
done done
# VolumeClone fast-path rename (ADR 0003 phase 4): when
# spawn-env clones a source PG PVC, the data dir carries
# source's database name. Rename it to target's instance
# code BEFORE the existence check below, so the rest of
# the script sees the correct DB name. Idempotent — if
# CLONE_FROM == DBNAME or target already exists, skip.
if [ -n "$CLONE_FROM" ] && [ "$CLONE_FROM" != "$DBNAME" ]; then
if PGPASSWORD="$PASSWORD" psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -tAc \
"SELECT 1 FROM pg_database WHERE datname = '$CLONE_FROM'" | grep -q 1; then
if ! PGPASSWORD="$PASSWORD" psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -tAc \
"SELECT 1 FROM pg_database WHERE datname = '$DBNAME'" | grep -q 1; then
echo "── renaming cloned database $CLONE_FROM → $DBNAME ──"
PGPASSWORD="$PASSWORD" psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres \
-c "ALTER DATABASE \"$CLONE_FROM\" RENAME TO \"$DBNAME\""
else
echo "(both $CLONE_FROM and $DBNAME exist — leaving rename to operator)"
fi
fi
fi
# createdb is idempotent if we wrap with an existence check. # createdb is idempotent if we wrap with an existence check.
if PGPASSWORD="$PASSWORD" psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -tAc \ if PGPASSWORD="$PASSWORD" psql -h "$HOST" -p "$PORT" -U "$USER" -d postgres -tAc \
"SELECT 1 FROM pg_database WHERE datname = '$DBNAME'" | grep -q 1; then "SELECT 1 FROM pg_database WHERE datname = '$DBNAME'" | grep -q 1; then