From 535145fc07aca013c119acd99d30f5ef35286e18 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 11 Oct 2025 09:06:00 -0400 Subject: Add --keepdb flag to tests script --- scripts/lib/docker.sh | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'scripts/lib/docker.sh') diff --git a/scripts/lib/docker.sh b/scripts/lib/docker.sh index 494ccbf..d2efaf5 100644 --- a/scripts/lib/docker.sh +++ b/scripts/lib/docker.sh @@ -185,3 +185,63 @@ docker_wait_for_healthy_container() { log_info "$(docker ps -a)" return 1 } + +# Ensure a database container is running and healthy +# +# DESCRIPTION: +# Checks if a container is already running and healthy. If keepdb is true +# and container is healthy, reuses it. Otherwise, stops any existing container +# and starts a fresh one. +# +# ARGUMENTS: +# $1: Container name +# $2: Database type (postgresql, mysql, mssql) +# $3: Keep database running (true/false) +# +# RETURN: +# 0 - Container is running and healthy +# 1 - Failed to start or wait for container +# +docker_ensure_container_running() { + local container_name="$1" + local db_type="$2" + local keepdb="$3" + + local container_status + container_status=$(docker inspect -f '{{.State.Health.Status}}' "$container_name" 2>/dev/null || echo "not_found") + + if [[ $keepdb == "true" && $container_status == "healthy" ]]; then + log_info "Reusing existing healthy container '$container_name'" + return 0 + fi + + # Stop existing container if it exists but isn't healthy + if [[ $container_status != "not_found" ]]; then + log_info "Removing existing container '$container_name'..." + docker_stop_container "$container_name" + fi + + log_step "Starting $container_name..." + + case "$db_type" in + "postgresql") + docker_start_postgresql "$container_name" + ;; + "mysql") + docker_start_mysql "$container_name" + ;; + "mssql") + docker_start_mssql "$container_name" + ;; + *) + log_error "Unknown database type: $db_type" + return 1 + ;; + esac + + if ! docker_wait_for_healthy_container "$container_name"; then + log_error "Failed to start $container_name container" + docker_stop_container "$container_name" + return 1 + fi +} -- cgit v1.2.3