#!/bin/sh
set -e

echo "Waiting for database..."
until php -r "new PDO('mysql:host='.getenv('DB_HOST').';port='.(getenv('DB_PORT')?:3306), getenv('DB_USERNAME'), getenv('DB_PASSWORD'));" 2>/dev/null; do
    echo "DB not ready, retrying in 2s..."
    sleep 2
done

echo "Running database migrations..."
php artisan migrate --force --no-interaction

exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
