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+ 
910if  !  [ -x  " $( command -v sqlx) "   ];  then 
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
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
1516fi 
1617
1718#  Check if a custom user has been set, otherwise default to 'postgres'
18- DB_USER=${POSTGRES_USER:= postgres} 
19+ DB_USER=" ${POSTGRES_USER:= postgres} " 
1920#  Check if a custom password has been set, otherwise default to 'password'
2021DB_PASSWORD=" ${POSTGRES_PASSWORD:= password} " 
2122#  Check if a custom database name has been set, otherwise default to 'newsletter'
2223DB_NAME=" ${POSTGRES_DB:= newsletter} " 
2324#  Check if a custom port has been set, otherwise default to '5432'
2425DB_PORT=" ${POSTGRES_PORT:= 5432} " 
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
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+ 
3651#  Keep pinging Postgres until it's ready to accept commands
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
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
4155done 
4256
43- >&2  echo  " Postgres is up and running on port ${DB_PORT} !" 
57+ >&2  echo  " Postgres is up and running on port ${DB_PORT}  - running migrations now !" 
4458
45- export  DATABASE_URL=postgres://${DB_USER} :${DB_PASSWORD} @localhost :${DB_PORT} /${DB_NAME} 
59+ export  DATABASE_URL=postgres://${DB_USER} :${DB_PASSWORD} @${DB_HOST} :${DB_PORT} /${DB_NAME} 
4660sqlx database create
4761sqlx migrate run
4862
49- >&2  echo  " Postgres has been migrated, ready to go!" 
63+ >&2  echo  " Postgres has been migrated, ready to go!" 
0 commit comments