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.

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