who_need_help/scripts/restore-drill-env-regression.sh
SimpleTest 00083d17c1
Some checks failed
Quality / full-local-gates (push) Has been cancelled
Preserve JSON values during restore drills
2026-08-28 21:04:21 +03:00

68 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
umask 077
ROOT=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
source_script=${WNH_RESTORE_DRILL_SCRIPT:-"$ROOT/scripts/restore-drill-compose.sh"}
run_dir=$(mktemp -d "$ROOT/output/restore-drill-env-regression.XXXXXX")
fixture="$run_dir/project"
mock_bin="$run_dir/mock-bin"
cleanup() {
status=$?
trap - EXIT HUP INT TERM
find "$run_dir" -xdev -depth -delete 2>/dev/null || true
exit "$status"
}
trap cleanup EXIT HUP INT TERM
install -d -m 700 "$fixture/scripts" "$mock_bin"
install -m 755 "$source_script" "$fixture/scripts/restore-drill-compose.sh"
install -m 755 "$ROOT/scripts/compose.sh" "$fixture/scripts/compose.sh"
install -m 600 /dev/null "$fixture/.env"
printf '%s\n' \
'DEPLOYMENT_TARGET=compose' \
'DEPLOYMENT_ENV=test' \
'COMPOSE_PROJECT_NAME=who_need_help_restore_drill_env_regression' \
'DATABASE_MODE=container' \
'APP_TOPOLOGY=compact' \
'PUBLIC_EDGE_ENABLED=false' \
'EMAIL_DELIVERY_PROVIDER=smtp' \
'SMTP_RELAY=smtp.example.invalid' \
'POSTGRES_DB=who_need_help_test' \
'POSTGRES_USER=who_need_help_test' \
'POSTGRES_PASSWORD=restore-drill-regression' \
'DATABASE_URL=ecto://who_need_help_test:restore-drill-regression@db/who_need_help_test' \
'RATE_LIMIT_POLICIES_JSON={"registration_email":{"limit":4,"window_seconds":3600}}' \
>"$fixture/.env"
dump="$run_dir/test.dump"
printf 'restore drill regression fixture\n' >"$dump"
printf '%s %s\n' "$(sha256sum "$dump" | awk '{print $1}')" \
"$(basename -- "$dump")" >"$dump.sha256"
cat >"$mock_bin/docker" <<'EOF'
#!/bin/sh
set -eu
if [ "${RATE_LIMIT_POLICIES_JSON+x}" = x ]; then
echo "RATE_LIMIT_POLICIES_JSON leaked through shell evaluation of .env." >&2
exit 97
fi
case "$*" in
*"SELECT tablename"*) printf 'example_table\n' ;;
*'SELECT count(*) FROM "example_table";'*) printf '1\n' ;;
*'SELECT PostGIS_Lib_Version();'*) printf '3.6.4\n' ;;
*'SELECT count(*) FROM schema_migrations;'*) printf '29\n' ;;
*'SELECT count(*) FROM pg_database'*) printf '0\n' ;;
esac
EOF
chmod 755 "$mock_bin/docker"
PATH="$mock_bin:$PATH" \
"$fixture/scripts/restore-drill-compose.sh" "$dump" >/dev/null
echo "Restore drill treats .env as data and preserves JSON values for Compose."