#!/bin/bash # IMPORTANT: This script is sourced (not executed) by the webdevops entrypoint. # Never use "exit" — it would kill the parent entrypoint and prevent supervisord from starting. # Use "return" instead to leave this script without affecting the parent. echo ">>> Waiting for database connection..." echo ">>> DB_HOST=${DB_HOST:-NOT SET}" echo ">>> DB_PORT=${DB_PORT:-NOT SET}" echo ">>> DB_DATABASE=${DB_DATABASE:-NOT SET}" echo ">>> DB_USERNAME=${DB_USERNAME:-NOT SET}" echo ">>> DB_PASSWORD is $([ -n "$DB_PASSWORD" ] && echo 'set' || echo 'NOT SET')" if [ -z "$DB_HOST" ] || [ -z "$DB_USERNAME" ]; then echo ">>> ERROR: DB_HOST or DB_USERNAME not set. Skipping migrations." return 0 2>/dev/null || true fi _migrate_max_retries=30 _migrate_count=0 _migrate_done=false while [ $_migrate_count -lt $_migrate_max_retries ]; do if php -r ' try { $host = getenv("DB_HOST"); $port = getenv("DB_PORT") ?: "3306"; $user = getenv("DB_USERNAME"); $pass = getenv("DB_PASSWORD"); new PDO("mysql:host=$host;port=$port", $user, $pass, [PDO::ATTR_TIMEOUT => 3]); exit(0); } catch (Throwable $e) { fwrite(STDERR, "PDO error: " . $e->getMessage() . "\n"); exit(1); } ' 2>&1; then echo ">>> Database is reachable. Running migrations..." if php /app/migrate.php; then echo ">>> Migrations completed successfully." else echo ">>> WARNING: migrate.php exited with code $?" fi _migrate_done=true break fi _migrate_count=$((_migrate_count + 1)) echo ">>> Waiting for database... attempt $_migrate_count/$_migrate_max_retries" sleep 2 done if [ "$_migrate_done" = false ]; then echo ">>> WARNING: Could not connect to database after $_migrate_max_retries attempts. Skipping migrations." fi