Skip to content

Commit 375b83e

Browse files
committed
Add postgres:reset command
1 parent c94658e commit 375b83e

File tree

6 files changed

+80
-15
lines changed

6 files changed

+80
-15
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ postgres:list List all postgres services
4242
postgres:logs <name> [-t] Print the most recent log(s) for this service
4343
postgres:promote <name> <app> Promote service <name> as DATABASE_URL in <app>
4444
postgres:restart <name> Graceful shutdown and restart of the postgres service container
45+
postgres:reset <name> Delete all data in the postgres service
4546
postgres:start <name> Start a previously stopped postgres service
4647
postgres:stop <name> Stop a running postgres service
4748
postgres:unexpose <name> Unexpose a previously exposed postgres service
@@ -152,6 +153,9 @@ dokku postgres:import lolipop < database.dump
152153
# you can clone an existing database to a new one
153154
dokku postgres:clone lolipop new_database
154155

156+
# you can reset a database to delete all its data
157+
dokku postgres:reset lolipop
158+
155159
# finally, you can destroy the container
156160
dokku postgres:destroy lolipop
157161
```

common-functions

+20
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ is_implemented_command() {
8383
return 0
8484
}
8585

86+
prompt_confirmation() {
87+
declare desc="Prompts user to confirm destructive action"
88+
declare MESSAGE="$1" SERVICE="$2" FORCE_FLAG="$3"
89+
90+
if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then
91+
DOKKU_APPS_FORCE_DELETE=1
92+
fi
93+
if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then
94+
dokku_log_warn "WARNING: Potentially Destructive Action"
95+
dokku_log_warn "$MESSAGE"
96+
dokku_log_warn "To proceed, type \"$SERVICE\""
97+
echo ""
98+
99+
read -rp "> " service_name
100+
if [[ "$service_name" != "$SERVICE" ]]; then
101+
dokku_log_fail "Confirmation did not match $SERVICE. Aborted."
102+
fi
103+
fi
104+
}
105+
86106
remove_from_links_file() {
87107
declare desc="Removes an app from the service link file"
88108
declare SERVICE="$1" APP="$2"

functions

+11
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ service_import() {
117117
docker exec -i "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_restore -h localhost -cO --if-exists -d "$DATABASE_NAME" -U postgres -w
118118
}
119119

120+
service_reset() {
121+
local SERVICE="$1"
122+
local SERVICE_NAME="$(get_service_name "$SERVICE")"
123+
local DATABASE_NAME="$(get_database_name "$SERVICE")"
124+
125+
dokku_log_info2_quiet "Deleting all data in $SERVICE"
126+
docker exec "$SERVICE_NAME" su - postgres -c "dropdb $DATABASE_NAME"
127+
docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME"
128+
dokku_log_info2 "All $SERVICE data deleted"
129+
}
130+
120131
service_start() {
121132
local SERVICE="$1"
122133
local QUIET="$2"

subcommands/destroy

+2-15
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,8 @@ service-destroy-cmd() {
2121

2222
[[ -s "$LINKS_FILE" ]] && dokku_log_fail "Cannot delete linked service"
2323

24-
if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then
25-
DOKKU_APPS_FORCE_DELETE=1
26-
fi
27-
if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then
28-
dokku_log_warn "WARNING: Potentially Destructive Action"
29-
dokku_log_warn "This command will destroy $SERVICE $PLUGIN_SERVICE service."
30-
dokku_log_warn "To proceed, type \"$SERVICE\""
31-
echo ""
32-
33-
read -rp "> " service_name
34-
if [[ "$service_name" != "$SERVICE" ]]; then
35-
dokku_log_warn "Confirmation did not match $SERVICE. Aborted."
36-
exit 1
37-
fi
38-
fi
24+
local message="This command will destroy $SERVICE $PLUGIN_SERVICE service."
25+
prompt_confirmation "$message" "$SERVICE" "$FORCE_FLAG"
3926

4027
dokku_log_info2_quiet "Deleting $SERVICE"
4128
service_backup_unschedule "$SERVICE"

subcommands/reset

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
service-reset-cmd() {
8+
#E delete all data in $PLUGIN_COMMAND_PREFIX service named lolipop
9+
#E dokku $PLUGIN_COMMAND_PREFIX:reset lolipop
10+
#A service, service to run command against
11+
#F -f|--force, force delete without asking for confirmation
12+
declare desc="delete all data in $PLUGIN_SERVICE service"
13+
local cmd="$PLUGIN_COMMAND_PREFIX:reset" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
14+
declare SERVICE="$1" FORCE_FLAG="$2"
15+
16+
[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
17+
verify_service_name "$SERVICE"
18+
19+
local message="This command will delete all data in $SERVICE $PLUGIN_SERVICE service."
20+
prompt_confirmation "$message" "$SERVICE" "$FORCE_FLAG"
21+
22+
service_reset "$SERVICE"
23+
}
24+
25+
service-reset-cmd "$@"

tests/service_reset.bats

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bats
2+
load test_helper
3+
4+
@test "($PLUGIN_COMMAND_PREFIX:reset) success with --force" {
5+
dokku "$PLUGIN_COMMAND_PREFIX:create" l
6+
run dokku --force "$PLUGIN_COMMAND_PREFIX:reset" l
7+
assert_contains "${lines[*]}" "All l data deleted"
8+
}
9+
10+
@test "($PLUGIN_COMMAND_PREFIX:reset) error when there are no arguments" {
11+
run dokku "$PLUGIN_COMMAND_PREFIX:reset"
12+
assert_contains "${lines[*]}" "Please specify a name for the service"
13+
}
14+
15+
@test "($PLUGIN_COMMAND_PREFIX:reset) error when service does not exist" {
16+
run dokku "$PLUGIN_COMMAND_PREFIX:reset" not_existing_service
17+
assert_contains "${lines[*]}" "service not_existing_service does not exist"
18+
}

0 commit comments

Comments
 (0)