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 c027740dad
commit 9a1d45c540
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.
# 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 }}

View file

@ -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}