diff --git a/README.md b/README.md index 809356d..55e61de 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ The rest of this section assumes you're in the dev shell. ### Running Postgres -`scripts/with-db $COMMAND` will create a Postgres cluster in a temporary directory with the credentials `user=yore-test dbname=yore-test port=5433`, start the Postgres server, run `$COMMAND` and then stop the Postgres erver. +`scripts/with-db $COMMAND` will create a Postgres cluster in a temporary directory with the credentials `user=yore-test dbname=yore-test port=5433`, start the Postgres server, apply migrations using `dbmate`, run `$COMMAND` and then stop the Postgres server. On Linux, you need to have write permissions vor `/var/run/postgresql` to run it. You can use this to run the tests in an entirely fresh database, e.g.: @@ -24,6 +24,8 @@ You can also use it to run a database for development by choosing a `$COMMAND` t ``` BASE_DIR=./pgdata scripts/with-db psql "dbname=yore-test port=5433 user=yore-test" +# cabal repl with env vars set +BASE_DIR=./pgdata scripts/with-db scripts/dev ``` By setting `$BASE_DIR` you can persist the database for later runs. diff --git a/scripts/dev b/scripts/dev new file mode 100755 index 0000000..39af491 --- /dev/null +++ b/scripts/dev @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +YORE_DB="host=localhost port=${DB_PORT} user=${DB_USER} dbname=${DB_DBNAME}" cabal repl exe:yore diff --git a/scripts/with-db b/scripts/with-db index 383cbab..a7bdf98 100755 --- a/scripts/with-db +++ b/scripts/with-db @@ -5,19 +5,25 @@ if [ -z "$BASE_DIR" ]; then fi export PGDATA="$BASE_DIR/pgdata" +export DB_USER=yore-test +export DB_DBNAME=yore-test +export DB_PORT=5433 + # On error or exit: Stop the DB server stop_db_server() { - pg_ctl stop -o "-p 5433" --wait + pg_ctl stop -o "-p ${DB_PORT}" --wait } trap stop_db_server EXIT if [ ! -s "$PGDATA/PG_VERSION" ]; then - initdb --username=yore-test --auth=trust || true + initdb --username="${DB_USER}" --auth=trust || true DO_CREATE_DB=1 fi pg_ctl start -o "-p 5433" --wait if [ -n "$DO_CREATE_DB" ]; then - createdb -p 5433 yore-test --username=yore-test || true + createdb -p "${DB_PORT}" "${DB_DBNAME}" --username="${DB_USER}" || true fi +DATABASE_URL="postgres://${DB_USER}@localhost:${DB_PORT}/${DB_DBNAME}?sslmode=disable" dbmate up + "$@"