133 lines
3.4 KiB

  1. # -*- mode: shell-script -*-
  2. include pretty
  3. export DB_NAME="$SERVICE_NAME" ## general type of database (ie: postgres/mysql...)
  4. export DB_DATADIR=/var/lib/postgresql/data
  5. export DATA_DIR=$SERVICE_DATASTORE$DB_DATADIR
  6. export LOCAL_DB_PASSFILE="$DATA_DIR/pgpass"
  7. export CLIENT_DB_PASSFILE="/root/.pgpass"
  8. export PG_HBA="$DATA_DIR/pg_hba.conf"
  9. is_db_locked() {
  10. local host_db_volume
  11. if [ "${HOST_DATASTORE+x}" ]; then
  12. host_db_volume="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  13. else
  14. host_db_volume="$DATASTORE/${SERVICE_NAME}$DB_DATADIR"
  15. fi
  16. is_volume_used "$host_db_volume"
  17. }
  18. _set_server_db_params() {
  19. server_docker_opts+=()
  20. }
  21. _set_db_params() {
  22. local docker_ip="$1" docker_network="$2"
  23. if [ "${HOST_DATASTORE+x}" ]; then
  24. export HOST_DB_PASSFILE="$HOST_DATASTORE/${SERVICE_NAME}$DB_DATADIR/pgpass"
  25. else
  26. export HOST_DB_PASSFILE="$CLIENT_DB_PASSFILE"
  27. fi
  28. [ -f "$CLIENT_DB_PASSFILE" ] || touch "$CLIENT_DB_PASSFILE"
  29. db_docker_opts+=("--network" "$docker_network"
  30. "-e" PGHOST="$docker_ip"
  31. "-e" PGUSER=postgres
  32. "-e" prefix_pg_local_command=' ')
  33. db_cmd_opts+=()
  34. check_command="SELECT 1;"
  35. }
  36. ddb () { dcmd psql -qAt "$@"; }
  37. ##
  38. ## Entrypoints
  39. ##
  40. db_drop () {
  41. local dbname="$1"
  42. dcmd dropdb --if-exists "$1"
  43. }
  44. db_create () {
  45. local dbname="$1"
  46. dcmd createdb "$dbname" || return 1
  47. info "Database '$dbname' created."
  48. }
  49. db_install_extensions() {
  50. local dbname="$1"
  51. shift
  52. while [ "$#" != 0 ]; do
  53. case "$1" in
  54. postgis)
  55. ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
  56. dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
  57. info "Installed postgis extensions on database '$dbname'."
  58. ;;
  59. *)
  60. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS $1;") || return 1
  61. info "Installed $1 extension on database '$dbname'."
  62. ;;
  63. esac
  64. shift
  65. done
  66. }
  67. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  68. db_has_database() {
  69. local dbname="$1"
  70. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  71. debug "Database $dbname exists."
  72. return 0
  73. else
  74. debug "Database $dbname exists."
  75. return 1
  76. fi
  77. }
  78. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  79. db_has_user() {
  80. local user="$1" users
  81. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  82. err "Failed to get user list"
  83. return 1
  84. }
  85. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  86. }
  87. db_create_user() {
  88. local user="$1" password="$2"
  89. ## Using this instead of pipes is important so that trap works
  90. ddb < <(echo "CREATE USER \"$user\" WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  91. }
  92. db_change_password() {
  93. local user="$1" password="$2"
  94. ## Using this instead of pipes is important so that trap works
  95. ddb < <(echo "ALTER USER \"$user\" WITH PASSWORD '$password';")
  96. }
  97. PGM() {
  98. ensure_db_docker_running </dev/null || return 1
  99. echo "${db_docker_opts[@]}"
  100. dcmd pgm "$@"
  101. }
  102. db_grant_rights () {
  103. local dbname="$1" user="$2"
  104. PGM chown "$user" "$dbname"
  105. }