2023-11-12 14:11:13 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
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:"
|
|
|
|
|
echo >&2 " cargo install --version=0.5.7 sqlx-cli --no-default-features --features postgres"
|
|
|
|
|
echo >&2 "to install it."
|
|
|
|
|
exit 1
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
DB_USER=${POSTGRES_USER:=postgres}
|
|
|
|
|
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
|
|
|
|
DB_NAME="${POSTGRES_DB:=newsletter}"
|
|
|
|
|
DB_PORT="${POSTGRES_PORT:=5432}"
|
|
|
|
|
|
|
|
|
|
# Allow to skip container run if a containerized Postgres database is already running
|
|
|
|
|
if [[ -z "${SKIP_DB_RUN}" ]]
|
|
|
|
|
then
|
|
|
|
|
podman run \
|
|
|
|
|
-e POSTGRES_USER=${DB_USER} \
|
|
|
|
|
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
|
|
|
|
|
-e POSTGRES_DB=${DB_NAME} \
|
|
|
|
|
-p "${DB_PORT}":5432 \
|
2023-12-25 16:56:26 +01:00
|
|
|
-d docker.io/postgres \
|
2023-11-12 14:11:13 +01:00
|
|
|
postgres -N 1000
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -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}@localhost:${DB_PORT}/${DB_NAME}
|
|
|
|
|
sqlx database create
|
|
|
|
|
sqlx migrate run
|
|
|
|
|
|
|
|
|
|
>&2 echo "Postgres has been migrated, ready to go!"
|