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.

55 lines
1.2 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/mongodb
  5. export DATA_DIR=$SERVICE_DATASTORE$DB_DATADIR
  6. is_db_locked() {
  7. is_volume_used "$DATASTORE/${SERVICE_NAME}"
  8. }
  9. _set_server_db_params() {
  10. server_docker_opts+=("-v" "${SERVICE_CONFIGSTORE}/etc/mongod.conf:/etc/mongod.conf"
  11. "--add-host" "${SERVICE_NAME}:127.0.0.1")
  12. }
  13. _set_db_params() {
  14. local docker_ip="$1" docker_network="$2"
  15. db_docker_opts+=("--network" "$docker_network")
  16. db_cmd_opts+=("--host" "$docker_ip")
  17. check_command="db.serverStatus().ok"
  18. }
  19. ddb() { dcmd mongo --quiet "$@"; }
  20. mongo:db:ls() {
  21. local out
  22. if ! out=$(ddb < <(echo "JSON.stringify(db.adminCommand( { listDatabases: 1 } ))")); then
  23. err "Could not query list of databases."
  24. return 1
  25. fi
  26. e "$out" | jq -r '.databases[] | .name'
  27. }
  28. mongo:db:rename() {
  29. local src="$1" dst="$2" out
  30. if ! out=$(ddb <<EOF
  31. db.copyDatabase("$src","$dst");
  32. use $src;
  33. db.dropDatabase();
  34. EOF
  35. ); then
  36. err "Could not rename database '$src' to '$dst'."
  37. err "$out"
  38. return 1
  39. fi
  40. debug "$out"
  41. }