#!/usr/bin/env bash set -Eeuo pipefail repository_root="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)" cd "$repository_root" # shellcheck source=scripts/lib/compose-job-network.sh source "$repository_root/scripts/lib/compose-job-network.sh" command -v docker >/dev/null || { echo "Docker is required for the PostgreSQL migration integration suite." >&2 exit 1 } command -v pnpm >/dev/null || { echo "pnpm is required for the PostgreSQL migration integration suite." >&2 exit 1 } if ! docker compose version >/dev/null 2>&1; then echo "Docker Compose is required for the PostgreSQL migration integration suite." >&2 exit 1 fi if ! compose_job_network_initialize; then echo "The current Docker job container could not be identified." >&2 exit 1 fi raw_project="ekklesia-migration-${GITHUB_RUN_ID:-local}-${GITHUB_RUN_ATTEMPT:-0}-${GITHUB_JOB:-job}-$$" project="${raw_project//[^a-zA-Z0-9_-]/-}" project="${project,,}" export POSTGRES_DB="ekklesia_migration_test" export POSTGRES_USER="ekklesia_migration_test" export POSTGRES_PASSWORD="temporary-$project-${RANDOM}-${RANDOM}" export POSTGRES_PORT="0" compose=( docker compose --project-name "$project" --env-file .env.example --file infrastructure/compose.yaml ) cleanup() { local exit_status=$? trap - EXIT INT TERM if ! compose_job_network_disconnect; then echo "PostgreSQL migration test network cleanup failed." >&2 if ((exit_status == 0)); then exit_status=1 fi fi if ! "${compose[@]}" down --volumes --remove-orphans --timeout 10 >/dev/null 2>&1; then echo "PostgreSQL migration test infrastructure cleanup failed." >&2 if ((exit_status == 0)); then exit_status=1 fi fi exit "$exit_status" } trap cleanup EXIT trap 'exit 130' INT trap 'exit 143' TERM if ! "${compose[@]}" config --quiet >/dev/null 2>&1; then echo "PostgreSQL migration test infrastructure configuration is invalid." >&2 exit 1 fi if ! "${compose[@]}" up --detach --wait --wait-timeout 120 postgres >/dev/null 2>&1; then echo "Disposable PostgreSQL did not become ready." >&2 exit 1 fi if [[ "$COMPOSE_JOB_NETWORK_RUNNING_IN_CONTAINER" == "true" ]]; then if ! compose_job_network_connect postgres "${compose[@]}"; then echo "The migration test job could not join the isolated PostgreSQL network." >&2 exit 1 fi database_host="postgres" database_port="5432" else if ! postgres_binding="$("${compose[@]}" port postgres 5432 2>/dev/null)"; then echo "The disposable PostgreSQL loopback port could not be determined." >&2 exit 1 fi if [[ ! "$postgres_binding" =~ ^127\.0\.0\.1:([0-9]+)$ ]]; then echo "The disposable PostgreSQL port is not bound safely to loopback." >&2 exit 1 fi database_host="127.0.0.1" database_port="${BASH_REMATCH[1]}" if ((database_port < 1 || database_port > 65535)); then echo "The disposable PostgreSQL loopback port is invalid." >&2 exit 1 fi fi EKKLESIA_MIGRATION_TEST_DATABASE_URL="postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@$database_host:$database_port/$POSTGRES_DB" \ pnpm exec vitest run --config vitest.migrations.integration.config.ts