about summary refs log tree commit diff
path: root/nixpkgs/pkgs/build-support/setup-hooks/postgresql-test-hook/postgresql-test-hook.sh
blob: d09153b2d64414064829e025da6344002e373d17 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
preCheckHooks+=('postgresqlStart')
postCheckHooks+=('postgresqlStop')


postgresqlStart() {

  # Add default environment variable values
  #
  # Client variables:
  #  - https://www.postgresql.org/docs/current/libpq-envars.html
  #
  # Server variables:
  #  - only PGDATA: https://www.postgresql.org/docs/current/creating-cluster.html

  if [[ "${PGDATA:-}" == "" ]]; then
    PGDATA="$NIX_BUILD_TOP/postgresql"
  fi
  export PGDATA

  if [[ "${PGHOST:-}" == "" ]]; then
    mkdir -p "$NIX_BUILD_TOP/run/postgresql"
    PGHOST="$NIX_BUILD_TOP/run/postgresql"
  fi
  export PGHOST

  if [[ "${PGUSER:-}" == "" ]]; then
    PGUSER="test_user"
  fi
  export PGUSER

  if [[ "${PGDATABASE:-}" == "" ]]; then
    PGDATABASE="test_db"
  fi
  export PGDATABASE

  if [[ "${postgresqlTestUserOptions:-}" == "" ]]; then
    postgresqlTestUserOptions="LOGIN"
  fi

  if [[ "${postgresqlTestSetupSQL:-}" == "" ]]; then
    postgresqlTestSetupSQL="$(cat <<EOF
      CREATE ROLE "$PGUSER" $postgresqlTestUserOptions;
      CREATE DATABASE "$PGDATABASE" OWNER '$PGUSER';
EOF
    )"
  fi

  if [[ "${postgresqlTestSetupCommands:-}" == "" ]]; then
    postgresqlTestSetupCommands='echo "$postgresqlTestSetupSQL" | PGUSER=postgres psql postgres'
  fi

  if ! type initdb >/dev/null; then
    echo >&2 'initdb not found. Did you add postgresql to the nativeCheckInputs?'
    false
  fi
  echo 'initializing postgresql'
  initdb -U postgres

  echo "$postgresqlExtraSettings" >>"$PGDATA/postgresql.conf"

  # Move the socket
  echo "unix_socket_directories = '$NIX_BUILD_TOP/run/postgresql'" >>"$PGDATA/postgresql.conf"

  # TCP ports can be a problem in some sandboxes,
  # so we disable tcp listening by default
  if ! [[ "${postgresqlEnableTCP:-}" = 1 ]]; then
    echo "listen_addresses = ''" >>"$PGDATA/postgresql.conf"
  fi

  echo 'starting postgresql'
  eval "${postgresqlStartCommands:-pg_ctl start}"

  echo 'setting up postgresql'
  eval "$postgresqlTestSetupCommands"

  runHook postgresqlTestSetupPost

}

postgresqlStop() {
  echo 'stopping postgresql'
  pg_ctl stop
}