mirror of
https://github.com/LukeMathWalker/zero-to-production.git
synced 2024-12-18 14:06:37 +00:00
65 lines
2.3 KiB
Bash
Executable file
65 lines
2.3 KiB
Bash
Executable file
#!/usr/bin/env bash
|
|
set -x
|
|
set -eo pipefail
|
|
|
|
if ! [ -x "$(command -v sqlx)" ]; then
|
|
echo >&2 "Error: sqlx is not installed."
|
|
echo >&2 "Use:"
|
|
echo >&2 " cargo install --version='~0.8' sqlx-cli --no-default-features --features rustls,postgres"
|
|
echo >&2 "to install it."
|
|
exit 1
|
|
fi
|
|
|
|
# Check if a custom user has been set, otherwise default to 'postgres'
|
|
DB_USER="${POSTGRES_USER:=postgres}"
|
|
# Check if a custom password has been set, otherwise default to 'password'
|
|
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
|
# Check if a custom database name has been set, otherwise default to 'newsletter'
|
|
DB_NAME="${POSTGRES_DB:=newsletter}"
|
|
# Check if a custom port has been set, otherwise default to '5432'
|
|
DB_PORT="${POSTGRES_PORT:=5432}"
|
|
# Check if a custom host has been set, otherwise default to 'localhost'
|
|
DB_HOST="${POSTGRES_HOST:=localhost}"
|
|
|
|
# Allow to skip Docker if a dockerized Postgres database is already running
|
|
if [[ -z "${SKIP_DOCKER}" ]]
|
|
then
|
|
# 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
|
|
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 "${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
|
|
|
|
>&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}
|
|
sqlx database create
|
|
sqlx migrate run
|
|
|
|
>&2 echo "Postgres has been migrated, ready to go!"
|