Skip to content

Commit

Permalink
Add postgres:reset command
Browse files Browse the repository at this point in the history
  • Loading branch information
jayjun committed Jan 28, 2021
1 parent e414eac commit ac5a3d5
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 27 deletions.
25 changes: 13 additions & 12 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 <name> # delete all data in the 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 Expand Up @@ -85,7 +86,7 @@ export DATABASE_IMAGE_VERSION="${PLUGIN_IMAGE_VERSION}"
dokku postgres:create lolipop
```

You can also specify custom environment variables to start the postgres service in semi-colon separated form.
You can also specify custom environment variables to start the postgres service in semi-colon separated form.

```shell
export DATABASE_CUSTOM_ENV="USER=alpha;HOST=beta"
Expand Down Expand Up @@ -137,7 +138,7 @@ dokku postgres:info lolipop --version

```shell
# usage
dokku postgres:list
dokku postgres:list
```

List all services:
Expand Down Expand Up @@ -181,7 +182,7 @@ flags:
- `-a|--alias "BLUE_DATABASE"`: an alternative alias to use for linking to an app via environment variable
- `-q|--querystring "pool=5"`: ampersand delimited querystring arguments to append to the service link

A postgres service can be linked to a container. This will use native docker links via the docker-options plugin. Here we link it to our 'playground' app.
A postgres service can be linked to a container. This will use native docker links via the docker-options plugin. Here we link it to our 'playground' app.

> NOTE: this will restart your app
Expand Down Expand Up @@ -212,7 +213,7 @@ The host exposed here only works internally in docker containers. If you want yo
dokku postgres:link other_service playground
```

It is possible to change the protocol for `DATABASE_URL` by setting the environment variable `POSTGRES_DATABASE_SCHEME` on the app. Doing so will after linking will cause the plugin to think the service is not linked, and we advise you to unlink before proceeding.
It is possible to change the protocol for `DATABASE_URL` by setting the environment variable `POSTGRES_DATABASE_SCHEME` on the app. Doing so will after linking will cause the plugin to think the service is not linked, and we advise you to unlink before proceeding.

```shell
dokku config:set playground POSTGRES_DATABASE_SCHEME=postgres2
Expand Down Expand Up @@ -264,13 +265,13 @@ dokku postgres:connect lolipop
dokku postgres:enter <service>
```

A bash prompt can be opened against a running service. Filesystem changes will not be saved to disk.
A bash prompt can be opened against a running service. Filesystem changes will not be saved to disk.

```shell
dokku postgres:enter lolipop
```

You may also run a command directly against the service. Filesystem changes will not be saved to disk.
You may also run a command directly against the service. Filesystem changes will not be saved to disk.

```shell
dokku postgres:enter lolipop touch /tmp/test
Expand Down Expand Up @@ -401,7 +402,7 @@ Service scripting can be executed using the following commands:
dokku postgres:app-links <app>
```

List all postgres services that are linked to the 'playground' app.
List all postgres services that are linked to the 'playground' app.

```shell
dokku postgres:app-links playground
Expand Down Expand Up @@ -435,7 +436,7 @@ dokku postgres:clone lolipop lolipop-2
dokku postgres:exists <service>
```

Here we check if the lolipop postgres service exists.
Here we check if the lolipop postgres service exists.

```shell
dokku postgres:exists lolipop
Expand All @@ -448,7 +449,7 @@ dokku postgres:exists lolipop
dokku postgres:linked <service> <app>
```

Here we check if the lolipop postgres service is linked to the 'playground' app.
Here we check if the lolipop postgres service is linked to the 'playground' app.

```shell
dokku postgres:linked lolipop playground
Expand All @@ -461,7 +462,7 @@ dokku postgres:linked lolipop playground
dokku postgres:links <service>
```

List all apps linked to the 'lolipop' postgres service.
List all apps linked to the 'lolipop' postgres service.

```shell
dokku postgres:links lolipop
Expand Down Expand Up @@ -611,7 +612,7 @@ flags:

Schedule a backup:

> 'schedule' is a crontab expression, eg. "0 3 * * *" for each day at 3am
> 'schedule' is a crontab expression, eg. "0 3 \* \* \*" for each day at 3am
```shell
dokku postgres:backup-schedule lolipop "0 3 * * *" my-s3-bucket
Expand Down Expand Up @@ -653,4 +654,4 @@ dokku postgres:backup-unschedule lolipop

If you wish to disable the `docker pull` calls that the plugin triggers, you may set the `POSTGRES_DISABLE_PULL` environment variable to `true`. Once disabled, you will need to pull the service image you wish to deploy as shown in the `stderr` output.

Please ensure the proper images are in place when `docker pull` is disabled.
Please ensure the proper images are in place when `docker pull` is disabled.
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"
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
}

0 comments on commit ac5a3d5

Please sign in to comment.