Files
poimen-memory/migrations/run_migrations.sh
T

128 lines
3.0 KiB
Bash
Raw Normal View History

#!/bin/bash
# Database Migration Runner
# Used by K8s Job to apply all migrations before integration tests
#
# Environment variables (from K8s):
# DB_HOST - PostgreSQL host
# DB_PORT - PostgreSQL port
# DB_NAME - Database name
# DB_USER - Database user
# DB_PASSWORD - Database password (from Secret)
set -e
DB_HOST="${DB_HOST:-memory-db-rw.poimen.svc.cluster.local}"
DB_PORT="${DB_PORT:-5432}"
DB_NAME="${DB_NAME:-memory}"
DB_USER="${DB_USER:-app}"
if [ -z "$DB_PASSWORD" ]; then
echo "ERROR: DB_PASSWORD not set"
exit 1
fi
echo "=========================================="
echo "Database Migration Runner"
echo "=========================================="
echo ""
echo "Configuration:"
echo " Host: $DB_HOST:$DB_PORT"
echo " Database: $DB_NAME"
echo " User: $DB_USER"
echo ""
# Export for psql
export PGPASSWORD="$DB_PASSWORD"
# Get migration directory (where this script is)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MIGRATION_DIR="$SCRIPT_DIR"
echo "Migration directory: $MIGRATION_DIR"
echo ""
# Collect all SQL files
MIGRATIONS=($(ls -1 "$MIGRATION_DIR"/*.sql 2>/dev/null | sort))
if [ ${#MIGRATIONS[@]} -eq 0 ]; then
echo "ERROR: No migration files found in $MIGRATION_DIR"
exit 1
fi
echo "Found ${#MIGRATIONS[@]} migration(s):"
for m in "${MIGRATIONS[@]}"; do
echo " - $(basename $m)"
done
echo ""
# Wait for DB to be ready
echo "Waiting for database to be ready..."
for i in {1..30}; do
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" >/dev/null 2>&1; then
echo "✓ Database is ready"
break
fi
if [ $i -eq 30 ]; then
echo "✗ Database not ready after 30 attempts"
exit 1
fi
echo " Attempt $i/30..."
sleep 1
done
echo ""
echo "=========================================="
echo "Running Migrations"
echo "=========================================="
echo ""
SUCCESS=0
FAILED=0
for migration in "${MIGRATIONS[@]}"; do
name=$(basename "$migration")
echo -n "▶ $name ... "
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -f "$migration" >/dev/null 2>&1; then
echo "✓"
((SUCCESS++))
else
echo "✗ FAILED"
echo ""
echo "Error output:"
psql -h "$DB_HOST" -p "$DB_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 ""
if [ $FAILED -eq 0 ]; then
echo "✓ All migrations applied successfully"
echo ""
echo "Verifying schema..."
echo ""
# Verify key tables exist
for table in memory_entity memory_edge ingest_jobs; do
if psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1 FROM information_schema.tables WHERE table_name='$table';" 2>&1 | grep -q "1 row"; then
echo " ✓ Table $table exists"
else
echo " ⚠ Table $table not found"
fi
done
exit 0
else
echo "✗ Some migrations failed"
exit 1
fi