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.

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