#!/bin/bash # Run Database Migrations Script echo -e "\n========================================" echo -e " DATABASE MIGRATIONS" echo -e "========================================\n" # Check if we're in the right directory if [ ! -d "orchestrator" ]; then echo -e "\033[0;31m❌ Error: Must run from project root\033[0m" echo -e " Current directory: $(pwd)" exit 1 fi # Check if .env exists if [ ! -f "orchestrator/.env" ]; then echo -e "\033[0;33m⚠️ orchestrator/.env not found\033[0m" echo -e " Creating from example..." if [ -f "orchestrator/src/config/env.example" ]; then cp orchestrator/src/config/env.example orchestrator/.env echo -e " ✅ Created orchestrator/.env" echo -e " \033[0;33m⚠️ Please update DATABASE_URL in orchestrator/.env\033[0m" else echo -e " \033[0;31m❌ env.example not found\033[0m" exit 1 fi fi # Check DATABASE_URL if ! grep -q "DATABASE_URL=" orchestrator/.env 2>/dev/null; then echo -e "\033[0;33m⚠️ DATABASE_URL not set in orchestrator/.env\033[0m" echo -e " Adding default DATABASE_URL..." echo "" >> orchestrator/.env echo "DATABASE_URL=postgresql://postgres:postgres@localhost:5432/comboflow" >> orchestrator/.env echo "RUN_MIGRATIONS=true" >> orchestrator/.env echo -e " ✅ Added default DATABASE_URL" fi # Check if database is accessible echo -e "\n🔍 Checking database connection..." DATABASE_URL=$(grep "^DATABASE_URL=" orchestrator/.env | cut -d '=' -f2- | tr -d '"' | tr -d "'") if [ -z "$DATABASE_URL" ]; then echo -e "\033[0;31m❌ DATABASE_URL is empty\033[0m" exit 1 fi # Extract connection details for testing if [[ $DATABASE_URL =~ postgresql://([^:]+):([^@]+)@([^:]+):([^/]+)/(.+) ]]; then DB_USER="${BASH_REMATCH[1]}" DB_PASS="${BASH_REMATCH[2]}" DB_HOST="${BASH_REMATCH[3]}" DB_PORT="${BASH_REMATCH[4]}" DB_NAME="${BASH_REMATCH[5]}" echo -e " Host: $DB_HOST" echo -e " Port: $DB_PORT" echo -e " Database: $DB_NAME" # Test connection with psql if available if command -v psql &> /dev/null; then if PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c "SELECT 1;" > /dev/null 2>&1; then echo -e " \033[0;32m✅ Database connection successful\033[0m" else echo -e " \033[0;33m⚠️ Could not connect to database\033[0m" echo -e " Make sure PostgreSQL is running and accessible" fi else echo -e " \033[0;33m⚠️ psql not found, skipping connection test\033[0m" fi else echo -e " \033[0;33m⚠️ Could not parse DATABASE_URL\033[0m" fi # Run migrations echo -e "\n🔄 Running database migrations..." cd orchestrator || exit 1 if [ ! -d "node_modules" ]; then echo -e "\033[0;33m⚠️ node_modules not found. Installing dependencies...\033[0m" npm install fi npm run migrate if [ $? -eq 0 ]; then echo -e "\n\033[0;32m✅ Migrations completed successfully\033[0m" else echo -e "\n\033[0;31m❌ Migrations failed\033[0m" exit 1 fi cd .. echo -e "\n📝 Next steps:" echo -e " 1. Verify health endpoint: http://localhost:8080/health" echo -e " 2. Check database tables: SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';" echo ""