#!/usr/bin/env bash set -euo pipefail # ============================================================================= # teardown_all.sh — Tear down migration in reverse order # Runs phase teardown scripts from phase 9 → phase 1 (or a subset). # # Usage: # ./teardown_all.sh # Tear down everything (phases 9 → 1) # ./teardown_all.sh --through=5 # Tear down phases 9 → 5 (leave 1-4) # ./teardown_all.sh --yes # Skip confirmation prompts # ============================================================================= SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" source "${SCRIPT_DIR}/lib/common.sh" # --------------------------------------------------------------------------- # Parse arguments # --------------------------------------------------------------------------- THROUGH=1 AUTO_YES=false for arg in "$@"; do case "$arg" in --through=*) THROUGH="${arg#*=}" if ! [[ "$THROUGH" =~ ^[0-9]+$ ]] || [[ "$THROUGH" -lt 1 ]] || [[ "$THROUGH" -gt 9 ]]; then log_error "--through must be a number between 1 and 9" exit 1 fi ;; --yes|-y) AUTO_YES=true ;; --help|-h) cat <>> Tearing down Phase ${phase_num}..." if [[ "$AUTO_YES" == "true" ]]; then # Pipe 'y' to all prompts in the teardown script if echo "y" | "${SCRIPT_DIR}/${script}"; then PASS=$((PASS + 1)) else log_warn "Phase ${phase_num} teardown had issues (continuing)" FAIL=$((FAIL + 1)) fi else # Run interactively — let user respond to each prompt if "${SCRIPT_DIR}/${script}"; then PASS=$((PASS + 1)) else log_warn "Phase ${phase_num} teardown had issues (continuing)" FAIL=$((FAIL + 1)) fi fi printf '\n' done # --------------------------------------------------------------------------- # Summary # --------------------------------------------------------------------------- printf '\n' log_info "Teardown summary: ${PASS} succeeded, ${FAIL} had issues" if [[ $FAIL -gt 0 ]]; then log_warn "Some teardowns had issues — check logs above" else log_success "All teardowns completed successfully" fi