diff options
| author | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-11 09:06:00 -0400 |
|---|---|---|
| committer | Eugene Triguba <hello@eugenetriguba.com> | 2025-10-11 09:06:00 -0400 |
| commit | 535145fc07aca013c119acd99d30f5ef35286e18 (patch) | |
| tree | 96af1e518d33e92b861f3e84f6dfb7fc785adb7d /scripts/lib/docker.sh | |
| parent | Add update homebrew script (diff) | |
| download | cli-535145fc07aca013c119acd99d30f5ef35286e18.zip | |
Add --keepdb flag to tests script
Diffstat (limited to 'scripts/lib/docker.sh')
| -rw-r--r-- | scripts/lib/docker.sh | 60 |
1 files changed, 60 insertions, 0 deletions
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 +} |