# shellcheck shell=bash [[ -n "${BOLT_DOCKER_LOADED:-}" ]] && return readonly BOLT_DOCKER_LOADED=1 source "$(dirname "${BASH_SOURCE[0]}")/log.sh" readonly BOLT_POSTGRES_DB="${POSTGRES_DB:-test_db}" readonly BOLT_POSTGRES_USER="${POSTGRES_USER:-test_user}" readonly BOLT_POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-testpassword}" readonly BOLT_MYSQL_DATABASE="${MYSQL_DATABASE:-test_db}" readonly BOLT_MYSQL_USER="${MYSQL_USER:-test_user}" readonly BOLT_MYSQL_PASSWORD="${MYSQL_PASSWORD:-testpassword}" readonly BOLT_MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-testpassword}" readonly BOLT_MSSQL_SA_PASSWORD="${MSSQL_SA_PASSWORD:-TestPass123!}" # Generate database DSN string for a given database type # # DESCRIPTION: # Creates a connection string using the same credentials as the Docker containers. # This ensures perfect synchronization between container setup and Go test configuration. # # ARGUMENTS: # $1: Database type (postgresql, mysql, mssql, sqlite3) # # OUTPUTS: # Prints the DSN string to stdout # # RETURN: # 0 - DSN generated successfully # 1 - Unknown database type # docker_generate_dsn() { local db_type="$1" case "$db_type" in "postgresql") echo "postgresql://${BOLT_POSTGRES_USER}:${BOLT_POSTGRES_PASSWORD}@localhost:5432/${BOLT_POSTGRES_DB}?sslmode=disable" ;; "mysql") echo "${BOLT_MYSQL_USER}:${BOLT_MYSQL_PASSWORD}@tcp(localhost:3306)/${BOLT_MYSQL_DATABASE}?parseTime=true" ;; "mssql") local url_encoded_password url_encoded_password=$(printf %s "$BOLT_MSSQL_SA_PASSWORD" | jq --slurp --raw-input --raw-output @uri) echo "sqlserver://sa:${url_encoded_password}@localhost:1433?database=master" ;; "sqlite3") echo "/tmp/bolt_test.db" ;; *) log_error "Unknown database type: $db_type" return 1 ;; esac } # Start a PostgreSQL database container # # ARGUMENTS: # $1: Container name (default: bolt_postgresql_db) # docker_start_postgresql() { local container_name="${1:-bolt_postgresql_db}" log_info "Starting PostgreSQL container '$container_name'..." docker run --detach \ --name "$container_name" \ --publish 5432:5432 \ --health-cmd "pg_isready -U postgres" \ --health-interval 10s \ --health-timeout 5s \ --health-retries 5 \ --env POSTGRES_DB="$BOLT_POSTGRES_DB" \ --env POSTGRES_USER="$BOLT_POSTGRES_USER" \ --env POSTGRES_PASSWORD="$BOLT_POSTGRES_PASSWORD" \ postgres:18-alpine } # Start a MySQL database container # # ARGUMENTS: # $1: Container name (default: bolt_mysql_db) # docker_start_mysql() { local container_name="${1:-bolt_mysql_db}" log_info "Starting MySQL container '$container_name'..." docker run --detach \ --name "$container_name" \ --publish 3306:3306 \ --health-cmd "mysqladmin ping -h localhost" \ --health-interval 10s \ --health-timeout 5s \ --health-retries 5 \ --env MYSQL_DATABASE="$BOLT_MYSQL_DATABASE" \ --env MYSQL_USER="$BOLT_MYSQL_USER" \ --env MYSQL_PASSWORD="$BOLT_MYSQL_PASSWORD" \ --env MYSQL_ROOT_PASSWORD="$BOLT_MYSQL_ROOT_PASSWORD" \ mysql:9.4 } # Start a MSSQL database container # # ARGUMENTS: # $1: Container name (default: bolt_mssql_db) # docker_start_mssql() { local container_name="${1:-bolt_mssql_db}" log_info "Starting MSSQL container '$container_name'..." docker run --detach \ --name "$container_name" \ --publish 1433:1433 \ --health-cmd "/opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P '$BOLT_MSSQL_SA_PASSWORD' -Q 'SELECT 1' -C || exit 1" \ --health-interval 10s \ --health-timeout 3s \ --health-start-period 10s \ --health-retries 10 \ --env ACCEPT_EULA=Y \ --env MSSQL_SA_PASSWORD="$BOLT_MSSQL_SA_PASSWORD" \ mcr.microsoft.com/mssql/server:2022-latest } # Stop and remove a database container # # ARGUMENTS: # $1: Container name # docker_stop_container() { local container_name="$1" log_info "Stopping and removing container '$container_name'..." docker stop "$container_name" >/dev/null 2>&1 || true docker rm "$container_name" >/dev/null 2>&1 || true } # Wait for a container to be healthy # # ARGUMENTS: # $1: Container name # $2: Timeout in seconds (default: 60) # # RETURN: # 0 - Container is healthy # 1 - Container not found, unhealthy, or timeout # docker_wait_for_healthy_container() { local container_name="$1" local timeout="${2:-60}" local elapsed=0 log_info "Waiting for container '$container_name' to be healthy..." while [[ $elapsed -lt $timeout ]]; do local status status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found") case "$status" in "healthy") log_success "Container '$container_name' is healthy!" return 0 ;; "not_found") log_error "Error: Container '$container_name' not found" return 1 ;; "unhealthy") log_error "Error: Container '$container_name' is unhealthy" return 1 ;; *) log_progress sleep 1 ((elapsed++)) ;; esac done log_progress_end log_error "Timeout: Container '$container_name' did not become healthy within ${timeout} seconds" log_info "$(docker ps -a)" return 1 }