Skip to content

Commit 0e3f647

Browse files
committed
Add command to destroy databases
1 parent 3357ca9 commit 0e3f647

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

commands

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ case "$1" in
122122
$PLUGIN_COMMAND_PREFIX:create <name>, Create a $PLUGIN_SERVICE service
123123
$PLUGIN_COMMAND_PREFIX:create-database <name> <db>, Create a $PLUGIN_SERVICE database in the specified service
124124
$PLUGIN_COMMAND_PREFIX:destroy <name>, Delete the $PLUGIN_SERVICE service and stop its container if there are no links left
125-
$PLUGIN_COMMAND_PREFIX:destroy-database <name> <db>, Delete a $PLUGIN_SERVICE database on the specified service
125+
$PLUGIN_COMMAND_PREFIX:destroy-database <name> <db>, Delete a $PLUGIN_SERVICE database in the specified service
126126
$PLUGIN_COMMAND_PREFIX:export <name>, Export a dump of the $PLUGIN_SERVICE service database
127127
$PLUGIN_COMMAND_PREFIX:expose <name> [port], Expose a $PLUGIN_SERVICE service on custom port if provided (random port otherwise)
128128
$PLUGIN_COMMAND_PREFIX:import <name> < <file>, Import a dump into the $PLUGIN_SERVICE service database

subcommands/destroy-database

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/config"
3+
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
4+
source "$PLUGIN_BASE_PATH/common/functions"
5+
source "$(dirname "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)")/functions"
6+
7+
postgres-destroy-database-cmd() {
8+
declare desc="delete a $PLUGIN_SERVICE database from the specified service"
9+
local cmd="$PLUGIN_COMMAND_PREFIX:destroy-database" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
10+
declare SERVICE="$1" DATABASE="$2"
11+
12+
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
13+
[[ -z "$DATABASE" ]] && dokku_log_fail "Please specify a name for the database"
14+
verify_service_name "$SERVICE"
15+
verify_database_name "$SERVICE" "$DATABASE"
16+
SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
17+
SERVICE_NAME="$(get_service_name "$SERVICE")"
18+
19+
dokku_log_info1 "Deleting $DATABASE from $SERVICE"
20+
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
21+
rm -f "$SERVICE_ROOT/databases/$DATABASE"
22+
rm -f "$SERVICE_ROOT/auth/$DATABASE"
23+
dokku_log_info2 "$PLUGIN_SERVICE $SERVICE database deleted: $DATABASE"
24+
else
25+
dokku_log_fail "Could not delete the database"
26+
fi
27+
}
28+
29+
postgres-destroy-database-cmd "$@"

0 commit comments

Comments
 (0)