#!/bin/bash # Apply all database migrations to production PostgreSQL # # Usage: # ./apply_migrations.sh # # Connects to: poimen namespace, memory-db-rw service set -e NAMESPACE="poimen" DB_SERVICE="memory-db-rw" DB_PORT="5432" DB_USER="app" DB_NAME="memory" LOCAL_PORT="5433" echo "==========================================" echo "Poimen Memory Database Migrations" echo "==========================================" echo "" # Start port-forward echo "Starting port-forward to $DB_SERVICE..." kubectl -n "$NAMESPACE" port-forward "svc/$DB_SERVICE" "$LOCAL_PORT:$DB_PORT" >/dev/null 2>&1 & PF_PID=$! cleanup() { if [ -n "$PF_PID" ]; then kill $PF_PID 2>/dev/null || true wait $PF_PID 2>/dev/null || true fi } trap cleanup EXIT sleep 2 if ! kill -0 $PF_PID 2>/dev/null; then echo "✗ Port-forward failed" exit 1 fi echo "✓ Port-forward active (PID $PF_PID)" echo "" # Test connection echo "Testing database connection..." if ! PGPASSWORD="$DB_PASSWORD" psql -h localhost -p "$LOCAL_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT version();" >/dev/null 2>&1; then echo "✗ Cannot connect to database" echo " Host: localhost:$LOCAL_PORT" echo " User: $DB_USER" echo " Database: $DB_NAME" exit 1 fi echo "✓ Database connected" echo "" # Get migration files MIGRATION_DIR="crates/mem-store/migrations" if [ ! -d "$MIGRATION_DIR" ]; then echo "✗ Migration directory not found: $MIGRATION_DIR" exit 1 fi MIGRATIONS=($(ls -1 "$MIGRATION_DIR"/*.sql | sort)) if [ ${#MIGRATIONS[@]} -eq 0 ]; then echo "✗ No migrations found in $MIGRATION_DIR" exit 1 fi echo "Found ${#MIGRATIONS[@]} migration(s):" for m in "${MIGRATIONS[@]}"; do echo " - $(basename $m)" done echo "" # Run migrations echo "==========================================" echo "Running Migrations" echo "==========================================" echo "" success=0 failed=0 for migration in "${MIGRATIONS[@]}"; do name=$(basename "$migration") echo -n "▶ $name ... " if PGPASSWORD="$DB_PASSWORD" psql -h localhost -p "$LOCAL_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$migration" >/dev/null 2>&1; then echo "✓" ((success++)) else echo "✗" echo " Error output:" PGPASSWORD="$DB_PASSWORD" psql -h localhost -p "$LOCAL_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$migration" 2>&1 | sed 's/^/ /' ((failed++)) fi done echo "" echo "==========================================" echo "Migration Summary" echo "==========================================" echo " Success: $success" echo " Failed: $failed" echo "" # Verify schema echo "Verifying schema..." echo "" echo "Tables created:" PGPASSWORD="$DB_PASSWORD" psql -h localhost -p "$LOCAL_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT tablename FROM pg_tables WHERE schemaname='public' ORDER BY tablename;" | grep -v "^--" | tail -n+3 echo "" if [ $failed -eq 0 ]; then echo "✓ All migrations applied successfully" exit 0 else echo "✗ Some migrations failed" exit 1 fi