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.

103 lines
2.7 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. if [ "$POSTGIS" ]; then
  33. ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
  34. dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
  35. info "Installed postgis extensions on database '$dbname'."
  36. fi
  37. if [ "$UNACCENT" ]; then
  38. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS unaccent;") || return 1
  39. info "Installed unaccent extension on database '$dbname'."
  40. fi
  41. }
  42. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  43. db_has_database() {
  44. local dbname="$1"
  45. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  46. debug "Database $dbname exists."
  47. return 0
  48. else
  49. debug "Database $dbname exists."
  50. return 1
  51. fi
  52. }
  53. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  54. db_has_user() {
  55. local user="$1" users
  56. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  57. err "Failed to get user list"
  58. return 1
  59. }
  60. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  61. }
  62. db_create_user() {
  63. local user="$1" password="$2"
  64. ## Using this instead of pipes is important so that trap works
  65. ddb < <(echo "CREATE USER $user WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  66. }
  67. db_change_password() {
  68. local user="$1" password="$2"
  69. ## Using this instead of pipes is important so that trap works
  70. ddb < <(echo "ALTER USER $user WITH PASSWORD '$password';")
  71. }
  72. PGM() {
  73. ensure_db_docker_running </dev/null || return 1
  74. echo "${db_docker_opts[@]}"
  75. dcmd pgm "$@"
  76. }
  77. db_grant_rights () {
  78. local dbname="$1" user="$2"
  79. PGM chown "$user" "$dbname"
  80. }