#!/usr/bin/env bash set -euo pipefail if [[ ${EUID} -ne 0 ]]; then echo "Bitte mit sudo starten: sudo ./scripts/update-migrations-on-x64.sh" >&2 exit 1 fi if [[ "$(uname -s)" != "Linux" || "$(uname -m)" != "x86_64" ]]; then echo "Für diesen x64-Schritt ist ein Linux-x86_64-Rechner erforderlich." >&2 exit 1 fi ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" if [[ ! -f "$ROOT/dist/landwelle-migrate-linux-amd64" ]]; then echo "Migrator fehlt: zuerst ./scripts/build-migrator-x64.sh starten." >&2 exit 1 fi TARGET="${LANDWELLE_DIR:-/opt/landwelle}" SERVICE="${LANDWELLE_SERVICE:-landwelle-radio}" APP_USER="${LANDWELLE_USER:-landwelle}" APP_GROUP="${LANDWELLE_GROUP:-landwelle}" ENV_FILE="${LANDWELLE_ENV_FILE:-/etc/landwelle/landwelle.env}" if ! id "$APP_USER" >/dev/null 2>&1; then echo "Systembenutzer $APP_USER existiert nicht." >&2 exit 1 fi if [[ ! -f "$ENV_FILE" ]]; then echo "Konfiguration fehlt: $ENV_FILE" >&2 exit 1 fi if ! command -v psql >/dev/null 2>&1; then echo "PostgreSQL-Client wird einmalig installiert ..." apt-get update DEBIAN_FRONTEND=noninteractive apt-get install -y postgresql-client fi install -d -o root -g root -m 755 "$TARGET/dist" "$TARGET/scripts" install -d -o "$APP_USER" -g "$APP_GROUP" -m 750 \ "$TARGET/migrations" "$TARGET/migrations/applied" install -o root -g root -m 755 \ "$ROOT/dist/landwelle-migrate-linux-amd64" \ "$TARGET/dist/landwelle-migrate-linux-amd64" install -o root -g root -m 755 \ "$ROOT/scripts/migrate.sh" \ "$TARGET/scripts/migrate.sh" # Bereinigt die beiden kurzzeitig falsch nummerierten Landwelle-Zuhause-Dateien. # 0039 und 0040 sind im produktiven Stand bereits durch andere Fachmigrationen belegt. # Die konsolidierte Zuhause-Navigation verwendet deshalb die bereits eingeführte Nummer 9000. rm -f \ "$ROOT/migrations/0039_landwelle_zuhause_navigation.sql" \ "$ROOT/migrations/0040_landwelle_zuhause_navigation.sql" \ "$TARGET/migrations/0039_landwelle_zuhause_navigation.sql" \ "$TARGET/migrations/0040_landwelle_zuhause_navigation.sql" shopt -s nullglob migration_files=("$ROOT"/migrations/*.sql) if (( ${#migration_files[@]} > 0 )); then for migration in "${migration_files[@]}"; do install -o "$APP_USER" -g "$APP_GROUP" -m 640 \ "$migration" "$TARGET/migrations/$(basename "$migration")" done fi # Der Dienstbenutzer braucht Leserechte auf die Environment-Datei und # Ausführungsrecht auf allen übergeordneten Verzeichnissen, um sie öffnen zu können. ENV_DIR="$(dirname "$ENV_FILE")" install -d -o root -g "$APP_GROUP" -m 750 "$ENV_DIR" chown root:"$APP_GROUP" "$ENV_FILE" chmod 640 "$ENV_FILE" if ! runuser -u "$APP_USER" -- test -r "$ENV_FILE"; then echo "Environment-Datei ist für $APP_USER nicht lesbar: $ENV_FILE" >&2 echo "Berechtigungen:" >&2 namei -l "$ENV_FILE" >&2 || true exit 1 fi if ! grep -q '^DATABASE_URL=' "$ENV_FILE"; then echo "DATABASE_URL fehlt in $ENV_FILE" >&2 exit 1 fi echo "Starte PostgreSQL-Migrationen ..." runuser -u "$APP_USER" -- \ "$TARGET/scripts/migrate.sh" \ --env-file "$ENV_FILE" \ --dir "$TARGET/migrations" \ --archive-dir "$TARGET/migrations/applied" if systemctl list-unit-files "$SERVICE.service" >/dev/null 2>&1; then systemctl restart "$SERVICE" systemctl --no-pager --full status "$SERVICE" || true fi echo echo "Fertig. Erfolgreiche SQL-Dateien liegen unter:" echo " $TARGET/migrations/applied"