#!/bin/bash # Apply cluster registration migration to Hive database # This script applies the 007_add_cluster_registration.sql migration set -e echo "🚀 Applying Cluster Registration Migration..." # Configuration DB_NAME="hive" DB_USER="postgres" DB_PASSWORD="hive123" MIGRATION_FILE="./migrations/007_add_cluster_registration.sql" # Check if migration file exists if [[ ! -f "$MIGRATION_FILE" ]]; then echo "❌ Migration file not found: $MIGRATION_FILE" exit 1 fi echo "📁 Migration file: $MIGRATION_FILE" # Function to run SQL via Docker run_sql_docker() { local sql_file="$1" echo "🐳 Executing migration via Docker..." # Check if PostgreSQL service is running in Docker swarm if docker service ls | grep -q "hive_postgres"; then echo "✅ PostgreSQL service found in Docker swarm" # Get a running PostgreSQL container CONTAINER_ID=$(docker ps --filter "label=com.docker.swarm.service.name=hive_postgres" --format "{{.ID}}" | head -n1) if [[ -z "$CONTAINER_ID" ]]; then echo "❌ No running PostgreSQL container found" exit 1 fi echo "📦 Using PostgreSQL container: $CONTAINER_ID" # Copy migration file to container and execute docker cp "$sql_file" "$CONTAINER_ID:/tmp/migration.sql" docker exec "$CONTAINER_ID" psql -U "$DB_USER" -d "$DB_NAME" -f /tmp/migration.sql docker exec "$CONTAINER_ID" rm -f /tmp/migration.sql else echo "❌ PostgreSQL service not found in Docker swarm" exit 1 fi } # Function to run SQL locally run_sql_local() { local sql_file="$1" echo "🏠 Executing migration locally..." # Check if psql is available if ! command -v psql &> /dev/null; then echo "❌ psql command not found" exit 1 fi # Try to connect locally PGPASSWORD="$DB_PASSWORD" psql -h localhost -U "$DB_USER" -d "$DB_NAME" -f "$sql_file" } # Try Docker first, then local echo "🔍 Attempting migration..." if run_sql_docker "$MIGRATION_FILE" 2>/dev/null; then echo "✅ Migration applied successfully via Docker!" elif run_sql_local "$MIGRATION_FILE" 2>/dev/null; then echo "✅ Migration applied successfully locally!" else echo "❌ Migration failed via both Docker and local methods" echo "📝 Manual steps:" echo "1. Ensure PostgreSQL is running" echo "2. Check database credentials" echo "3. Run manually: psql -h localhost -U postgres -d hive -f $MIGRATION_FILE" exit 1 fi echo "" echo "🎉 Cluster Registration Migration Complete!" echo "" echo "📋 Summary:" echo " ✅ cluster_tokens table created" echo " ✅ cluster_nodes table created" echo " ✅ node_heartbeats table created" echo " ✅ node_registration_attempts table created" echo " ✅ Indexes and triggers created" echo " ✅ Development tokens inserted" echo "" echo "🔐 Development Tokens:" echo " Dev Token: hive_dev_cluster_token_12345678901234567890123456789012" echo " Prod Token: hive_prod_cluster_token_98765432109876543210987654321098" echo "" echo "⚠️ SECURITY WARNING: Change production tokens before deployment!" echo "" echo "🚀 Next steps:" echo " 1. Implement registration API endpoints (/api/cluster/register)" echo " 2. Add heartbeat API endpoint (/api/cluster/heartbeat)" echo " 3. Update Bzzz clients to use registration system" echo ""