Skip to content

Commit

Permalink
Add command to destroy databases
Browse files Browse the repository at this point in the history
  • Loading branch information
kane-c committed Jun 29, 2017
1 parent 52e566c commit 492f172
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion commands
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ case "$1" in
$PLUGIN_COMMAND_PREFIX:create <name>, Create a $PLUGIN_SERVICE service
$PLUGIN_COMMAND_PREFIX:create-database <name> <db>, Create a $PLUGIN_SERVICE database in the specified service
$PLUGIN_COMMAND_PREFIX:destroy <name>, Delete the $PLUGIN_SERVICE service and stop its container if there are no links left
$PLUGIN_COMMAND_PREFIX:destroy-database <name> <db>, Delete a $PLUGIN_SERVICE database on the specified service
$PLUGIN_COMMAND_PREFIX:destroy-database <name> <db>, Delete a $PLUGIN_SERVICE database in the specified service
$PLUGIN_COMMAND_PREFIX:export <name>, Export a dump of the $PLUGIN_SERVICE service database
$PLUGIN_COMMAND_PREFIX:expose <name> [port], Expose a $PLUGIN_SERVICE service on custom port if provided (random port otherwise)
$PLUGIN_COMMAND_PREFIX:import <name> < <file>, Import a dump into the $PLUGIN_SERVICE service database
Expand Down
29 changes: 29 additions & 0 deletions subcommands/destroy-database
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config"
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_BASE_PATH/common/functions"
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"

postgres-destroy-database-cmd() {
declare desc="delete a $PLUGIN_SERVICE database from the specified service"
local cmd="$PLUGIN_COMMAND_PREFIX:destroy-database" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
declare SERVICE="$1" DATABASE="$2"

[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
[[ -z "$DATABASE" ]] && dokku_log_fail "Please specify a name for the database"
verify_service_name "$SERVICE"
verify_database_name "$SERVICE" "$DATABASE"
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
SERVICE_NAME="$(get_service_name "$SERVICE")"

dokku_log_info1 "Deleting $DATABASE from $SERVICE"
if docker exec "$SERVICE_NAME" su - postgres -c "psql -c \"DROP DATABASE $DATABASE;\"" 2> /dev/null && docker exec "$SERVICE_NAME" su - postgres -c "psql -c \"DROP USER $DATABASE;\"" 2> /dev/null; then
rm -f "$SERVICE_ROOT/databases/$DATABASE"
rm -f "$SERVICE_ROOT/auth/$DATABASE"
dokku_log_info2 "$PLUGIN_SERVICE $SERVICE database deleted: $DATABASE"
else
dokku_log_fail "Could not delete the database"
fi
}

postgres-destroy-database-cmd "$@"

0 comments on commit 492f172

Please sign in to comment.