From 2abf6653ba8ff0032d527fbbd1916650a14f9b0b Mon Sep 17 00:00:00 2001 From: Stephen Adamson Date: Sun, 22 Feb 2026 00:24:03 +0000 Subject: [PATCH] fix: add PostgreSQL startup wait loop before test database setup - PostgreSQL container takes time to fully initialize after docker compose up - Previous fix didn't account for container startup time - Add explicit wait loop (30 second timeout) for PostgreSQL service to be ready - Only create test database AFTER PostgreSQL itself responds to connections - Prevents 'database system is starting up' errors - More robust and handles slower container startup scenarios --- local-dev-2.sh | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/local-dev-2.sh b/local-dev-2.sh index fea9d9a..adc9b15 100755 --- a/local-dev-2.sh +++ b/local-dev-2.sh @@ -53,7 +53,23 @@ log_step "Resetting PostgreSQL..." docker compose down -v postgres > /dev/null 2>&1 docker compose up postgres -d > /dev/null 2>&1 log_success "PostgreSQL reset complete" -sleep 3 + +# --- 3a. Wait for PostgreSQL to be ready --- +log_step "Waiting for PostgreSQL to initialize..." +MAX_STARTUP_ATTEMPTS=30 +STARTUP_ATTEMPT=0 +while [ $STARTUP_ATTEMPT -lt $MAX_STARTUP_ATTEMPTS ]; do + if docker exec postgres psql -U myuser -d mydb -c "SELECT 1;" > /dev/null 2>&1; then + log_success "PostgreSQL is ready" + break + fi + STARTUP_ATTEMPT=$((STARTUP_ATTEMPT+1)) + sleep 1 +done +if [ $STARTUP_ATTEMPT -eq $MAX_STARTUP_ATTEMPTS ]; then + log_error "PostgreSQL failed to start after $MAX_STARTUP_ATTEMPTS attempts" + exit 1 +fi # --- 3b. Create Test Database --- log_step "Setting up test database (crussell_test)..." @@ -64,7 +80,6 @@ sleep 1 log_step "Seeding test database schema..." docker exec -i postgres psql -U myuser -d crussell_test < init-scripts/init-script.sql 2>&1 | head -5 || true log_success "Test database schema seeded" - # --- 3d. Verify test database is ready --- log_step "Verifying test database readiness..." MAX_ATTEMPTS=10 @@ -408,8 +423,6 @@ echo "${C_GREEN}✅ Created $count_future/30 Bookings (Future)${C_RESET}" echo "${C_GREEN}✅ Created $count_past/8 Bookings (Past)${C_RESET}" echo "${C_GREEN}✅ Created $TOTAL/45 Bookings (Total)${C_RESET}" -# 5. Confirm Random Half of Upcoming Bookings -echo -e "\n${C_BLUE}🔒 Confirming Random Upcoming Bookings...${C_RESET}" # 5. Confirm Random Half of Upcoming Bookings echo -e "\n${C_BLUE}🔒 Confirming Random Upcoming Bookings...${C_RESET}" confirmed_count=0