File tree Expand file tree Collapse file tree 6 files changed +87
-15
lines changed Expand file tree Collapse file tree 6 files changed +87
-15
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,7 @@ postgres:list List all postgres services
4242postgres:logs <name> [-t] Print the most recent log(s) for this service
4343postgres:promote <name> <app> Promote service <name> as DATABASE_URL in <app>
4444postgres:restart <name> Graceful shutdown and restart of the postgres service container
45+ postgres:reset <name> Delete all data in the postgres service
4546postgres:start <name> Start a previously stopped postgres service
4647postgres:stop <name> Stop a running postgres service
4748postgres: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
153154dokku 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
156160dokku postgres:destroy lolipop
157161```
Original file line number Diff line number Diff line change @@ -100,6 +100,26 @@ is_valid_service_name() {
100100 return 1
101101}
102102
103+ prompt_confirmation () {
104+ declare desc=" Prompts user to confirm destructive action"
105+ declare MESSAGE=" $1 " SERVICE=" $2 " FORCE_FLAG=" $3 "
106+
107+ if [[ " $FORCE_FLAG " == " force" ]] || [[ " $FORCE_FLAG " == " -f" ]] || [[ " $FORCE_FLAG " == " --force" ]]; then
108+ DOKKU_APPS_FORCE_DELETE=1
109+ fi
110+ if [[ -z " $DOKKU_APPS_FORCE_DELETE " ]]; then
111+ dokku_log_warn " WARNING: Potentially Destructive Action"
112+ dokku_log_warn " $MESSAGE "
113+ dokku_log_warn " To proceed, type \" $SERVICE \" "
114+ echo " "
115+
116+ read -rp " > " service_name
117+ if [[ " $service_name " != " $SERVICE " ]]; then
118+ dokku_log_fail " Confirmation did not match $SERVICE . Aborted."
119+ fi
120+ fi
121+ }
122+
103123remove_from_links_file () {
104124 declare desc=" Removes an app from the service link file"
105125 declare SERVICE=" $1 " APP=" $2 "
Original file line number Diff line number Diff 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+
124135service_start () {
125136 local SERVICE=" $1 "
126137 local QUIET=" $2 "
Original file line number Diff line number Diff 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 "
Original file line number Diff line number Diff line change 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 " $@ "
Original file line number Diff line number Diff line change 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+ }
16+
17+ @test " ($PLUGIN_COMMAND_PREFIX :reset) error when there are no arguments" {
18+ run dokku " $PLUGIN_COMMAND_PREFIX :reset"
19+ assert_contains " ${lines[*]} " " Please specify a name for the service"
20+ }
21+
22+ @test " ($PLUGIN_COMMAND_PREFIX :reset) error when service does not exist" {
23+ run dokku " $PLUGIN_COMMAND_PREFIX :reset" not_existing_service
24+ assert_contains " ${lines[*]} " " service not_existing_service does not exist"
25+ }
You can’t perform that action at this time.
0 commit comments