Skip to content

Commit a2f977a

Browse files
committed
Allow to pass custom args to the createdb command
1 parent f1d60d6 commit a2f977a

File tree

8 files changed

+26
-2
lines changed

8 files changed

+26
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ dokku postgres:create <service> [--create-flags...]
6666
flags:
6767

6868
- `-c|--config-options "--args --go=here"`: extra arguments to pass to the container create command (default: `None`)
69+
- `-c|--createdb-options "--locale=C"`: extra arguments to pass to the `createdb` command inside the container (default: `-E=UTF8`)
6970
- `-C|--custom-env "USER=alpha;HOST=beta"`: semi-colon delimited environment variables to start the service with
7071
- `-i|--image IMAGE`: the image name to start the service with
7172
- `-I|--image-version IMAGE_VERSION`: the image version to start the service with
@@ -399,6 +400,7 @@ dokku postgres:upgrade <service> [--upgrade-flags...]
399400
flags:
400401

401402
- `-c|--config-options "--args --go=here"`: extra arguments to pass to the container create command (default: `None`)
403+
- `-c|--createdb-options "--locale=C"`: extra arguments to pass to the `createdb` command inside the container (default: `-E=UTF8`)
402404
- `-C|--custom-env "USER=alpha;HOST=beta"`: semi-colon delimited environment variables to start the service with
403405
- `-i|--image IMAGE`: the image name to start the service with
404406
- `-I|--image-version IMAGE_VERSION`: the image version to start the service with
@@ -469,6 +471,7 @@ dokku postgres:clone <service> <new-service> [--clone-flags...]
469471
flags:
470472

471473
- `-c|--config-options "--args --go=here"`: extra arguments to pass to the container create command (default: `None`)
474+
- `-c|--createdb-options "--locale=C"`: extra arguments to pass to the `createdb` command inside the container (default: `-E=UTF8`)
472475
- `-C|--custom-env "USER=alpha;HOST=beta"`: semi-colon delimited environment variables to start the service with
473476
- `-i|--image IMAGE`: the image name to start the service with
474477
- `-I|--image-version IMAGE_VERSION`: the image version to start the service with

bin/generate

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,8 @@ def command_help(command, service, variable, alias, image, scheme, ports, option
302302
for flag in data["flags"]:
303303
if "--config-options" in flag and options != "":
304304
flag = f"{flag} (default: `{options}`)"
305+
if "--createdb-options" in flag and options != "":
306+
flag = f"{flag} (default: `-E=UTF8`)"
305307
content.append(f"- {flag}")
306308

307309
if len(data["examples"]) > 0:

common-functions

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,10 @@ service_commit_config() {
265265
echo "" >"$SERVICE_ROOT/CONFIG_OPTIONS"
266266
fi
267267

268+
if [[ -n "$CREATEDB_OPTIONS" ]]; then
269+
echo "$CREATEDB_OPTIONS" >"$SERVICE_ROOT/CREATEDB_OPTIONS"
270+
fi
271+
268272
if [[ -n "$SERVICE_MEMORY" ]]; then
269273
echo "$SERVICE_MEMORY" >"$SERVICE_ROOT/SERVICE_MEMORY"
270274
fi
@@ -452,6 +456,7 @@ service_info() {
452456
local flag_map=(
453457
"--config-dir: ${SERVICE_ROOT}/${PLUGIN_CONFIG_SUFFIX}"
454458
"--config-options: $(cat "$SERVICE_ROOT/CONFIG_OPTIONS")"
459+
"--createdb-options: ${CREATEDB_OPTIONS}"
455460
"--data-dir: ${SERVICE_ROOT}/data"
456461
"--dsn: ${SERVICE_URL}"
457462
"--exposed-ports: $(service_exposed_ports "$SERVICE")"
@@ -606,6 +611,7 @@ service_parse_args() {
606611
case "$arg" in
607612
"--alias") set -- "$@" "-a" ;;
608613
"--config-options") set -- "$@" "-c" ;;
614+
"--createdb-options") set -- "$@" "-o" ;;
609615
"--custom-env") set -- "$@" "-C" ;;
610616
"--database") set -- "$@" "-d" ;;
611617
"--image-version") set -- "$@" "-I" ;;
@@ -622,7 +628,7 @@ service_parse_args() {
622628
done
623629

624630
OPTIND=1
625-
while getopts "a:c:C:d:i:I:m:p:q:R:r:s:u:" opt; do
631+
while getopts "a:c:o:C:d:i:I:m:p:q:R:r:s:u:" opt; do
626632
case "$opt" in
627633
a)
628634
SERVICE_ALIAS="${OPTARG^^}"
@@ -631,6 +637,9 @@ service_parse_args() {
631637
c)
632638
export PLUGIN_CONFIG_OPTIONS=$OPTARG
633639
;;
640+
o)
641+
export CREATEDB_OPTIONS=$OPTARG
642+
;;
634643
C)
635644
export SERVICE_CUSTOM_ENV=$OPTARG
636645
;;

functions

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ service_create_container() {
7373
export CONFIG_OPTIONS="$(cat "$SERVICE_ROOT/CONFIG_OPTIONS")"
7474
fi
7575

76+
if [[ -f "$SERVICE_ROOT/CREATEDB_OPTIONS" ]]; then
77+
export CREATEDB_OPTIONS="$(cat "$SERVICE_ROOT/CREATEDB_OPTIONS")"
78+
fi
79+
7680
[[ -f "$SERVICE_ROOT/SERVICE_MEMORY" ]] && SERVICE_MEMORY="$(cat "$SERVICE_ROOT/SERVICE_MEMORY")"
7781
if [[ -n "$SERVICE_MEMORY" ]]; then
7882
MEMORY_LIMIT="--memory=${SERVICE_MEMORY}m"
@@ -94,7 +98,7 @@ service_create_container() {
9498
docker run --rm --link "$SERVICE_NAME:$PLUGIN_COMMAND_PREFIX" "$PLUGIN_WAIT_IMAGE" -p "$PLUGIN_DATASTORE_WAIT_PORT" >/dev/null
9599

96100
dokku_log_verbose_quiet "Creating container database"
97-
docker exec "$SERVICE_NAME" su - postgres -c "createdb -E utf8 $DATABASE_NAME" 2>/dev/null || dokku_log_verbose_quiet 'Already exists'
101+
docker exec "$SERVICE_NAME" su - postgres -c "createdb -E=UTF8 $CREATEDB_OPTIONS $DATABASE_NAME" 2>/dev/null || dokku_log_verbose_quiet 'Already exists'
98102

99103
dokku_log_verbose_quiet "Securing connection to database"
100104
service_stop "$SERVICE" >/dev/null

subcommands/clone

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ service-clone-cmd() {
1111
#A service, service to run command against
1212
#A new-service, name of new service
1313
#F -c|--config-options "--args --go=here", extra arguments to pass to the container create command
14+
#F -c|--createdb-options "--locale=C", extra arguments to pass to the `createdb` command inside the container
1415
#F -C|--custom-env "USER=alpha;HOST=beta", semi-colon delimited environment variables to start the service with
1516
#F -i|--image IMAGE, the image name to start the service with
1617
#F -I|--image-version IMAGE_VERSION, the image version to start the service with

subcommands/create

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ service-create-cmd() {
1919
#E dokku $PLUGIN_COMMAND_PREFIX:create lollipop
2020
#A service, service to run command against
2121
#F -c|--config-options "--args --go=here", extra arguments to pass to the container create command
22+
#F -c|--createdb-options "--locale=C", extra arguments to pass to the `createdb` command inside the container
2223
#F -C|--custom-env "USER=alpha;HOST=beta", semi-colon delimited environment variables to start the service with
2324
#F -i|--image IMAGE, the image name to start the service with
2425
#F -I|--image-version IMAGE_VERSION, the image version to start the service with

subcommands/upgrade

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ service-upgrade-cmd() {
1111
#E dokku $PLUGIN_COMMAND_PREFIX:upgrade lollipop
1212
#A service, service to run command against
1313
#F -c|--config-options "--args --go=here", extra arguments to pass to the container create command
14+
#F -c|--createdb-options "--locale=C", extra arguments to pass to the `createdb` command inside the container
1415
#F -C|--custom-env "USER=alpha;HOST=beta", semi-colon delimited environment variables to start the service with
1516
#F -i|--image IMAGE, the image name to start the service with
1617
#F -I|--image-version IMAGE_VERSION, the image version to start the service with

tests/service_info.bats

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ teardown() {
4141
run dokku "$PLUGIN_COMMAND_PREFIX:info" l --config-dir
4242
assert_success
4343

44+
run dokku "$PLUGIN_COMMAND_PREFIX:info" l --createdb-options
45+
assert_success
46+
4447
run dokku "$PLUGIN_COMMAND_PREFIX:info" l --data-dir
4548
assert_success
4649

0 commit comments

Comments
 (0)