#!/bin/bash # ============================================================ # HOA LedgerIQ - Repeatable Seed Data Script # ============================================================ # Usage: ./db/seed/reseed.sh # # This script will: # 1. Drop the tenant schema (if it exists) # 2. Remove the test users and organization # 3. Re-run the full seed SQL to recreate everything fresh # # Prerequisites: # - PostgreSQL container must be running (docker compose up -d postgres) # - DATABASE_URL or PGHOST/PGUSER etc must be configured # ============================================================ set -e # Configuration DB_HOST="${DB_HOST:-localhost}" DB_PORT="${DB_PORT:-5432}" DB_NAME="${DB_NAME:-hoa_platform}" DB_USER="${DB_USER:-hoa_admin}" DB_PASS="${DB_PASS:-hoa_secure_pass}" SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" SEED_FILE="$SCRIPT_DIR/seed.sql" echo "============================================" echo " HOA LedgerIQ - Reseed Database" echo "============================================" echo "" echo "Host: $DB_HOST:$DB_PORT" echo "Database: $DB_NAME" echo "" # Check if seed file exists if [ ! -f "$SEED_FILE" ]; then echo "ERROR: Seed file not found at $SEED_FILE" exit 1 fi CLEANUP_SQL=$(cat <<'EOSQL' DROP SCHEMA IF EXISTS tenant_sunrise_valley CASCADE; DELETE FROM shared.user_organizations WHERE user_id IN ( SELECT id FROM shared.users WHERE email IN ('admin@sunrisevalley.org', 'viewer@sunrisevalley.org') ); DELETE FROM shared.users WHERE email IN ('admin@sunrisevalley.org', 'viewer@sunrisevalley.org'); DELETE FROM shared.organizations WHERE schema_name = 'tenant_sunrise_valley'; EOSQL ) # Check if we can connect directly export PGPASSWORD="$DB_PASS" if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1" > /dev/null 2>&1; then echo "Step 1: Cleaning existing tenant data..." echo "$CLEANUP_SQL" | psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" echo "Step 2: Running seed SQL..." psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$SEED_FILE" else # Try via docker echo "Direct connection failed. Trying via Docker..." DOCKER_CMD="docker compose exec -T postgres psql -U $DB_USER -d $DB_NAME" echo "Step 1: Cleaning existing tenant data..." echo "$CLEANUP_SQL" | $DOCKER_CMD echo "Step 2: Running seed SQL..." $DOCKER_CMD < "$SEED_FILE" fi echo "" echo "============================================" echo " Reseed complete!" echo "============================================" echo "" echo " Test Accounts:" echo " Admin: admin@sunrisevalley.org / password123" echo " (SuperAdmin + President role)" echo "" echo " Viewer: viewer@sunrisevalley.org / password123" echo " (Homeowner role)" echo "============================================"