diff --git a/local-dev-2.sh b/local-dev-2.sh index 0f19a0c..9e0d761 100755 --- a/local-dev-2.sh +++ b/local-dev-2.sh @@ -44,20 +44,23 @@ fi # --- 3. Database Reset --- log_step "Resetting PostgreSQL..." -# Host port 5432 must be free: the container maps 5432:5432, so a squatter -# (e.g. a leftover manual postgres) makes the port bind fail. Under ERR_EXIT -# with hidden output that failure used to kill the script silently. -if command -v ss > /dev/null 2>&1 && ss -tln 2>/dev/null | grep -q ":5432 "; then - log_error "Port 5432 is already in use. The postgres container cannot bind it." - log_error "Something else is listening on 5432:" - ss -tlnp 2>/dev/null | grep ":5432 " - log_error "Stop the process holding 5432 (e.g. a manually-started postgres), then re-run." - exit 1 -fi +# Order matters: `down` runs FIRST so the previous postgres container (which +# owns host port 5432 after a prior successful run) is stopped and its port +# released. Only THEN do we check for a remaining squatter — any process still +# on 5432 after the down must be a non-Docker postgres (e.g. a manually +# started one), which the container cannot bind against. Under ERR_EXIT with +# hidden output the original script died silently on this failure. if ! docker compose down -v postgres > /tmp/opencode/pg-down.log 2>&1; then log_error "Failed to stop old postgres container. See /tmp/opencode/pg-down.log" exit 1 fi +if command -v ss > /dev/null 2>&1 && ss -tln 2>/dev/null | grep -q ":5432 "; then + log_error "Port 5432 is still in use after stopping the postgres container." + log_error "Something else is listening on 5432:" + ss -tlnp 2>/dev/null | grep ":5432 " + log_error "Stop the non-Docker process holding 5432 (e.g. a manually-started postgres), then re-run." + exit 1 +fi if ! docker compose up postgres -d > /tmp/opencode/pg-up.log 2>&1; then log_error "Failed to start postgres container. See /tmp/opencode/pg-up.log" exit 1