33set -eo pipefail
44
55if ! [ -x " $( command -v psql) " ]; then
6- echo >&2 " Error: psql is not installed."
7- exit 1
6+ echo >&2 " Error: psql is not installed."
7+ exit 1
88fi
9-
109if ! [ -x " $( command -v sqlx) " ]; then
11- echo >&2 " Error: sqlx is not installed."
12- echo >&2 " Use:"
13- echo >&2 " cargo install --version='~0.6' sqlx-cli --no-default-features --features rustls, postgres"
14- echo >&2 " to install it."
15- exit 1
10+ echo >&2 " Error: sqlx is not installed."
11+ echo >&2 " Use:"
12+ echo >&2 " cargo install --version=0.5.7 sqlx-cli --no-default-features --features postgres"
13+ echo >&2 " to install it."
14+ exit 1
1615fi
1716
1817# Check if a custom user has been set, otherwise default to 'postgres'
19- DB_USER=" ${POSTGRES_USER:= postgres} "
18+ DB_USER=${POSTGRES_USER:= postgres}
2019# Check if a custom password has been set, otherwise default to 'password'
2120DB_PASSWORD=" ${POSTGRES_PASSWORD:= password} "
2221# Check if a custom database name has been set, otherwise default to 'newsletter'
2322DB_NAME=" ${POSTGRES_DB:= newsletter} "
2423# Check if a custom port has been set, otherwise default to '5432'
2524DB_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-
25+ # stop postgres if it's already running
26+ sudo systemctl stop postgresql
27+ # Launch postgres using Docker
28+ docker run \
29+ -e POSTGRES_USER=${DB_USER} \
30+ -e POSTGRES_PASSWORD=${DB_PASSWORD} \
31+ -e POSTGRES_DB=${DB_NAME} \
32+ -p " ${DB_PORT} " :5432 \
33+ -d postgres \
34+ postgres -N 1000
35+ # ^ Increased maximum number of connections for testing purposes
5136# 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
37+ export PGPASSWORD=" ${DB_PASSWORD} "
38+ until psql -h " localhost" -U " ${DB_USER} " -p " ${DB_PORT} " -d " postgres" -c ' \q' ; do
39+ >&2 echo " Postgres is still unavailable - sleeping"
40+ sleep 1
5541done
5642
57- >&2 echo " Postgres is up and running on port ${DB_PORT} - running migrations now !"
43+ >&2 echo " Postgres is up and running on port ${DB_PORT} !"
5844
59- export DATABASE_URL=postgres://${DB_USER} :${DB_PASSWORD} @${DB_HOST} :${DB_PORT} /${DB_NAME}
45+ export DATABASE_URL=postgres://${DB_USER} :${DB_PASSWORD} @localhost :${DB_PORT} /${DB_NAME}
6046sqlx database create
6147sqlx migrate run
6248
63- >&2 echo " Postgres has been migrated, ready to go!"
49+ >&2 echo " Postgres has been migrated, ready to go!"
0 commit comments