Skip to content

Commit adb5119

Browse files
nix: Make postgrest-with-postgresql-xxx postgrest-test-io work better
The upside is that postgrest-with-postgresql-xxx postgrest-test-io works as expected now. The downside is, that postgrest-with-postgresql-xxx psql now starts without any schema. This now needs an explicit postgrest-with-postgresql-xxx -f path/to.sql to do anything useful. Resolves #2864
1 parent 8a3b0c6 commit adb5119

File tree

3 files changed

+62
-60
lines changed

3 files changed

+62
-60
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ jobs:
9292

9393
- name: Run IO tests
9494
if: always()
95-
run: postgrest-with-postgresql-${{ matrix.pgVersion }} -f test/io/fixtures.sql postgrest-test-io -vv
95+
run: postgrest-with-postgresql-${{ matrix.pgVersion }} postgrest-test-io -vv
9696

9797

9898
Test-Memory-Nix:

nix/tools/tests.nix

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ let
2828
withEnv = postgrest.env;
2929
}
3030
''
31-
${withTools.withPg} ${cabal-install}/bin/cabal v2-run ${devCabalOptions} \
32-
test:spec -- "''${_arg_leftovers[@]}"
31+
${withTools.withPg} -f test/spec/fixtures/load.sql \
32+
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec -- "''${_arg_leftovers[@]}"
3333
'';
3434

3535
testDoctests =
@@ -59,9 +59,10 @@ let
5959
withEnv = postgrest.env;
6060
}
6161
''
62-
${withTools.withPg} ${runtimeShell} -c " \
63-
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec && \
64-
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec"
62+
${withTools.withPg} -f test/spec/fixtures/load.sql \
63+
${runtimeShell} -c " \
64+
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec && \
65+
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec"
6566
'';
6667

6768
ioTestPython =
@@ -137,11 +138,12 @@ let
137138
138139
# collect all tests
139140
HPCTIXFILE="$tmpdir"/io.tix \
140-
${withTools.withPg} -f test/io/fixtures.sql ${cabal-install}/bin/cabal v2-exec ${devCabalOptions} -- \
141-
${ioTestPython}/bin/pytest -v test/io
141+
${withTools.withPg} -f test/io/fixtures.sql \
142+
${cabal-install}/bin/cabal v2-exec ${devCabalOptions} -- ${ioTestPython}/bin/pytest -v test/io
142143
143144
HPCTIXFILE="$tmpdir"/spec.tix \
144-
${withTools.withPg} ${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec
145+
${withTools.withPg} -f test/spec/fixtures/load.sql \
146+
${cabal-install}/bin/cabal v2-run ${devCabalOptions} test:spec
145147
146148
# Note: No coverage for doctests, as doctests leverage GHCi and GHCi does not support hpc
147149

nix/tools/withTools.nix

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ let
1717
{ name, postgresql }:
1818
let
1919
commandName = "postgrest-with-${name}";
20-
superuserRole = "postgres";
2120
in
2221
checkedShellScript
2322
{
2423
name = commandName;
25-
docs = "Run the given command in a temporary database with ${name}. If you wish to mutate the database, login with the '${superuserRole}' role.";
24+
docs = "Run the given command in a temporary database with ${name}. If you wish to mutate the database, login with the postgres role.";
2625
args =
2726
[
28-
"ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from], [test/spec/fixtures/load.sql])"
27+
"ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from])"
2928
"ARG_POSITIONAL_SINGLE([command], [Command to run])"
3029
"ARG_LEFTOVERS([command arguments])"
3130
"ARG_USE_ENV([PGUSER], [postgrest_test_authenticator], [Authenticator PG role])"
@@ -41,63 +40,64 @@ let
4140
withTmpDir = true;
4241
}
4342
''
44-
# avoid starting multiple layers of withTmpDb
45-
if test -v PGHOST; then
46-
exec "$_arg_command" "''${_arg_leftovers[@]}"
47-
fi
48-
4943
setuplog="$tmpdir/setup.log"
5044
5145
log () {
5246
echo "$1" >> "$setuplog"
5347
}
5448
55-
mkdir -p "$tmpdir"/{db,socket}
56-
# remove data dir, even if we keep tmpdir - no need to upload it to artifacts
57-
trap 'rm -rf $tmpdir/db' EXIT
58-
59-
export PGDATA="$tmpdir/db"
60-
export PGHOST="$tmpdir/socket"
61-
export PGUSER
62-
export PGDATABASE
63-
export PGRST_DB_SCHEMAS
64-
export PGTZ
65-
export PGOPTIONS
66-
67-
HBA_FILE="$tmpdir/pg_hba.conf"
68-
echo "local $PGDATABASE some_protected_user password" > "$HBA_FILE"
69-
echo "local $PGDATABASE all trust" >> "$HBA_FILE"
70-
71-
log "Initializing database cluster..."
72-
# We try to make the database cluster as independent as possible from the host
73-
# by specifying the timezone, locale and encoding.
74-
# initdb -U creates a superuser(man initdb)
75-
TZ=$PGTZ initdb --no-locale --encoding=UTF8 --nosync -U "${superuserRole}" --auth=trust \
76-
>> "$setuplog"
77-
78-
log "Starting the database cluster..."
79-
# Instead of listening on a local port, we will listen on a unix domain socket.
80-
pg_ctl -l "$tmpdir/db.log" -w start -o "-F -c listen_addresses=\"\" -c hba_file=$HBA_FILE -k $PGHOST -c log_statement=\"all\" " \
81-
>> "$setuplog"
82-
83-
# shellcheck disable=SC2317
84-
stop () {
85-
log "Stopping the database cluster..."
86-
pg_ctl stop -m i >> "$setuplog"
87-
rm -rf "$tmpdir/db"
88-
}
89-
trap stop EXIT
90-
91-
log "Creating a minimally privileged $PGUSER connection role..."
92-
createuser "$PGUSER" -U "${superuserRole}" --host="$tmpdir/socket" --no-createdb --no-inherit --no-superuser --no-createrole --no-replication --login
49+
# Avoid starting multiple layers of withTmpDb, but make sure to have the last invocation
50+
# load fixtures. Otherwise postgrest-with-postgresql-xx postgrest-test-io would not be possible.
51+
if ! test -v PGHOST; then
52+
53+
mkdir -p "$tmpdir"/{db,socket}
54+
# remove data dir, even if we keep tmpdir - no need to upload it to artifacts
55+
trap 'rm -rf $tmpdir/db' EXIT
56+
57+
export PGDATA="$tmpdir/db"
58+
export PGHOST="$tmpdir/socket"
59+
export PGUSER
60+
export PGDATABASE
61+
export PGRST_DB_SCHEMAS
62+
export PGTZ
63+
export PGOPTIONS
64+
65+
HBA_FILE="$tmpdir/pg_hba.conf"
66+
echo "local $PGDATABASE some_protected_user password" > "$HBA_FILE"
67+
echo "local $PGDATABASE all trust" >> "$HBA_FILE"
68+
69+
log "Initializing database cluster..."
70+
# We try to make the database cluster as independent as possible from the host
71+
# by specifying the timezone, locale and encoding.
72+
# initdb -U creates a superuser(man initdb)
73+
TZ=$PGTZ initdb --no-locale --encoding=UTF8 --nosync -U postgres --auth=trust \
74+
>> "$setuplog"
75+
76+
log "Starting the database cluster..."
77+
# Instead of listening on a local port, we will listen on a unix domain socket.
78+
pg_ctl -l "$tmpdir/db.log" -w start -o "-F -c listen_addresses=\"\" -c hba_file=$HBA_FILE -k $PGHOST -c log_statement=\"all\" " \
79+
>> "$setuplog"
80+
81+
# shellcheck disable=SC2317
82+
stop () {
83+
log "Stopping the database cluster..."
84+
pg_ctl stop -m i >> "$setuplog"
85+
rm -rf "$tmpdir/db"
86+
}
87+
trap stop EXIT
9388
94-
log "Loading fixtures under the ${superuserRole} role..."
95-
psql -U "${superuserRole}" -v PGUSER="$PGUSER" -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog"
89+
log "Creating a minimally privileged $PGUSER connection role..."
90+
createuser "$PGUSER" -U postgres --host="$tmpdir/socket" --no-createdb --no-inherit --no-superuser --no-createrole --no-replication --login
9691
97-
log "Done. Running command..."
92+
echo "${commandName}: You can connect with: psql 'postgres:///$PGDATABASE?host=$tmpdir/socket' -U postgres"
93+
echo "${commandName}: You can tail the logs with: tail -f $tmpdir/db.log"
94+
fi
9895
99-
echo "${commandName}: You can connect with: psql 'postgres:///$PGDATABASE?host=$tmpdir/socket' -U ${superuserRole}"
100-
echo "${commandName}: You can tail the logs with: tail -f $tmpdir/db.log"
96+
if test "$_arg_fixtures"; then
97+
log "Loading fixtures under the postgres role..."
98+
psql -U postgres -v PGUSER="$PGUSER" -v ON_ERROR_STOP=1 -f "$_arg_fixtures" >> "$setuplog"
99+
log "Done. Running command..."
100+
fi
101101
102102
("$_arg_command" "''${_arg_leftovers[@]}")
103103
'';

0 commit comments

Comments
 (0)