1
0

apply-updates.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # BeepZone Database Updater
  4. # Applies SQL update scripts from backend/database/dev/ to the configured database.
  5. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  6. WORK_DIR="${SCRIPT_DIR}"
  7. # Check for MariaDB/MySQL client - try common brew locations on macOS
  8. MYSQL_CLIENT=""
  9. if command -v mariadb >/dev/null 2>&1; then
  10. MYSQL_CLIENT="mariadb"
  11. elif command -v mysql >/dev/null 2>&1; then
  12. MYSQL_CLIENT="mysql"
  13. elif [[ -x /opt/homebrew/opt/mysql-client/bin/mysql ]]; then
  14. MYSQL_CLIENT="/opt/homebrew/opt/mysql-client/bin/mysql"
  15. export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
  16. elif [[ -x /opt/homebrew/opt/mariadb/bin/mariadb ]]; then
  17. MYSQL_CLIENT="/opt/homebrew/opt/mariadb/bin/mariadb"
  18. export PATH="/opt/homebrew/opt/mariadb/bin:$PATH"
  19. elif [[ -x /opt/homebrew/bin/mariadb ]]; then
  20. MYSQL_CLIENT="/opt/homebrew/bin/mariadb"
  21. export PATH="/opt/homebrew/bin:$PATH"
  22. elif [[ -x /opt/homebrew/bin/mysql ]]; then
  23. MYSQL_CLIENT="/opt/homebrew/bin/mysql"
  24. export PATH="/opt/homebrew/bin:$PATH"
  25. elif [[ -x /usr/local/opt/mysql-client/bin/mysql ]]; then
  26. MYSQL_CLIENT="/usr/local/opt/mysql-client/bin/mysql"
  27. export PATH="/usr/local/opt/mysql-client/bin:$PATH"
  28. elif [[ -x /usr/local/bin/mariadb ]]; then
  29. MYSQL_CLIENT="/usr/local/bin/mariadb"
  30. export PATH="/usr/local/bin:$PATH"
  31. elif [[ -x /usr/local/bin/mysql ]]; then
  32. MYSQL_CLIENT="/usr/local/bin/mysql"
  33. export PATH="/usr/local/bin:$PATH"
  34. else
  35. echo "[ERROR] MariaDB/MySQL client is required but not found."
  36. exit 1
  37. fi
  38. ENV_FILE="$SCRIPT_DIR/.env"
  39. # Load existing settings from .env if present
  40. if [[ -f "$ENV_FILE" ]]; then
  41. source "$ENV_FILE"
  42. else
  43. echo "[ERROR] .env file not found. Please run beepzone-helper.sh first to configure the environment."
  44. exit 1
  45. fi
  46. # Set defaults if not loaded from .env (though they should be)
  47. : "${DB_HOST:=127.0.0.1}"
  48. : "${DB_PORT:=3306}"
  49. : "${DB_NAME:=beepzone}"
  50. : "${DB_USER:=beepzone}"
  51. : "${DB_PASS:=beepzone}"
  52. echo "=== BeepZone Database Updater ==="
  53. echo "Target Database: $DB_NAME on $DB_HOST:$DB_PORT"
  54. echo "User: $DB_USER"
  55. echo ""
  56. UPDATES_DIR="$WORK_DIR/backend/database/dev"
  57. if [[ ! -d "$UPDATES_DIR" ]]; then
  58. echo "[ERROR] Updates directory not found: $UPDATES_DIR"
  59. exit 1
  60. fi
  61. # Find update scripts
  62. update_scripts=($(find "$UPDATES_DIR" -name "update_*.sql" | sort))
  63. if [[ ${#update_scripts[@]} -eq 0 ]]; then
  64. echo "No update scripts found in $UPDATES_DIR."
  65. exit 0
  66. fi
  67. echo "Found ${#update_scripts[@]} update script(s):"
  68. for script in "${update_scripts[@]}"; do
  69. echo " - $(basename "$script")"
  70. done
  71. echo ""
  72. read -p "Do you want to apply these updates? (y/N) " -n 1 -r
  73. echo ""
  74. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  75. echo "Aborted."
  76. exit 0
  77. fi
  78. echo ""
  79. for script in "${update_scripts[@]}"; do
  80. script_name=$(basename "$script")
  81. echo "Applying $script_name..."
  82. if "$MYSQL_CLIENT" -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < "$script"; then
  83. echo " [OK] $script_name applied successfully."
  84. else
  85. echo " [ERROR] Failed to apply $script_name."
  86. echo "Stopping further updates."
  87. exit 1
  88. fi
  89. done
  90. echo ""
  91. echo "All updates applied successfully."