2020-08-25 21:19:53 +00:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
set -x
|
|
|
|
set -eo pipefail
|
|
|
|
|
2021-07-05 06:55:57 +00:00
|
|
|
if ! [ -x "$(command -v psql)" ]; then
|
2021-12-28 11:18:38 +00:00
|
|
|
echo >&2 "Error: psql is not installed."
|
2021-07-05 06:55:57 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if ! [ -x "$(command -v sqlx)" ]; then
|
2021-12-28 11:18:38 +00:00
|
|
|
echo >&2 "Error: sqlx is not installed."
|
2021-07-05 06:55:57 +00:00
|
|
|
echo >&2 "Use:"
|
2023-09-14 07:43:32 +00:00
|
|
|
echo >&2 " cargo install --version='~0.7' sqlx-cli --no-default-features --features rustls,postgres"
|
2021-07-05 06:55:57 +00:00
|
|
|
echo >&2 "to install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-08-25 21:19:53 +00:00
|
|
|
# Check if a custom user has been set, otherwise default to 'postgres'
|
2021-04-02 10:50:08 +00:00
|
|
|
DB_USER="${POSTGRES_USER:=postgres}"
|
2020-08-25 21:19:53 +00:00
|
|
|
# Check if a custom password has been set, otherwise default to 'password'
|
|
|
|
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
2022-07-05 16:47:21 +00:00
|
|
|
# Check if a custom database name has been set, otherwise default to 'newsletter'
|
2020-08-25 21:19:53 +00:00
|
|
|
DB_NAME="${POSTGRES_DB:=newsletter}"
|
|
|
|
# Check if a custom port has been set, otherwise default to '5432'
|
|
|
|
DB_PORT="${POSTGRES_PORT:=5432}"
|
2021-04-02 10:50:08 +00:00
|
|
|
# Check if a custom host has been set, otherwise default to 'localhost'
|
|
|
|
DB_HOST="${POSTGRES_HOST:=localhost}"
|
2020-08-25 21:19:53 +00:00
|
|
|
|
|
|
|
# Allow to skip Docker if a dockerized Postgres database is already running
|
|
|
|
if [[ -z "${SKIP_DOCKER}" ]]
|
|
|
|
then
|
2021-04-02 10:50:08 +00:00
|
|
|
# if a postgres container is running, print instructions to kill it and exit
|
|
|
|
RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}')
|
|
|
|
if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then
|
|
|
|
echo >&2 "there is a postgres container already running, kill it with"
|
|
|
|
echo >&2 " docker kill ${RUNNING_POSTGRES_CONTAINER}"
|
|
|
|
exit 1
|
|
|
|
fi
|
2020-08-25 21:19:53 +00:00
|
|
|
# Launch postgres using Docker
|
|
|
|
docker run \
|
|
|
|
-e POSTGRES_USER=${DB_USER} \
|
|
|
|
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
|
|
|
|
-e POSTGRES_DB=${DB_NAME} \
|
|
|
|
-p "${DB_PORT}":5432 \
|
2021-04-28 19:41:08 +00:00
|
|
|
-d \
|
2021-04-02 10:50:08 +00:00
|
|
|
--name "postgres_$(date '+%s')" \
|
2020-08-25 21:19:53 +00:00
|
|
|
postgres -N 1000
|
|
|
|
# ^ Increased maximum number of connections for testing purposes
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Keep pinging Postgres until it's ready to accept commands
|
2021-04-02 10:50:08 +00:00
|
|
|
until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
|
2020-08-25 21:19:53 +00:00
|
|
|
>&2 echo "Postgres is still unavailable - sleeping"
|
|
|
|
sleep 1
|
|
|
|
done
|
|
|
|
|
|
|
|
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
|
|
|
|
|
2023-02-18 16:24:47 +00:00
|
|
|
export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}
|
2020-08-25 21:19:53 +00:00
|
|
|
sqlx database create
|
|
|
|
sqlx migrate run
|
|
|
|
|
|
|
|
>&2 echo "Postgres has been migrated, ready to go!"
|