Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postgres:reset command #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ postgres:links <service> # list all apps linked to the
postgres:list # list all postgres services
postgres:logs <service> [-t|--tail] # print the most recent log(s) for this service
postgres:promote <service> <app> # promote service <service> as DATABASE_URL in <app>
postgres:reset <service> [-f|--force] # delete all data in postgres service
postgres:restart <service> # graceful shutdown and restart of the postgres service container
postgres:start <service> # start a previously stopped postgres service
postgres:stop <service> # stop a running postgres service
Expand Down
20 changes: 20 additions & 0 deletions common-functions
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,26 @@ is_valid_service_name() {
return 1
}

prompt_confirmation() {
declare desc="Prompts user to confirm destructive action"
declare MESSAGE="$1" SERVICE="$2" FORCE_FLAG="$3"

if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then
DOKKU_APPS_FORCE_DELETE=1
fi
if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then
dokku_log_warn "WARNING: Potentially Destructive Action"
dokku_log_warn "$MESSAGE"
dokku_log_warn "To proceed, type \"$SERVICE\""
echo ""

read -rp "> " service_name
if [[ "$service_name" != "$SERVICE" ]]; then
dokku_log_fail "Confirmation did not match $SERVICE. Aborted."
fi
fi
}

remove_from_links_file() {
declare desc="Removes an app from the service link file"
declare SERVICE="$1" APP="$2"
Expand Down
11 changes: 11 additions & 0 deletions functions
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ service_import() {
docker exec -i "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_restore -h localhost -cO --if-exists -d "$DATABASE_NAME" -U postgres -w
}

service_reset() {
local SERVICE="$1"
local SERVICE_NAME="$(get_service_name "$SERVICE")"
local DATABASE_NAME="$(get_database_name "$SERVICE")"

dokku_log_info2_quiet "Deleting all data in $SERVICE"
docker exec "$SERVICE_NAME" su - postgres -c "dropdb $DATABASE_NAME"
docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME"
dokku_log_info2 "All $SERVICE data deleted"
}

service_start() {
local SERVICE="$1"
local QUIET="$2"
Expand Down
17 changes: 2 additions & 15 deletions subcommands/destroy
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,8 @@ service-destroy-cmd() {

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

if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then
DOKKU_APPS_FORCE_DELETE=1
fi
if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then
dokku_log_warn "WARNING: Potentially Destructive Action"
dokku_log_warn "This command will destroy $SERVICE $PLUGIN_SERVICE service."
dokku_log_warn "To proceed, type \"$SERVICE\""
echo ""

read -rp "> " service_name
if [[ "$service_name" != "$SERVICE" ]]; then
dokku_log_warn "Confirmation did not match $SERVICE. Aborted."
exit 1
fi
fi
local message="This command will destroy $SERVICE $PLUGIN_SERVICE service."
prompt_confirmation "$message" "$SERVICE" "$FORCE_FLAG"

dokku_log_info2_quiet "Deleting $SERVICE"
service_backup_unschedule "$SERVICE"
Expand Down
25 changes: 25 additions & 0 deletions subcommands/reset
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/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"

service-reset-cmd() {
#E delete all data in $PLUGIN_COMMAND_PREFIX service named lolipop
#E dokku $PLUGIN_COMMAND_PREFIX:reset lolipop
#A service, service to run command against
#F -f|--force, force delete without asking for confirmation
declare desc="delete all data in $PLUGIN_SERVICE service"
local cmd="$PLUGIN_COMMAND_PREFIX:reset" argv=("$@"); [[ ${argv[0]} == "$cmd" ]] && shift 1
declare SERVICE="$1" FORCE_FLAG="$2"

[[ -z "$SERVICE" ]] && dokku_log_fail "Please specify a name for the service"
verify_service_name "$SERVICE"

local message="This command will delete all data in $SERVICE $PLUGIN_SERVICE service."
prompt_confirmation "$message" "$SERVICE" "$FORCE_FLAG"

service_reset "$SERVICE"
}

service-reset-cmd "$@"
28 changes: 28 additions & 0 deletions tests/service_reset.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bats
load test_helper

setup() {
dokku "$PLUGIN_COMMAND_PREFIX:create" l
}

teardown() {
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
}

@test "($PLUGIN_COMMAND_PREFIX:reset) success with --force" {
run dokku --force "$PLUGIN_COMMAND_PREFIX:reset" l
assert_contains "${lines[*]}" "All l data deleted"
josegonzalez marked this conversation as resolved.
Show resolved Hide resolved
assert_success
}

@test "($PLUGIN_COMMAND_PREFIX:reset) error when there are no arguments" {
run dokku "$PLUGIN_COMMAND_PREFIX:reset"
assert_contains "${lines[*]}" "Please specify a name for the service"
assert_failure
}

@test "($PLUGIN_COMMAND_PREFIX:reset) error when service does not exist" {
run dokku "$PLUGIN_COMMAND_PREFIX:reset" not_existing_service
assert_contains "${lines[*]}" "service not_existing_service does not exist"
assert_failure
}