From ba47a54293c1c9b4aaab37e1effd89411651709c Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Tue, 20 Apr 2021 21:28:07 +0200 Subject: [PATCH] nix(refactor): Make with_tmp_db true checkedShellScript This extends the interface of withTmpDb to allow loading different sets of database fixtures via `postgrest-with-postgresql-xx --fixtures `. --- .../checked-shell-script.nix | 3 +- nix/tools/style.nix | 2 +- nix/tools/withtools.nix | 63 ++++++++++++++++--- test/create_test_db | 5 -- test/fixtures/database.sql | 3 + test/with_tmp_db | 7 +-- 6 files changed, 61 insertions(+), 22 deletions(-) diff --git a/nix/overlays/checked-shell-script/checked-shell-script.nix b/nix/overlays/checked-shell-script/checked-shell-script.nix index 26e526f69..5deb12749 100644 --- a/nix/overlays/checked-shell-script/checked-shell-script.nix +++ b/nix/overlays/checked-shell-script/checked-shell-script.nix @@ -77,9 +77,8 @@ let text = '' #!${bash_5}/bin/bash - set -euo pipefail - source ${argsParser} + set -euo pipefail '' + lib.optionalString redirectTixFiles '' diff --git a/nix/tools/style.nix b/nix/tools/style.nix index 1ea78ffa5..ed354d969 100644 --- a/nix/tools/style.nix +++ b/nix/tools/style.nix @@ -57,7 +57,7 @@ let | xargs ${hlint}/bin/hlint -X QuasiQuotes -X NoPatternSynonyms # Lint bash scripts - ${shellcheck}/bin/shellcheck test/create_test_db test/memory-tests.sh test/with_tmp_db + ${shellcheck}/bin/shellcheck test/create_test_db test/memory-tests.sh ''; in diff --git a/nix/tools/withtools.nix b/nix/tools/withtools.nix index c3ee30e6f..38209b9f1 100644 --- a/nix/tools/withtools.nix +++ b/nix/tools/withtools.nix @@ -6,7 +6,6 @@ , writeTextFile }: let - # Wrap the `test/with_tmp_db` script with the required dependencies from Nix. withTmpDb = { name, postgresql }: checkedShellScript @@ -15,22 +14,70 @@ let docs = "Run the given command in a temporary database with ${name}"; args = [ + "ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from], [test/fixtures/load.sql])" "ARG_POSITIONAL_SINGLE([command], [Command to run])" "ARG_LEFTOVERS([command arguments])" + "ARG_USE_ENV([PGUSER], [postgrest_test_authenticator], [Authenticator PG role])" + "ARG_USE_ENV([PGDATABASE], [postgres], [PG database name])" + "ARG_USE_ENV([PGRST_DB_SCHEMAS], [test], [Schema to expose])" + "ARG_USE_ENV([PGRST_DB_ANON_ROLE], [postgrest_test_anonymous], [Anonymous PG role])" ]; addCommandCompletion = true; inRootDir = true; redirectTixFiles = false; + withTmpDir = true; } '' - # avoid starting multiple layers of with_tmp_db - if test ! -v PGRST_DB_URI; then - export PATH=${postgresql}/bin:"$PATH" - - exec ${../../test/with_tmp_db} "$_arg_command" "''${_arg_leftovers[@]}" - else - "$@" + # avoid starting multiple layers of withTmpDb + if test -v PGRST_DB_URI; then + exec "$@" fi + + export PATH=${postgresql}/bin:"$PATH" + setuplog="$tmpdir/setup.log" + + log () { + echo "$1" >> "$setuplog" + } + + mkdir -p "$tmpdir"/{db,socket} + + export PGDATA="$tmpdir/db" + export PGHOST="$tmpdir/socket" + export PGUSER + export PGDATABASE + export PGRST_DB_URI="postgresql:///$PGDATABASE?host=$PGHOST&user=$PGUSER" + export PGRST_DB_SCHEMAS + export PGRST_DB_ANON_ROLE + + log "Initializing database cluster..." + # We try to make the database cluster as independent as possible from the host + # by specifying the timezone, locale and encoding. + PGTZ=UTC initdb --no-locale --encoding=UTF8 --nosync -U "$PGUSER" --auth=trust \ + >> "$setuplog" + + log "Starting the database cluster..." + # Instead of listening on a local port, we will listen on a unix domain socket. + pg_ctl -l "$tmpdir/db.log" start -o "-F -c listen_addresses=\"\" -k $PGHOST" \ + >> "$setuplog" + + log "Waiting for the database cluster to be ready..." + # Waiting is required for older versions of Postgres (< 10). + until pg_isready >> "$setuplog"; do + sleep 0.1 + done + + stop () { + log "Stopping the database cluster..." + pg_ctl stop -m i >> "$setuplog" + } + trap stop EXIT + + log "Loading fixtures..." + psql -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog" + + log "Done. Running command..." + ("$_arg_command" "''${_arg_leftovers[@]}") ''; # Helper script for running a command against all PostgreSQL versions. diff --git a/test/create_test_db b/test/create_test_db index dfd50a78b..3a90893a9 100755 --- a/test/create_test_db +++ b/test/create_test_db @@ -62,11 +62,6 @@ EOF #Remove database path from the connection uri--prevents setting up the new database name with PGDATABASE URI=$(echo "$URI" | cut -d'/' -f1-3) -PGDATABASE=$DB PGOPTIONS='-c client_min_messages=WARNING' psql "$URI" --set=db="$DB" -Xq <> "$setuplog" << EOF - create extension pgcrypto; - alter database $PGDATABASE set request.jwt.claim.id = '-1'; - alter role $PGUSER set default_text_search_config to english; - \i test/fixtures/load.sql -EOF +psql -v ON_ERROR_STOP=1 -f test/fixtures/load.sql >> "$setuplog" log "Done. Running command..." # Run the command that was given as an argument. The `exit` trap above will