1
0

apply-updates.sh 3.5 KB

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