Skip to content

Commit ac5a3d5

Browse files
committed
Add postgres:reset command
1 parent e414eac commit ac5a3d5

File tree

6 files changed

+99
-27
lines changed

6 files changed

+99
-27
lines changed

README.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ postgres:links <service> # list all apps linked to the
4242
postgres:list # list all postgres services
4343
postgres:logs <service> [-t|--tail] # print the most recent log(s) for this service
4444
postgres:promote <service> <app> # promote service <service> as DATABASE_URL in <app>
45+
postgres:reset <name> # delete all data in the postgres service
4546
postgres:restart <service> # graceful shutdown and restart of the postgres service container
4647
postgres:start <service> # start a previously stopped postgres service
4748
postgres:stop <service> # stop a running postgres service
@@ -85,7 +86,7 @@ export DATABASE_IMAGE_VERSION="${PLUGIN_IMAGE_VERSION}"
8586
dokku postgres:create lolipop
8687
```
8788

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

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

138139
```shell
139140
# usage
140-
dokku postgres:list
141+
dokku postgres:list
141142
```
142143

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

184-
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.
185+
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.
185186

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

215-
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.
216+
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.
216217

217218
```shell
218219
dokku config:set playground POSTGRES_DATABASE_SCHEME=postgres2
@@ -264,13 +265,13 @@ dokku postgres:connect lolipop
264265
dokku postgres:enter <service>
265266
```
266267

267-
A bash prompt can be opened against a running service. Filesystem changes will not be saved to disk.
268+
A bash prompt can be opened against a running service. Filesystem changes will not be saved to disk.
268269

269270
```shell
270271
dokku postgres:enter lolipop
271272
```
272273

273-
You may also run a command directly against the service. Filesystem changes will not be saved to disk.
274+
You may also run a command directly against the service. Filesystem changes will not be saved to disk.
274275

275276
```shell
276277
dokku postgres:enter lolipop touch /tmp/test
@@ -401,7 +402,7 @@ Service scripting can be executed using the following commands:
401402
dokku postgres:app-links <app>
402403
```
403404

404-
List all postgres services that are linked to the 'playground' app.
405+
List all postgres services that are linked to the 'playground' app.
405406

406407
```shell
407408
dokku postgres:app-links playground
@@ -435,7 +436,7 @@ dokku postgres:clone lolipop lolipop-2
435436
dokku postgres:exists <service>
436437
```
437438

438-
Here we check if the lolipop postgres service exists.
439+
Here we check if the lolipop postgres service exists.
439440

440441
```shell
441442
dokku postgres:exists lolipop
@@ -448,7 +449,7 @@ dokku postgres:exists lolipop
448449
dokku postgres:linked <service> <app>
449450
```
450451

451-
Here we check if the lolipop postgres service is linked to the 'playground' app.
452+
Here we check if the lolipop postgres service is linked to the 'playground' app.
452453

453454
```shell
454455
dokku postgres:linked lolipop playground
@@ -461,7 +462,7 @@ dokku postgres:linked lolipop playground
461462
dokku postgres:links <service>
462463
```
463464

464-
List all apps linked to the 'lolipop' postgres service.
465+
List all apps linked to the 'lolipop' postgres service.
465466

466467
```shell
467468
dokku postgres:links lolipop
@@ -611,7 +612,7 @@ flags:
611612

612613
Schedule a backup:
613614

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

654655
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.
655656

656-
Please ensure the proper images are in place when `docker pull` is disabled.
657+
Please ensure the proper images are in place when `docker pull` is disabled.

common-functions

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,26 @@ is_valid_service_name() {
101101
return 1
102102
}
103103

104+
prompt_confirmation() {
105+
declare desc="Prompts user to confirm destructive action"
106+
declare MESSAGE="$1" SERVICE="$2" FORCE_FLAG="$3"
107+
108+
if [[ "$FORCE_FLAG" == "force" ]] || [[ "$FORCE_FLAG" == "-f" ]] || [[ "$FORCE_FLAG" == "--force" ]]; then
109+
DOKKU_APPS_FORCE_DELETE=1
110+
fi
111+
if [[ -z "$DOKKU_APPS_FORCE_DELETE" ]]; then
112+
dokku_log_warn "WARNING: Potentially Destructive Action"
113+
dokku_log_warn "$MESSAGE"
114+
dokku_log_warn "To proceed, type \"$SERVICE\""
115+
echo ""
116+
117+
read -rp "> " service_name
118+
if [[ "$service_name" != "$SERVICE" ]]; then
119+
dokku_log_fail "Confirmation did not match $SERVICE. Aborted."
120+
fi
121+
fi
122+
}
123+
104124
remove_from_links_file() {
105125
declare desc="Removes an app from the service link file"
106126
declare SERVICE="$1" APP="$2"

functions

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ service_import() {
121121
docker exec -i "$SERVICE_NAME" env PGPASSWORD="$PASSWORD" pg_restore -h localhost -cO --if-exists -d "$DATABASE_NAME" -U postgres -w
122122
}
123123

124+
service_reset() {
125+
local SERVICE="$1"
126+
local SERVICE_NAME="$(get_service_name "$SERVICE")"
127+
local DATABASE_NAME="$(get_database_name "$SERVICE")"
128+
129+
dokku_log_info2_quiet "Deleting all data in $SERVICE"
130+
docker exec "$SERVICE_NAME" su - postgres -c "dropdb $DATABASE_NAME"
131+
docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME"
132+
dokku_log_info2 "All $SERVICE data deleted"
133+
}
134+
124135
service_start() {
125136
local SERVICE="$1"
126137
local QUIET="$2"

subcommands/destroy

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,8 @@ service-destroy-cmd() {
2424

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

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

4330
dokku_log_info2_quiet "Deleting $SERVICE"
4431
service_backup_unschedule "$SERVICE"

subcommands/reset

Lines changed: 25 additions & 0 deletions
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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bats
2+
load test_helper
3+
4+
setup() {
5+
dokku "$PLUGIN_COMMAND_PREFIX:create" l
6+
}
7+
8+
teardown() {
9+
dokku --force "$PLUGIN_COMMAND_PREFIX:destroy" l
10+
}
11+
12+
@test "($PLUGIN_COMMAND_PREFIX:reset) success with --force" {
13+
run dokku --force "$PLUGIN_COMMAND_PREFIX:reset" l
14+
assert_contains "${lines[*]}" "All l data deleted"
15+
assert_success
16+
}
17+
18+
@test "($PLUGIN_COMMAND_PREFIX:reset) error when there are no arguments" {
19+
run dokku "$PLUGIN_COMMAND_PREFIX:reset"
20+
assert_contains "${lines[*]}" "Please specify a name for the service"
21+
assert_failure
22+
}
23+
24+
@test "($PLUGIN_COMMAND_PREFIX:reset) error when service does not exist" {
25+
run dokku "$PLUGIN_COMMAND_PREFIX:reset" not_existing_service
26+
assert_contains "${lines[*]}" "service not_existing_service does not exist"
27+
assert_failure
28+
}

0 commit comments

Comments
 (0)