|
1 | 1 | #!/usr/bin/env bash |
2 | 2 |
|
3 | 3 | if ! [ -x "$(command -v psql)" ]; then |
4 | | -echo >&2 "Error: psql is not installed." |
5 | | -exit 1 |
| 4 | + echo >&2 "Error: psql is not installed." |
| 5 | + exit 1 |
6 | 6 | fi |
7 | 7 | if ! [ -x "$(command -v sqlx)" ]; then |
8 | | -echo >&2 "Error: sqlx is not installed." |
9 | | -echo >&2 "Use:" |
10 | | -echo >&2 " cargo install --version='~0.7' sqlx-cli \ |
| 8 | + echo >&2 "Error: sqlx is not installed." |
| 9 | + echo >&2 "Use:" |
| 10 | + echo >&2 " cargo install --version='~0.7' sqlx-cli \ |
11 | 11 | --no-default-features --features rustls,postgres" |
12 | | -echo >&2 "to install it." |
| 12 | + echo >&2 "to install it." |
13 | 13 | exit 1 |
14 | 14 | fi |
15 | 15 |
|
16 | 16 | # The rest of the script |
17 | 17 | set -x |
18 | 18 | set -eo pipefail |
19 | 19 |
|
| 20 | +# Default settings |
| 21 | +DB_HOST="${DB_HOST:=localhost}" |
20 | 22 | DB_PORT="${DB_PORT:=5432}" |
| 23 | +DB_USER="${DB_USER:=postgres}" |
| 24 | +DB_PASSWORD="${DB_PASSWORD:=password}" |
| 25 | +DB_NAME="${DB_NAME:=postgres}" |
21 | 26 | SUPERUSER="${SUPERUSER:=postgres}" |
22 | 27 | SUPERUSER_PWD="${SUPERUSER_PWD:=welcome}" |
23 | 28 | APP_USER="${APP_USER:=app}" |
|
63 | 68 | # Grant create db privileges to the app user |
64 | 69 | GRANT_QUERY="ALTER USER ${APP_USER} CREATEDB;" |
65 | 70 | docker exec -it "${CONTAINER_NAME}" psql -U "${SUPERUSER}" -c "${GRANT_QUERY}" |
| 71 | + |
| 72 | + # In this case we're running in docker, so let's use APP_USER for DB_USER later |
| 73 | + DB_USER="${APP_USER}" |
| 74 | + DB_PASSWORD="${APP_USER_PWD}" |
| 75 | + DB_NAME="${APP_DB_NAME}" |
| 76 | +else |
| 77 | + # We're skipping docker, so we need to ensure the DB variables are set |
| 78 | + # In CI, these should match the GitHub Actions postgres service settings |
| 79 | + echo "Using existing PostgreSQL instance at ${DB_HOST}:${DB_PORT}" |
66 | 80 | fi |
67 | 81 |
|
68 | | ->&2 echo "Postgres is up and running on port ${DB_PORT} - running migrations now!" |
| 82 | +# Debug info - print connection details |
| 83 | +echo "Connecting with: host=${DB_HOST}, port=${DB_PORT}, user=${DB_USER}, db=${DB_NAME}" |
| 84 | + |
69 | 85 | # Keep pinging Postgres until it's ready to accept commands |
70 | 86 | export PGPASSWORD="${DB_PASSWORD}" |
71 | 87 | until psql -h "${DB_HOST}" -U "${DB_USER}" -p "${DB_PORT}" -d "postgres" -c '\q'; do |
72 | | ->&2 echo "Postgres is still unavailable - sleeping" |
73 | | -sleep 1 |
| 88 | + >&2 echo "Postgres is still unavailable - sleeping" |
| 89 | + sleep 1 |
74 | 90 | done |
75 | 91 | >&2 echo "Postgres is up and running on port ${DB_PORT}!" |
76 | 92 |
|
|
0 commit comments