|  | 
|  | 1 | +#!/usr/bin/env bash | 
|  | 2 | +set -x | 
|  | 3 | +set -eo pipefail | 
|  | 4 | + | 
|  | 5 | +if ! [ -x "$(command -v psql)" ]; then | 
|  | 6 | +  echo >&2 "Error: psql is not installed." | 
|  | 7 | +  exit 1 | 
|  | 8 | +fi | 
|  | 9 | + | 
|  | 10 | +if ! [ -x "$(command -v sqlx)" ]; then | 
|  | 11 | +  echo >&2 "Error: sqlx is not installed." | 
|  | 12 | +  echo >&2 "Use:" | 
|  | 13 | +  echo >&2 "    cargo install --version='~0.8' sqlx-cli --no-default-features --features rustls,postgres" | 
|  | 14 | +  echo >&2 "to install it." | 
|  | 15 | +  exit 1 | 
|  | 16 | +fi | 
|  | 17 | + | 
|  | 18 | +# Check if a custom user has been set, otherwise default to 'postgres' | 
|  | 19 | +DB_USER="${POSTGRES_USER:=postgres}" | 
|  | 20 | +# Check if a custom password has been set, otherwise default to 'password' | 
|  | 21 | +DB_PASSWORD="${POSTGRES_PASSWORD:=password}" | 
|  | 22 | +# Check if a custom database name has been set, otherwise default to 'bdk_wallet' | 
|  | 23 | +DB_NAME="${POSTGRES_DB:=bdk_wallet}" | 
|  | 24 | +# Check if a custom port has been set, otherwise default to '5432' | 
|  | 25 | +DB_PORT="${POSTGRES_PORT:=5432}" | 
|  | 26 | +# Check if a custom host has been set, otherwise default to 'localhost' | 
|  | 27 | +DB_HOST="${POSTGRES_HOST:=localhost}" | 
|  | 28 | + | 
|  | 29 | +# Allow to skip Docker if a dockerized Postgres database is already running | 
|  | 30 | +# if [[ -z "${SKIP_DOCKER}" ]] | 
|  | 31 | +# then | 
|  | 32 | +#   # if a postgres container is running, print instructions to kill it and exit | 
|  | 33 | +#   RUNNING_POSTGRES_CONTAINER=$(docker ps --filter 'name=postgres' --format '{{.ID}}') | 
|  | 34 | +#   if [[ -n $RUNNING_POSTGRES_CONTAINER ]]; then | 
|  | 35 | +#     echo >&2 "there is a postgres container already running, kill it with" | 
|  | 36 | +#     echo >&2 "    docker kill ${RUNNING_POSTGRES_CONTAINER}" | 
|  | 37 | +#     exit 1 | 
|  | 38 | +#   fi | 
|  | 39 | +#   # Launch postgres using Docker | 
|  | 40 | +#   docker run \ | 
|  | 41 | +#       -e POSTGRES_USER=${DB_USER} \ | 
|  | 42 | +#       -e POSTGRES_PASSWORD=${DB_PASSWORD} \ | 
|  | 43 | +#       -e POSTGRES_DB=${DB_NAME} \ | 
|  | 44 | +#       -p "${DB_PORT}":5432 \ | 
|  | 45 | +#       -d \ | 
|  | 46 | +#       --name "postgres_$(date '+%s')" \ | 
|  | 47 | +#       postgres -N 1000 | 
|  | 48 | +#       # ^ Increased maximum number of connections for testing purposes | 
|  | 49 | +# fi | 
|  | 50 | + | 
|  | 51 | +# Keep pinging Postgres until it's ready to accept commands | 
|  | 52 | +until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do | 
|  | 53 | +  >&2 echo "Postgres is still unavailable - sleeping" | 
|  | 54 | +  sleep 1 | 
|  | 55 | +done | 
|  | 56 | + | 
|  | 57 | +>&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations." | 
|  | 58 | + | 
|  | 59 | +export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME} | 
|  | 60 | +export DATABASE_TEST_URL=$DATABASE_URL | 
|  | 61 | +sqlx database create | 
|  | 62 | +sqlx migrate run --source ./db/migrations | 
|  | 63 | + | 
|  | 64 | +>&2 echo "Completed migration." | 
0 commit comments