Skip to content
This repository has been archived by the owner on Nov 14, 2020. It is now read-only.

Commit

Permalink
Enhance and simplify the wait-postgres-docker script.
Browse files Browse the repository at this point in the history
It tries to connect psql instead of just parsing logs
so we are sure database is ready.
  • Loading branch information
cyrilgdn committed Jun 8, 2020
1 parent c1cecc0 commit 60ef095
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions tests/wait-postgres-docker.sh
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
#!/bin/bash

COMPOSE_FILE=${1:-"docker-compose.yml"}

TIMEOUT=30

if [ ! -e "$COMPOSE_FILE" ]; then
echo "Unable to find docker-compose file: $COMPOSE_FILE"
exit 1
fi
TIMEOUT=${TIMEOUT:-30}
export PGCONNECT_TIMEOUT=1

echo "Waiting for database to be up"
i=0
until docker-compose -f "$COMPOSE_FILE" logs postgres | grep "ready to accept connections" > /dev/null; do
i=$((i + 1))
if [ $i -eq $TIMEOUT ]; then
echo
echo "Timeout while waiting for database to be up"
exit 1

until [ $SECONDS -ge $TIMEOUT ]; do
if psql -c "SELECT 1" > /dev/null 2>&1 ; then
printf '\nDatabase is ready'
exit 0
fi
printf "."
sleep 1
done
echo
printf '\nTimeout while waiting for database to be up'
exit 1

0 comments on commit 60ef095

Please sign in to comment.