|
4 | 4 | # `postgresql://pgtest@localhost:9100` to connect to the server. |
5 | 5 | # |
6 | 6 | # Example: |
7 | | -# ./postgres_wrapper.sh /tmp/myinstance 'createdb --maintenance-db=postgresql://pgtest@localhost:9100/postgres mydb; psql postgresql://pgtest@localhost:9100/mydb -c \"CREATE TABLE foo (id integer);\"' |
| 7 | +# ./postgres_wrapper /tmp/myinstance 'createdb --maintenance-db=postgresql://pgtest@localhost:9100/postgres mydb; psql postgresql://pgtest@localhost:9100/mydb -c \"CREATE TABLE foo (id integer);\"' |
8 | 8 | # |
9 | 9 | # Inspired by https://github.com/tk0miya/testing.postgresql |
10 | | - |
11 | 10 | set -u |
12 | | - |
13 | 11 | TEST_DIR=$1 |
14 | 12 | shift |
| 13 | +DB_DIR="$(mktemp -d -t lnt)" |
15 | 14 | if [ -d "${TEST_DIR}" ]; then |
16 | 15 | echo 1>&2 "${TEST_DIR} already exists" |
17 | 16 | exit 1 |
18 | 17 | fi |
19 | 18 |
|
20 | | -mkdir -p "${TEST_DIR}/db" |
| 19 | +mkdir -p "${TEST_DIR}" |
| 20 | +ln -s ${TEST_DIR}/db_root ${DB_DIR} |
21 | 21 |
|
22 | | -INITDB_FLAGS+=" --pgdata=${TEST_DIR}/db" |
23 | | -INITDB_FLAGS+=" --waldir=${TEST_DIR}/db" |
| 22 | +INITDB_FLAGS+=" --pgdata=${DB_DIR}/db" |
| 23 | +INITDB_FLAGS+=" --waldir=${DB_DIR}/db" |
24 | 24 | INITDB_FLAGS+=" --nosync" |
25 | 25 | INITDB_FLAGS+=" --no-locale" |
26 | 26 | INITDB_FLAGS+=" --auth=trust" |
27 | 27 | INITDB_FLAGS+=" --username=pgtest" |
28 | | -echo "$ initdb $INITDB_FLAGS >& ${TEST_DIR}/initdb_log.txt" |
29 | | -initdb ${INITDB_FLAGS} >& ${TEST_DIR}/initdb_log.txt |
| 28 | +echo "$ initdb $INITDB_FLAGS >& ${DB_DIR}/initdb_log.txt" |
| 29 | +initdb ${INITDB_FLAGS} >& ${DB_DIR}/initdb_log.txt |
30 | 30 |
|
31 | 31 | POSTGRES_FLAGS+=" -p 9100" |
32 | | -POSTGRES_FLAGS+=" -D ${TEST_DIR}/db" |
33 | | -POSTGRES_FLAGS+=" -k ${TEST_DIR}/db" |
| 32 | +POSTGRES_FLAGS+=" -D ${DB_DIR}/db" |
| 33 | +POSTGRES_FLAGS+=" -k ${DB_DIR}/db" |
34 | 34 | POSTGRES_FLAGS+=" -h 127.0.0.1" |
35 | 35 | POSTGRES_FLAGS+=" -F" |
36 | 36 | POSTGRES_FLAGS+=" -c logging_collector=off" |
37 | | -echo "$ postgres $POSTGRES_FLAGS >& ${TEST_DIR}/server_log.txt" |
38 | | -postgres ${POSTGRES_FLAGS} >& ${TEST_DIR}/server_log.txt & |
| 37 | +echo "$ postgres $POSTGRES_FLAGS >& ${DB_DIR}/server_log.txt" |
| 38 | +postgres ${POSTGRES_FLAGS} >& ${DB_DIR}/server_log.txt & |
39 | 39 | PG_PID=$! |
40 | 40 | sleep 1 # Give the server time to start. |
41 | 41 |
|
|
47 | 47 | kill -15 ${PG_PID} |
48 | 48 | [ $? -ne 0 ] && (echo 1>&1 "Error: Could not kill postgres server"; exit 1) |
49 | 49 | wait ${PG_PID} |
| 50 | +[ ${RC} -ne 0 ] && (rm -rf ${DB_DIR}) |
50 | 51 | exit ${RC} |
0 commit comments