mirror of
https://github.com/kristoferssolo/echoes-of-ascension.git
synced 2026-02-04 06:22:03 +00:00
refactor: use postgres
This commit is contained in:
46
scripts/init_db
Executable file
46
scripts/init_db
Executable file
@@ -0,0 +1,46 @@
|
||||
#!/usr/bin/env bash
|
||||
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 rustls,postgres"
|
||||
echo >&2 "to install it."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
DB_USER="${POSTGRES_USER:=postgres}"
|
||||
DB_PASSWORD="${POSTGRES_PASSWORD:=password}"
|
||||
DB_NAME="${POSTGRES_DB:=maze_ascension}"
|
||||
DB_PORT="${POSTGRES_PORT:=5432}"
|
||||
DB_HOST="${POSTGRES_HOST:=localhost}"
|
||||
|
||||
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\
|
||||
postgres -N 1000
|
||||
# Increase max number of connections for testing purposes
|
||||
fi
|
||||
|
||||
# Keep pinging Postgres until it's ready to accept commands
|
||||
export PGPASSWORD="${DB_PASSWORD}"
|
||||
until psql -h "${DB_HOST}" -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 still up and running on port ${DB_PORT} - runing migrations now!"
|
||||
|
||||
DATABASE_URL="postgres://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
|
||||
export DATABASE_URL
|
||||
sqlx database create
|
||||
sqlx migrate run
|
||||
Reference in New Issue
Block a user