You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
2.8 KiB

  1. # -*- mode: shell-script -*-
  2. include pretty
  3. export DB_NAME="postgres"
  4. export DB_DATADIR=/var/lib/postgresql/data
  5. export DB_PASSFILE=/root/.pgpass
  6. is_db_locked() {
  7. is_volume_used "$DATASTORE/${SERVICE_NAME}"
  8. }
  9. _set_db_params() {
  10. local docker_ip="$1" docker_network="$2"
  11. export db_docker_opts="--network $docker_network -e PGHOST=$docker_ip -e PGUSER=postgres"
  12. export db_cmd_opts=
  13. }
  14. ddb () { dcmd psql -qAt "$@"; }
  15. ##
  16. ## Entrypoints
  17. ##
  18. db_drop () {
  19. local dbname="$1"
  20. dcmd dropdb --if-exists "$1"
  21. }
  22. db_create () {
  23. local dbname="$1"
  24. dcmd createdb "$dbname" || return 1
  25. info "Database '$dbname' created."
  26. if [ "$POSTGIS" ]; then
  27. ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
  28. dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
  29. info "Installed postgis extensions on database '$dbname'."
  30. fi
  31. if [ "$UNACCENT" ]; then
  32. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS unaccent;") || return 1
  33. info "Installed unaccent extension on database '$dbname'."
  34. fi
  35. }
  36. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  37. db_has_database() {
  38. local dbname="$1"
  39. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  40. debug "Database $dbname exists."
  41. return 0
  42. else
  43. debug "Database $dbname exists."
  44. return 1
  45. fi
  46. }
  47. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  48. db_has_user() {
  49. local user="$1" users
  50. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  51. err "Failed to get user list"
  52. return 1
  53. }
  54. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  55. }
  56. db_create_user() {
  57. local user="$1" password="$2"
  58. ## Using this instead of pipes is important so that trap works
  59. ddb < <(echo "CREATE USER $user WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  60. }
  61. db_change_password() {
  62. local user="$1" password="$2"
  63. ## Using this instead of pipes is important so that trap works
  64. ddb < <(echo "ALTER USER $user WITH PASSWORD '$password';")
  65. }
  66. PGM() {
  67. ensure_db_docker_running </dev/null || return 1
  68. ## XXXvlab: Here we should have been able to use this, but
  69. ## pgm requires the ``prefix_pg_local_command=' '`` hack. As
  70. ## soon this is fixed, we can re
  71. #dcmd /bin/sh pgm "$@"
  72. echo docker run -i --rm \
  73. $db_docker_opts -e prefix_pg_local_command=' ' \
  74. --entrypoint pgm "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@" >&2
  75. docker run -i --rm \
  76. $db_docker_opts -e prefix_pg_local_command=' ' \
  77. --entrypoint pgm "$DOCKER_BASE_IMAGE" $db_cmd_opts "$@"
  78. }
  79. db_grant_rights () {
  80. local dbname="$1" user="$2"
  81. PGM chown "$user" "$dbname"
  82. }