-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev_env.sh
executable file
·58 lines (46 loc) · 1.7 KB
/
dev_env.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
# This script setups the dev environment needed for testing and compiling the
# application. It is composed of a single instance of PostgreSQL running in a
# Docker container. This script also runs the needed migrations.
# Highly inspired by https://github.com/LukeMathWalker/zero-to-production
# Usage:
# Just run `./scripts/dev_env.sh`
# Additionally you can pass the `SKIP_DOCKER=true` environment variable
# if the Postgres instance is already running.
set -x
set -eo pipefail
if ! [ -x "$(command -v psql)" ]; then
echo >&2 "Error: `psql` is not installed."
exit 1
fi
if ! [ -x "$(command -v sqlx)" ]; then
echo >&2 "Error: `sqlx` is not installed."
echo >&2 "Use:"
echo >&2 " cargo install sqlx-cli --no-default-features --features postgres"
echo >&2 "to install it."
exit 1
fi
DB_USER=${POSTGRES_USER:=postgres}
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
DB_NAME="${POSTGRES_DB:=conduit}"
DB_PORT="${POSTGRES_PORT:=5432}"
# Allow to skip Docker if a dockerized Postgres database is already running
if [[ -z "${SKIP_DOCKER}" ]]
then
docker run \
-e POSTGRES_USER=${DB_USER} \
-e POSTGRES_PASSWORD=${DB_PASSWORD} \
-e POSTGRES_DB=${DB_NAME} \
-p "${DB_PORT}":5432 \
-d postgres:14 \
postgres -N 1000
fi
until PGPASSWORD="${DB_PASSWORD}" psql -h "localhost" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do
>&2 echo "Postgres is still unavailable - sleeping"
sleep 1
done
>&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}
sqlx database create
sqlx migrate run
>&2 echo "Postgres has been migrated, ready to go!"