From 9a1d45c54034ec2f558d0df67973b51c1beb0043 Mon Sep 17 00:00:00 2001 From: Luca Palmieri <20745048+LukeMathWalker@users.noreply.github.com> Date: Fri, 30 Aug 2024 11:28:12 +0200 Subject: [PATCH] Don't require the user to have psql installed --- .github/workflows/general.yml | 6 ------ scripts/init_db.sh | 26 ++++++++++++++------------ 2 files changed, 14 insertions(+), 18 deletions(-) diff --git a/.github/workflows/general.yml b/.github/workflows/general.yml index e2cb5ce..36d8e4d 100644 --- a/.github/workflows/general.yml +++ b/.github/workflows/general.yml @@ -58,8 +58,6 @@ jobs: # The --locked flag can be used to force Cargo to use the packaged Cargo.lock file if it is available. # This may be useful for ensuring reproducible builds, to use the exact same set of dependencies that were available when the package was published. # It may also be useful if a newer version of a dependency is published that no longer builds on your system, or has other problems - - name: Install postgresql-client - run: sudo apt-get update && sudo apt-get install postgresql-client -y - name: Migrate database run: | sudo apt-get install libpq-dev -y @@ -107,8 +105,6 @@ jobs: --features ${{ env.SQLX_FEATURES }} --no-default-features --locked - - name: Install postgresql-client - run: sudo apt-get update && sudo apt-get install postgresql-client -y - name: Migrate database run: | sudo apt-get install libpq-dev -y @@ -132,8 +128,6 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - uses: dtolnay/rust-toolchain@stable - - name: Install libpq - run: sudo apt-get update && sudo apt-get install postgresql-client -y - uses: Swatinem/rust-cache@v2 with: key: sqlx-${{ env.SQLX_VERSION }} diff --git a/scripts/init_db.sh b/scripts/init_db.sh index 07a76d5..8c91502 100755 --- a/scripts/init_db.sh +++ b/scripts/init_db.sh @@ -2,11 +2,6 @@ set -x set -eo pipefail -if ! [ -x "$(command -v psql)" ]; then - echo >&2 "Error: psql is not installed." - exit 1 -fi - if ! [ -x "$(command -v sqlx)" ]; then echo >&2 "Error: sqlx is not installed." echo >&2 "Use:" @@ -36,24 +31,31 @@ then echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}" exit 1 fi + CONTAINER_NAME="postgres_$(date '+%s')" # Launch postgres using Docker docker run \ -e POSTGRES_USER=${DB_USER} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \ -e POSTGRES_DB=${DB_NAME} \ + --health-cmd="pg_isready -U ${DB_USER} || exit 1" \ + --health-interval=1s \ + --health-timeout=5s \ + --health-retries=5 \ -p "${DB_PORT}":5432 \ -d \ - --name "postgres_$(date '+%s')" \ + --name "${CONTAINER_NAME}" \ postgres -N 1000 # ^ Increased maximum number of connections for testing purposes + + until [ \ + "$(docker inspect -f "{{.State.Health.Status}}" ${CONTAINER_NAME})" == \ + "healthy" \ + ]; do + >&2 echo "Postgres is still unavailable - sleeping" + sleep 1 + done fi -# Keep pinging Postgres until it's ready to accept commands -until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do - >&2 echo "Postgres is still unavailable - sleeping" - sleep 1 -done - >&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}