aboutsummaryrefslogtreecommitdiff
path: root/backend/database/dev/backup/apply-updates.sh
blob: 3d89c436829320932e88b5d75dab0d0e9487ffa3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash

set -euo pipefail

# BeepZone Slop Database Updater
# Applies SQL update scripts from backend/database/dev/ to the configured database.

if ! command -v "$DIALOG" >/dev/null 2>&1; then
  echo "\n[ERROR] 'dialog' is not installed or not in PATH." >&2
  echo "On macOS:   brew install dialog" >&2
  echo "On Debian:  sudo apt install dialog" >&2
  exit 1
fi

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
  if [ $? -eq 0 ]; then
    exit 1
  fi


SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
WORK_DIR="${SCRIPT_DIR}"

# Check for MariaDB/MySQL client - try common brew locations on macOS
MYSQL_CLIENT=""
if command -v mariadb >/dev/null 2>&1; then
  MYSQL_CLIENT="mariadb"
elif command -v mysql >/dev/null 2>&1; then
  MYSQL_CLIENT="mysql"
elif [[ -x /opt/homebrew/opt/mysql-client/bin/mysql ]]; then
  MYSQL_CLIENT="/opt/homebrew/opt/mysql-client/bin/mysql"
  export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH"
elif [[ -x /opt/homebrew/opt/mariadb/bin/mariadb ]]; then
  MYSQL_CLIENT="/opt/homebrew/opt/mariadb/bin/mariadb"
  export PATH="/opt/homebrew/opt/mariadb/bin:$PATH"
elif [[ -x /opt/homebrew/bin/mariadb ]]; then
  MYSQL_CLIENT="/opt/homebrew/bin/mariadb"
  export PATH="/opt/homebrew/bin:$PATH"
elif [[ -x /opt/homebrew/bin/mysql ]]; then
  MYSQL_CLIENT="/opt/homebrew/bin/mysql"
  export PATH="/opt/homebrew/bin:$PATH"
elif [[ -x /usr/local/opt/mysql-client/bin/mysql ]]; then
  MYSQL_CLIENT="/usr/local/opt/mysql-client/bin/mysql"
  export PATH="/usr/local/opt/mysql-client/bin:$PATH"
elif [[ -x /usr/local/bin/mariadb ]]; then
  MYSQL_CLIENT="/usr/local/bin/mariadb"
  export PATH="/usr/local/bin:$PATH"
elif [[ -x /usr/local/bin/mysql ]]; then
  MYSQL_CLIENT="/usr/local/bin/mysql"
  export PATH="/usr/local/bin:$PATH"
else
  echo "[ERROR] MariaDB/MySQL client is required but not found."
  exit 1
fi

ENV_FILE="$SCRIPT_DIR/.env"

# Load existing settings from .env if present
if [[ -f "$ENV_FILE" ]]; then
  source "$ENV_FILE"
else
  echo "[ERROR] .env file not found. Please run beepzone-helper.sh first to configure the environment."
  exit 1
fi

# Set defaults if not loaded from .env (though they should be)
: "${DB_HOST:=127.0.0.1}"
: "${DB_PORT:=3306}"
: "${DB_NAME:=beepzone}"
: "${DB_USER:=beepzone}"
: "${DB_PASS:=beepzone}"

echo "=== BeepZone Database Updater ==="
echo "Target Database: $DB_NAME on $DB_HOST:$DB_PORT"
echo "User: $DB_USER"
echo ""

UPDATES_DIR="$WORK_DIR/backend/database/dev"

if [[ ! -d "$UPDATES_DIR" ]]; then
  echo "[ERROR] Updates directory not found: $UPDATES_DIR"
  exit 1
fi

# Find update scripts
update_scripts=($(find "$UPDATES_DIR" -name "update_003*.sql" | sort))

if [[ ${#update_scripts[@]} -eq 0 ]]; then
  echo "No update scripts found in $UPDATES_DIR."
  exit 0
fi

echo "Found ${#update_scripts[@]} update script(s):"
for script in "${update_scripts[@]}"; do
  echo " - $(basename "$script")"
done
echo ""

read -p "Do you want to apply these updates? (y/N) " -n 1 -r
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  echo "Aborted."
  exit 0
fi

echo ""
for script in "${update_scripts[@]}"; do
  script_name=$(basename "$script")
  echo "Applying $script_name..."
  
  if "$MYSQL_CLIENT" -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" -p"$DB_PASS" "$DB_NAME" < "$script"; then
    echo " [OK] $script_name applied successfully."
  else
    echo " [ERROR] Failed to apply $script_name."
    echo "Stopping further updates."
    exit 1
  fi
done

echo ""
echo "All updates applied successfully."