#!/bin/bash # Database Variable Migration Script # This script helps migrate from MYSQL_* to unified DB_* variable names # # Usage: ./scripts/migrate-db-vars.sh [--dry-run] set -e DRY_RUN=false if [[ "$1" == "--dry-run" ]]; then DRY_RUN=true echo "🔍 DRY RUN MODE - No files will be modified" echo "" fi # Color codes for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color echo -e "${BLUE}================================================${NC}" echo -e "${BLUE} Database Variable Migration Script${NC}" echo -e "${BLUE}================================================${NC}" echo "" echo "This script will standardize database configuration variables:" echo " MYSQL_ROOT_PASSWORD → DB_ROOT_PASSWORD" echo " MYSQL_PASSWORD → DB_PASSWORD" echo " MYSQL_DATABASE → DB_NAME (already standardized)" echo " MYSQL_USER → DB_USER (already standardized)" echo "" # Check if .env file exists if [ -f ".env" ]; then echo -e "${YELLOW}⚠️ Found .env file${NC}" echo "" echo "Current database variables in .env:" grep -E "^(MYSQL_|DB_)" .env 2>/dev/null || echo " (none found)" echo "" if [ "$DRY_RUN" = false ]; then read -p "Do you want to update your .env file? (y/N) " -n 1 -r echo "" if [[ $REPLY =~ ^[Yy]$ ]]; then # Backup .env cp .env .env.backup.$(date +%Y%m%d_%H%M%S) echo -e "${GREEN}✓ Created backup of .env${NC}" # Replace variables if grep -q "^MYSQL_ROOT_PASSWORD=" .env; then sed -i.tmp 's/^MYSQL_ROOT_PASSWORD=/DB_ROOT_PASSWORD=/' .env echo -e "${GREEN}✓ Renamed MYSQL_ROOT_PASSWORD → DB_ROOT_PASSWORD${NC}" fi if grep -q "^MYSQL_PASSWORD=" .env; then sed -i.tmp 's/^MYSQL_PASSWORD=/DB_PASSWORD=/' .env echo -e "${GREEN}✓ Renamed MYSQL_PASSWORD → DB_PASSWORD${NC}" fi # Clean up temp files rm -f .env.tmp echo "" echo -e "${GREEN}✓ .env file updated successfully${NC}" echo "" echo "Updated variables in .env:" grep -E "^DB_" .env 2>/dev/null || echo " (none found)" else echo -e "${YELLOW}⚠️ Skipped .env update${NC}" fi else echo -e "${BLUE}[DRY RUN] Would update .env file${NC}" fi else echo -e "${YELLOW}⚠️ No .env file found (this is OK if using docker-compose)${NC}" fi echo "" echo -e "${BLUE}================================================${NC}" echo -e "${GREEN}✓ Migration script completed${NC}" echo -e "${BLUE}================================================${NC}" echo "" echo "Next steps:" echo "1. Review the changes in docker-compose.yml" echo "2. Update any deployment scripts or CI/CD pipelines" echo "3. Restart your services: docker-compose down && docker-compose up -d" echo "" echo "Variable mapping reference:" echo " DB_ROOT_PASSWORD - MySQL root password" echo " DB_PASSWORD - Application database password" echo " DB_USER - Application database user (default: voxblog)" echo " DB_NAME - Application database name (default: voxblog)" echo " DB_HOST - Database host (default: mysql in docker, localhost otherwise)" echo " DB_PORT - Database port (default: 3306)" echo ""