| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  | #!/usr/bin/env bash
 | 
					
						
							|  |  |  | set -x | 
					
						
							|  |  |  | set -eo pipefail | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-21 22:02:05 +08:00
										 |  |  | if ! [ -x "$(command -v psql)" ]; then | 
					
						
							|  |  |  |   echo >&2 "Error: `psql` is not installed." | 
					
						
							|  |  |  |   echo >&2 "install using brew: brew install libpq." | 
					
						
							|  |  |  |   echo >&2 "link to /usr/local/bin: brew link --force libpq ail" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if ! [ -x "$(command -v sqlx)" ]; then | 
					
						
							|  |  |  |   echo >&2 "Error: `sqlx` is not installed." | 
					
						
							|  |  |  |   echo >&2 "Use:" | 
					
						
							| 
									
										
										
										
											2021-12-01 17:39:23 +08:00
										 |  |  |   echo >&2 "    cargo install --version=^0.5.7 sqlx-cli --no-default-features --features postgres" | 
					
						
							| 
									
										
										
										
											2021-08-21 22:02:05 +08:00
										 |  |  |   echo >&2 "to install it." | 
					
						
							|  |  |  |   exit 1 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:58:52 +08:00
										 |  |  | DB_USER="${POSTGRES_USER:=postgres}" | 
					
						
							|  |  |  | DB_PASSWORD="${POSTGRES_PASSWORD:=password}" | 
					
						
							| 
									
										
										
										
											2021-12-02 18:55:51 +08:00
										 |  |  | DB_PORT="${POSTGRES_PORT:=5432}" | 
					
						
							| 
									
										
										
										
											2021-12-02 15:58:52 +08:00
										 |  |  | DB_HOST="${POSTGRES_HOST:=localhost}" | 
					
						
							|  |  |  | DB_NAME="${POSTGRES_DB:=flowy}" | 
					
						
							| 
									
										
										
										
											2021-12-01 17:39:23 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 16:44:58 +08:00
										 |  |  | if [[ -z "${SKIP_DOCKER}" ]] | 
					
						
							|  |  |  | then | 
					
						
							|  |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   docker run \
 | 
					
						
							|  |  |  |       -e POSTGRES_USER=${DB_USER} \
 | 
					
						
							|  |  |  |       -e POSTGRES_PASSWORD=${DB_PASSWORD} \
 | 
					
						
							| 
									
										
										
										
											2021-12-01 17:39:23 +08:00
										 |  |  |       -e POSTGRES_DB="${DB_NAME}" \
 | 
					
						
							| 
									
										
										
										
											2021-12-01 16:44:58 +08:00
										 |  |  |       -p "${DB_PORT}":5432 \
 | 
					
						
							| 
									
										
										
										
											2021-12-02 15:58:52 +08:00
										 |  |  |       -d \
 | 
					
						
							|  |  |  |       --name "flowy_postgres_$(date '+%s')" \
 | 
					
						
							| 
									
										
										
										
											2021-12-01 16:44:58 +08:00
										 |  |  |       postgres -N 1000 | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Keep pinging Postgres until it's ready to accept commands | 
					
						
							|  |  |  | until PGPASSWORD="${DB_PASSWORD}" psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do | 
					
						
							| 
									
										
										
										
											2021-12-02 15:58:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  |   >&2 echo "Postgres is still unavailable - sleeping" | 
					
						
							|  |  |  |   sleep 1 | 
					
						
							|  |  |  | done | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-01 16:44:58 +08:00
										 |  |  | >&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@localhost:${DB_PORT}/${DB_NAME} | 
					
						
							| 
									
										
										
										
											2021-08-19 17:26:07 +08:00
										 |  |  | sqlx database create | 
					
						
							| 
									
										
										
										
											2021-12-01 16:44:58 +08:00
										 |  |  | sqlx migrate run | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | >&2 echo "Postgres has been migrated, ready to go!" | 
					
						
							|  |  |  | 
 |