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.

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