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 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:"
|
2024-08-29 12:54:52 +00:00
|
|
|
echo >&2 " cargo install --version='~0.8' sqlx-cli --no-default-features --features rustls,postgres"
|
2021-07-05 06:55:57 +00:00
|
|
|
echo >&2 "to install it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2024-08-31 15:09:08 +00:00
|
|
|
# Check if a custom parameter has been set, otherwise use default values
|
|
|
|
DB_PORT="${DB_PORT:=5432}"
|
|
|
|
SUPERUSER="${SUPERUSER:=postgres}"
|
|
|
|
SUPERUSER_PWD="${SUPERUSER_PWD:=password}"
|
|
|
|
APP_USER="${APP_USER:=app}"
|
|
|
|
APP_USER_PWD="${APP_USER_PWD:=secret}"
|
|
|
|
APP_DB_NAME="${APP_DB_NAME:=newsletter}"
|
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:41:14 +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
|
2024-08-30 09:28:12 +00:00
|
|
|
CONTAINER_NAME="postgres_$(date '+%s')"
|
2020-08-25 21:19:53 +00:00
|
|
|
# Launch postgres using Docker
|
|
|
|
docker run \
|
2024-08-31 15:09:08 +00:00
|
|
|
--env POSTGRES_USER=${SUPERUSER} \
|
|
|
|
--env POSTGRES_PASSWORD=${SUPERUSER_PWD} \
|
|
|
|
--health-cmd="pg_isready -U ${SUPERUSER} || exit 1" \
|
2024-08-30 09:28:12 +00:00
|
|
|
--health-interval=1s \
|
|
|
|
--health-timeout=5s \
|
|
|
|
--health-retries=5 \
|
2024-08-31 15:09:08 +00:00
|
|
|
--publish "${DB_PORT}":5432 \
|
|
|
|
--detach \
|
2024-08-30 09:28:12 +00:00
|
|
|
--name "${CONTAINER_NAME}" \
|
2020-08-25 21:19:53 +00:00
|
|
|
postgres -N 1000
|
|
|
|
# ^ Increased maximum number of connections for testing purposes
|
2024-08-30 09:28:12 +00:00
|
|
|
|
|
|
|
until [ \
|
|
|
|
"$(docker inspect -f "{{.State.Health.Status}}" ${CONTAINER_NAME})" == \
|
|
|
|
"healthy" \
|
|
|
|
]; do
|
|
|
|
>&2 echo "Postgres is still unavailable - sleeping"
|
|
|
|
sleep 1
|
|
|
|
done
|
2024-08-31 15:09:08 +00:00
|
|
|
|
|
|
|
# Create the application user
|
|
|
|
CREATE_QUERY="CREATE USER ${APP_USER} WITH PASSWORD '${APP_USER_PWD}';"
|
|
|
|
docker exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER}" -c "${CREATE_QUERY}"
|
|
|
|
|
|
|
|
# Grant create db privileges to the app user
|
|
|
|
GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;"
|
|
|
|
docker exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER}" -c "${GRANT_QUERY}"
|
2020-08-25 21:19:53 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!"
|
|
|
|
|
2024-08-31 15:09:08 +00:00
|
|
|
# Create the application database
|
|
|
|
DATABASE_URL=postgres://${APP_USER}:${APP_USER_PWD}@localhost:${DB_PORT}/${APP_DB_NAME}
|
|
|
|
export DATABASE_URL
|
2020-08-25 21:19:53 +00:00
|
|
|
sqlx database create
|
|
|
|
sqlx migrate run
|
|
|
|
|
|
|
|
>&2 echo "Postgres has been migrated, ready to go!"
|