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.

146 lines
3.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 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. shift
  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. if out=$(ddb -d "$dbname" < <(echo "SELECT extname
  57. FROM pg_extension
  58. WHERE extname = 'postgis';")); then
  59. if [ "$out" ]; then
  60. info "PostGIS already installed."
  61. shift
  62. continue
  63. fi
  64. fi
  65. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS postgis;
  66. CREATE EXTENSION IF NOT EXISTS postgis_topology;") || return 1
  67. dcmd /bin/bash -c "psql -d '$dbname' -v ON_ERROR_STOP=1 -f /usr/local/share/postgresql/contrib/postgis-*/legacy.sql" || return 1
  68. info "Installed postgis extensions on database '$dbname'."
  69. ;;
  70. *)
  71. ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS $1;") || return 1
  72. info "Installed $1 extension on database '$dbname'."
  73. ;;
  74. esac
  75. shift
  76. done
  77. }
  78. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  79. db_has_database() {
  80. local dbname="$1"
  81. if [ "$(ddb < <(echo "SELECT 1 FROM pg_database WHERE datname = '$dbname'"))" ]; then
  82. debug "Database $dbname exists."
  83. return 0
  84. else
  85. debug "Database $dbname exists."
  86. return 1
  87. fi
  88. }
  89. ## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
  90. db_has_user() {
  91. local user="$1" users
  92. users=$(ddb < <(echo "select u.usename from pg_catalog.pg_user u")) || {
  93. err "Failed to get user list"
  94. return 1
  95. }
  96. echo "$users" | grep "^${user}$" >/dev/null 2>&1
  97. }
  98. db_create_user() {
  99. local user="$1" password="$2"
  100. ## Using this instead of pipes is important so that trap works
  101. ddb < <(echo "CREATE USER \"$user\" WITH PASSWORD '$password' CREATEDB NOCREATEROLE;")
  102. }
  103. db_change_password() {
  104. local user="$1" password="$2"
  105. ## Using this instead of pipes is important so that trap works
  106. ddb < <(echo "ALTER USER \"$user\" WITH PASSWORD '$password';")
  107. }
  108. PGM() {
  109. ensure_db_docker_running </dev/null || return 1
  110. echo "${db_docker_opts[@]}"
  111. dcmd pgm "$@"
  112. }
  113. db_grant_rights () {
  114. local dbname="$1" user="$2"
  115. PGM chown -v "$user" "$dbname"
  116. }