fork 0k-charms
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.

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