#!/bin/bash # Optimized migration script set -euo pipefail log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $*" } # Fetch and process tags log "๐ŸŒ Fetching tags from GitHub" tags=$(curl -s https://api.github.com/repos/bunkerity/bunkerweb/tags | jq -r '.[].name | sub("^v"; "")' | jq -R -s -c 'split("\n")[:-1] | map(select(test("^[1-9]+\\.(5|[6-9]|[1-9][0-9]+)")))' | jq -c 'reverse') current_dir=$(basename "$(pwd)") # Navigate to the root directory if in a subdirectory case "$current_dir" in migration) cd ../.. ;; misc) cd .. ;; esac if [[ ! -f src/VERSION ]]; then log "โŒ src/VERSION file not found" exit 1 fi # Read and validate the current version current_version=$( /dev/null; then log "๐Ÿ”„ Migration scripts for version $tag and database $database already exist" export ONLY_UPDATE=1 fi export DATABASE_URI="$database_uri" # Run the migration script log "๐Ÿฆƒ Running migration script for $tag and $database" if ! docker run --rm \ --network=bw-db \ -v bw-data:/data \ -v bw-db:/db \ -v bw-sqlite:/var/lib/bunkerweb \ -v "$migration_dir":/usr/share/migration/versions \ -e TAG \ -e DATABASE \ -e DATABASE_URI \ -e NEXT_TAG \ -e ONLY_UPDATE \ -e UID="$(id -u)" \ -e GID="$(id -g)" \ local/bw-migration; then log "โŒ Failed to run the migration script" docker compose down -v --remove-orphans find "$db_dir" -type d -name "__pycache__" -exec rm -rf {} + exit 1 fi export TAG="$tag" echo "" done # Clean up Docker stack log "๐Ÿงน Cleaning up Docker stack" docker compose down -v --remove-orphans done log "๐ŸŽ‰ Migration scripts generation completed" # Final cleanup log "๐Ÿ›‘ Stopping and cleaning up any remaining Docker stacks" docker compose down -v --remove-orphans || true find "$db_dir" -type d -name "__pycache__" -exec rm -rf {} + cd "$current_dir" || exit