Don't require the user to have psql installed

This commit is contained in:
Luca Palmieri 2024-08-30 11:28:12 +02:00
parent 70432103cd
commit 3f4cff9858
2 changed files with 14 additions and 18 deletions

View file

@ -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. # 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. # 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 # 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 - name: Migrate database
run: | run: |
sudo apt-get install libpq-dev -y sudo apt-get install libpq-dev -y
@ -107,8 +105,6 @@ jobs:
--features ${{ env.SQLX_FEATURES }} --features ${{ env.SQLX_FEATURES }}
--no-default-features --no-default-features
--locked --locked
- name: Install postgresql-client
run: sudo apt-get update && sudo apt-get install postgresql-client -y
- name: Migrate database - name: Migrate database
run: | run: |
sudo apt-get install libpq-dev -y sudo apt-get install libpq-dev -y
@ -132,8 +128,6 @@ jobs:
- name: Checkout repository - name: Checkout repository
uses: actions/checkout@v3 uses: actions/checkout@v3
- uses: dtolnay/rust-toolchain@stable - 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 - uses: Swatinem/rust-cache@v2
with: with:
key: sqlx-${{ env.SQLX_VERSION }} key: sqlx-${{ env.SQLX_VERSION }}

View file

@ -2,11 +2,6 @@
set -x set -x
set -eo pipefail 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 if ! [ -x "$(command -v sqlx)" ]; then
echo >&2 "Error: sqlx is not installed." echo >&2 "Error: sqlx is not installed."
echo >&2 "Use:" echo >&2 "Use:"
@ -36,24 +31,31 @@ then
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}" echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
exit 1 exit 1
fi fi
CONTAINER_NAME="postgres_$(date '+%s')"
# Launch postgres using Docker # Launch postgres using Docker
docker run \ docker run \
-e POSTGRES_USER=${DB_USER} \ -e POSTGRES_USER=${DB_USER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \ -e POSTGRES_PASSWORD=${DB_PASSWORD} \
-e POSTGRES_DB=${DB_NAME} \ -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 \ -p "${DB_PORT}":5432 \
-d \ -d \
--name "postgres_$(date '+%s')" \ --name "${CONTAINER_NAME}" \
postgres -N 1000 postgres -N 1000
# ^ Increased maximum number of connections for testing purposes # ^ 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 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!" >&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} export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}