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 <path>`.
This commit is contained in:
committed by
Wolfgang Walther
parent
f6b3a5cc22
commit
ba47a54293
@@ -77,9 +77,8 @@ let
|
||||
text =
|
||||
''
|
||||
#!${bash_5}/bin/bash
|
||||
set -euo pipefail
|
||||
|
||||
source ${argsParser}
|
||||
set -euo pipefail
|
||||
''
|
||||
|
||||
+ lib.optionalString redirectTixFiles ''
|
||||
|
||||
+1
-1
@@ -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
|
||||
|
||||
+55
-8
@@ -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.
|
||||
|
||||
@@ -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 <<EOF
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
ALTER DATABASE ${DB} SET request.jwt.claim.id = '-1';
|
||||
EOF
|
||||
|
||||
# Create a new connection string to use with the test runner
|
||||
export PGRST_DB_URI="postgres://${TEST_USER_NAME}:$TEST_USER_PASS@$HOST_PORT/$DB"
|
||||
export PGRST_DB_ANON_ROLE="postgrest_test_anonymous"
|
||||
|
||||
Vendored
+3
@@ -1,3 +1,6 @@
|
||||
CREATE EXTENSION IF NOT EXISTS pgcrypto;
|
||||
ALTER DATABASE :DBNAME SET request.jwt.claim.id = '-1';
|
||||
|
||||
set client_min_messages to warning;
|
||||
DROP SCHEMA IF EXISTS test, private, postgrest, jwt, public, تست, extensions, v1, v2 CASCADE;
|
||||
DROP TYPE IF EXISTS jwt_token CASCADE;
|
||||
|
||||
+1
-6
@@ -99,12 +99,7 @@ stop() {
|
||||
trap stop sigint sigterm exit
|
||||
|
||||
log "Loading fixtures..."
|
||||
psql -v ON_ERROR_STOP=1 >> "$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
|
||||
|
||||
Reference in New Issue
Block a user