diff --git a/test/with_tmp_db b/test/with_tmp_db new file mode 100755 index 000000000..244fd391f --- /dev/null +++ b/test/with_tmp_db @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +set -euo pipefail + +usage() { + cat << EOF + +USAGE: $0 + +Runs the given command with the POSTGREST_TEST_CONNECTION environment variable +set to a temporary database that is ready for running the PostgREST test suite. + +You'll need to have the Postgres binaries 'initdb' and 'pg_ctl' on your PATH. + +Example: + + $0 stack test + +EOF + exit -1 +} + +if [ "$#" -lt 1 ]; then + echo "Please provide a command to be run with the temporary database." + usage +fi + +# All data will be stored in a temporary directory. +tmpdir="$(mktemp -d)" + +trap "rm -rf tmpdir" exit + +export PGDATA="$tmpdir" +export PGHOST="$tmpdir" +export PGUSER=postgrest_test_authenticator +export PGDATABASE=postgres + +# Initialize a database cluster. We try to make it as independent as possible +# from the host by specifying the timezone, locale and encoding. +TZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER" + +# Start the database cluster. Instead of listening on a local port, we will +# listen on a unix domain socket. We will reuse the $PGDATA directory for that +# socket. +pg_ctl start -o "-F -c listen_addresses=\"\" -k $PGDATA" + +stop() { + pg_ctl stop -m i + rm -rf "$tmpdir" +} + +trap stop exit + +# Prepare the database for running the tests. +psql <