#!/usr/bin/env bash # Full dev environment reset: stop everything, drop and recreate the DB, # re-run all Liquibase migrations, then restart dev servers. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; NC='\033[0m' info() { echo -e "${GREEN}[apix]${NC} $*"; } warn() { echo -e "${YELLOW}[warn]${NC} $*"; } die() { echo -e "${RED}[fail]${NC} $*" >&2; exit 1; } warn "This will DROP and recreate the local 'apix' database." read -rp "Continue? [y/N] " confirm [[ "${confirm,,}" == "y" ]] || { echo "Aborted."; exit 0; } # Stop everything info "Stopping dev servers" "$SCRIPT_DIR/stop.sh" # Load .env cd "$PROJECT_ROOT" if [[ ! -f .env ]]; then die ".env not found — run ./scripts/setup-dev.sh first." fi set -a # shellcheck disable=SC1091 source .env set +a DB_USER="${APIX_DB_USER:-apix}" DB_PASS="${APIX_DB_PASSWORD:-apix}" DB_NAME="${APIX_DB_NAME:-apix}" DB_PORT="${APIX_DB_PORT:-5432}" # Remove and recreate the container (fastest way to wipe the DB on local dev) info "Removing apix-postgres container" docker rm -f apix-postgres 2>/dev/null || true info "Starting fresh PostgreSQL container" docker run -d \ --name apix-postgres \ --restart unless-stopped \ -e POSTGRES_USER="$DB_USER" \ -e POSTGRES_PASSWORD="$DB_PASS" \ -e POSTGRES_DB="$DB_NAME" \ -p "${DB_PORT}:5432" \ postgres:16-alpine >/dev/null info "Waiting for PostgreSQL…" for i in $(seq 1 30); do if docker exec apix-postgres pg_isready -U "$DB_USER" -q 2>/dev/null; then info "PostgreSQL ready" break fi [[ $i -eq 30 ]] && die "PostgreSQL did not become ready. Check: docker logs apix-postgres" sleep 1 done info "Running Liquibase migrations" mvn -q liquibase:update -pl apix-registry \ -Dliquibase.url="jdbc:postgresql://localhost:${DB_PORT}/${DB_NAME}" \ -Dliquibase.username="$DB_USER" \ -Dliquibase.password="$DB_PASS" info "Migrations applied" info "Reset complete — starting dev servers" "$SCRIPT_DIR/dev.sh"