|
17 | 17 | { name, postgresql }:
|
18 | 18 | let
|
19 | 19 | commandName = "postgrest-with-${name}";
|
20 |
| - superuserRole = "postgres"; |
21 | 20 | in
|
22 | 21 | checkedShellScript
|
23 | 22 | {
|
24 | 23 | 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."; |
26 | 25 | args =
|
27 | 26 | [
|
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])" |
29 | 28 | "ARG_POSITIONAL_SINGLE([command], [Command to run])"
|
30 | 29 | "ARG_LEFTOVERS([command arguments])"
|
31 | 30 | "ARG_USE_ENV([PGUSER], [postgrest_test_authenticator], [Authenticator PG role])"
|
|
41 | 40 | withTmpDir = true;
|
42 | 41 | }
|
43 | 42 | ''
|
44 |
| - # avoid starting multiple layers of withTmpDb |
45 |
| - if test -v PGHOST; then |
46 |
| - exec "$_arg_command" "''${_arg_leftovers[@]}" |
47 |
| - fi |
48 |
| -
|
49 | 43 | setuplog="$tmpdir/setup.log"
|
50 | 44 |
|
51 | 45 | log () {
|
52 | 46 | echo "$1" >> "$setuplog"
|
53 | 47 | }
|
54 | 48 |
|
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 |
93 | 88 |
|
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 |
96 | 91 |
|
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 |
98 | 95 |
|
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 |
101 | 101 |
|
102 | 102 | ("$_arg_command" "''${_arg_leftovers[@]}")
|
103 | 103 | '';
|
|
0 commit comments