From 5feeabc3ac77d96c6ff466b40cb51030040ad5c9 Mon Sep 17 00:00:00 2001
From: Emanuele Panzeri <thePanz@users.noreply.github.com>
Date: Fri, 22 Jan 2021 17:15:50 +0100
Subject: [PATCH] fix: do not check DB availability if code errors (#110)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* fix: do not check DB availability if code errors

Do not wait for the DB to be available if there are other errors blocking the access to it.

* Update docker-entrypoint.sh

Co-authored-by: Kévin Dunglas <dunglas@gmail.com>
---
 docker/php/docker-entrypoint.sh | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/docker/php/docker-entrypoint.sh b/docker/php/docker-entrypoint.sh
index 20108cc..6d0f406 100755
--- a/docker/php/docker-entrypoint.sh
+++ b/docker/php/docker-entrypoint.sh
@@ -32,14 +32,20 @@ if [ "$1" = 'php-fpm' ] || [ "$1" = 'php' ] || [ "$1" = 'bin/console' ]; then
 	if grep -q ^DATABASE_URL= .env; then
 		echo "Waiting for db to be ready..."
 		ATTEMPTS_LEFT_TO_REACH_DATABASE=60
-		until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || bin/console doctrine:query:sql "SELECT 1" >/dev/null 2>&1; do
+		until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(bin/console doctrine:query:sql "SELECT 1" 2>&1); do
+			if [ $? -eq 255 ]; then
+				# If the Doctrine command exits with 255, an unrecoverable error occurred
+				ATTEMPTS_LEFT_TO_REACH_DATABASE=0
+				break
+			fi
 			sleep 1
 			ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
 			echo "Still waiting for db to be ready... Or maybe the db is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left"
 		done
 
 		if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
-			echo "The db is not up or not reachable"
+			echo "The database is not up or not reachable:"
+			echo "$DATABASE_ERROR"
 			exit 1
 		else
 			echo "The db is now ready and reachable"