gcp-hockey-results/motm_app/run_production_migration.sh

103 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
# Quick script to run the voting deadline migration on production Kubernetes
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}MOTM App - Production Migration${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
# Default namespace
NAMESPACE="${1:-motm-app}"
echo -e "${YELLOW}Using namespace: ${NAMESPACE}${NC}"
echo ""
# Step 1: Find the pod
echo -e "${YELLOW}Step 1: Finding production pod...${NC}"
POD_NAME=$(kubectl get pods -n "$NAMESPACE" -l app.kubernetes.io/name=motm-app -o jsonpath='{.items[0].metadata.name}' 2>/dev/null || echo "")
if [ -z "$POD_NAME" ]; then
echo -e "${RED}✗ Error: Could not find motm-app pod in namespace '$NAMESPACE'${NC}"
echo ""
echo "Available namespaces:"
kubectl get namespaces
echo ""
echo "Usage: $0 [namespace]"
echo "Example: $0 motm-app"
exit 1
fi
echo -e "${GREEN}✓ Found pod: ${POD_NAME}${NC}"
echo ""
# Step 2: Check if migration script exists in pod
echo -e "${YELLOW}Step 2: Checking migration script...${NC}"
if kubectl exec -n "$NAMESPACE" "$POD_NAME" -- test -f /app/add_voting_deadline.py 2>/dev/null; then
echo -e "${GREEN}✓ Migration script found${NC}"
else
echo -e "${RED}✗ Error: Migration script not found in pod${NC}"
echo "The add_voting_deadline.py script needs to be in the Docker image."
echo "Please rebuild and redeploy the application."
exit 1
fi
echo ""
# Step 3: Run the migration
echo -e "${YELLOW}Step 3: Running migration...${NC}"
if kubectl exec -n "$NAMESPACE" "$POD_NAME" -- python /app/add_voting_deadline.py; then
echo -e "${GREEN}✓ Migration completed successfully!${NC}"
else
echo -e "${RED}✗ Migration failed!${NC}"
exit 1
fi
echo ""
# Step 4: Verify the migration
echo -e "${YELLOW}Step 4: Verifying migration...${NC}"
VERIFY_CMD="from db_config import db_config; from sqlalchemy import text, inspect; engine = db_config.engine; inspector = inspect(engine); columns = inspector.get_columns('admin_settings'); voting_deadline_exists = any(col['name'] == 'votingdeadline' for col in columns); print('✓ votingdeadline column exists' if voting_deadline_exists else '✗ votingdeadline column missing')"
if kubectl exec -n "$NAMESPACE" "$POD_NAME" -- python -c "$VERIFY_CMD" 2>&1 | grep -q "✓"; then
echo -e "${GREEN}✓ Verification successful!${NC}"
else
echo -e "${RED}✗ Verification failed!${NC}"
echo "The column may not have been created properly."
exit 1
fi
echo ""
# Step 5: Restart the pod to clear cached connections
echo -e "${YELLOW}Step 5: Restarting pod to clear cached connections...${NC}"
kubectl rollout restart deployment/motm-app -n "$NAMESPACE" 2>/dev/null || \
kubectl delete pod "$POD_NAME" -n "$NAMESPACE"
echo -e "${GREEN}✓ Pod restart initiated${NC}"
echo ""
# Wait for pod to be ready
echo -e "${YELLOW}Waiting for pod to be ready...${NC}"
kubectl wait --for=condition=ready pod -l app.kubernetes.io/name=motm-app -n "$NAMESPACE" --timeout=120s
echo ""
# Summary
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN}Migration Complete!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo "Next steps:"
echo "1. Visit https://motm.ervine.cloud/admin/motm"
echo "2. The page should load without SQL errors"
echo "3. You should see the 'Voting Deadline' field"
echo "4. Set a deadline for your next match"
echo ""
echo "To check logs:"
echo " kubectl logs -n $NAMESPACE -l app.kubernetes.io/name=motm-app --tail=50"
echo ""