Merge pull request #63 from dokku/database-adapter
Read POSTGRES_DATABASE_SCHEME variable from app when setting DATABASE_URL
This commit is contained in:
27
README.md
27
README.md
@@ -130,6 +130,33 @@ dokku postgres:clone lolipop new_database
|
|||||||
dokku postgres:destroy lolipop
|
dokku postgres:destroy lolipop
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Changing database adapter
|
||||||
|
|
||||||
|
It's possible to change the protocol for DATABASE_URL by setting
|
||||||
|
the environment variable POSTGRES_DATABASE_SCHEME on the app:
|
||||||
|
|
||||||
|
```
|
||||||
|
dokku config:set playground POSTGRES_DATABASE_SCHEME=postgres2
|
||||||
|
dokku postgres:link lolipop playground
|
||||||
|
```
|
||||||
|
|
||||||
|
Will cause DATABASE_URL to be set as
|
||||||
|
postgres2://postgres:SOME_PASSWORD@dokku-postgres-lolipop:5432/lolipop
|
||||||
|
|
||||||
|
CAUTION: Changing POSTGRES_DATABASE_SCHEME after linking will cause dokku to
|
||||||
|
believe the postgres is not linked when attempting to use `dokku postgres:unlink`
|
||||||
|
or `dokku postgres:promote`.
|
||||||
|
You should be able to fix this by
|
||||||
|
|
||||||
|
- Changing DATABASE_URL manually to the new value.
|
||||||
|
|
||||||
|
OR
|
||||||
|
|
||||||
|
- Set POSTGRES_DATABASE_SCHEME back to its original setting
|
||||||
|
- Unlink the service
|
||||||
|
- Change POSTGRES_DATABASE_SCHEME to the desired setting
|
||||||
|
- Relink the service
|
||||||
|
|
||||||
## upgrade/downgrade
|
## upgrade/downgrade
|
||||||
|
|
||||||
At the moment a database can’t be upgraded (or downgraded) inplace. Instead a clone has to be made, like this:
|
At the moment a database can’t be upgraded (or downgraded) inplace. Instead a clone has to be made, like this:
|
||||||
|
|||||||
10
functions
10
functions
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
set -eo pipefail; [[ $DOKKU_TRACE ]] && set -x
|
||||||
source "$(dirname "$0")/config"
|
source "$(dirname "$0")/config"
|
||||||
|
source "$PLUGIN_AVAILABLE_PATH/config/functions"
|
||||||
|
|
||||||
get_random_ports() {
|
get_random_ports() {
|
||||||
local iterations="${1:-1}"
|
local iterations="${1:-1}"
|
||||||
@@ -70,6 +71,7 @@ service_exposed_ports() {
|
|||||||
service_link() {
|
service_link() {
|
||||||
local APP="$2"
|
local APP="$2"
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
|
update_plugin_scheme_for_app "$APP"
|
||||||
local SERVICE_URL=$(service_url "$SERVICE")
|
local SERVICE_URL=$(service_url "$SERVICE")
|
||||||
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
||||||
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
local SERVICE_ROOT="$PLUGIN_DATA_ROOT/$SERVICE"
|
||||||
@@ -253,6 +255,7 @@ service_stop() {
|
|||||||
service_unlink() {
|
service_unlink() {
|
||||||
local APP="$2"
|
local APP="$2"
|
||||||
local SERVICE="$1"
|
local SERVICE="$1"
|
||||||
|
update_plugin_scheme_for_app "$APP"
|
||||||
local SERVICE_URL=$(service_url "$SERVICE")
|
local SERVICE_URL=$(service_url "$SERVICE")
|
||||||
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
local SERVICE_NAME=$(get_service_name "$SERVICE")
|
||||||
local EXISTING_CONFIG=$(dokku config "$APP")
|
local EXISTING_CONFIG=$(dokku config "$APP")
|
||||||
@@ -316,6 +319,7 @@ promote() {
|
|||||||
local APP="$2"
|
local APP="$2"
|
||||||
local PLUGIN_DEFAULT_CONFIG_VAR="${PLUGIN_DEFAULT_ALIAS}_URL"
|
local PLUGIN_DEFAULT_CONFIG_VAR="${PLUGIN_DEFAULT_ALIAS}_URL"
|
||||||
local EXISTING_CONFIG=$(dokku config "$APP")
|
local EXISTING_CONFIG=$(dokku config "$APP")
|
||||||
|
update_plugin_scheme_for_app "$APP"
|
||||||
local SERVICE_URL=$(service_url "$SERVICE")
|
local SERVICE_URL=$(service_url "$SERVICE")
|
||||||
local CONFIG_VARS=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
|
local CONFIG_VARS=($(echo "$EXISTING_CONFIG" | grep "$SERVICE_URL" | cut -d: -f1)) || true
|
||||||
local PREVIOUS_DEFAULT_URL=$(get_url_from_config "$EXISTING_CONFIG" "$PLUGIN_DEFAULT_CONFIG_VAR")
|
local PREVIOUS_DEFAULT_URL=$(get_url_from_config "$EXISTING_CONFIG" "$PLUGIN_DEFAULT_CONFIG_VAR")
|
||||||
@@ -357,3 +361,9 @@ service_linked_apps() {
|
|||||||
|
|
||||||
tr '\n' ' ' < "$LINKS_FILE"
|
tr '\n' ' ' < "$LINKS_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
update_plugin_scheme_for_app() {
|
||||||
|
local APP=$1
|
||||||
|
local POSTGRES_DATABASE_SCHEME=$(config_get $APP POSTGRES_DATABASE_SCHEME)
|
||||||
|
PLUGIN_SCHEME=${POSTGRES_DATABASE_SCHEME:-$PLUGIN_SCHEME}
|
||||||
|
}
|
||||||
|
|||||||
@@ -59,3 +59,12 @@ teardown() {
|
|||||||
assert_contains "${lines[*]}" "--link dokku.postgres.l:dokku-postgres-l"
|
assert_contains "${lines[*]}" "--link dokku.postgres.l:dokku-postgres-l"
|
||||||
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
|
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test "($PLUGIN_COMMAND_PREFIX:link) uses apps POSTGRES_DATABASE_SCHEME variable" {
|
||||||
|
dokku config:set my_app POSTGRES_DATABASE_SCHEME=postgres2
|
||||||
|
dokku "$PLUGIN_COMMAND_PREFIX:link" l my_app
|
||||||
|
url=$(dokku config:get my_app DATABASE_URL)
|
||||||
|
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
|
||||||
|
assert_contains "$url" "postgres2://postgres:$password@dokku-postgres-l:5432/l"
|
||||||
|
dokku "$PLUGIN_COMMAND_PREFIX:unlink" l my_app
|
||||||
|
}
|
||||||
|
|||||||
@@ -53,3 +53,10 @@ teardown() {
|
|||||||
run dokku config my_app
|
run dokku config my_app
|
||||||
assert_contains "${lines[*]}" "DOKKU_POSTGRES_"
|
assert_contains "${lines[*]}" "DOKKU_POSTGRES_"
|
||||||
}
|
}
|
||||||
|
@test "($PLUGIN_COMMAND_PREFIX:promote) uses POSTGRES_DATABASE_SCHEME variable" {
|
||||||
|
password="$(cat "$PLUGIN_DATA_ROOT/l/PASSWORD")"
|
||||||
|
dokku config:set my_app "POSTGRES_DATABASE_SCHEME=postgres2" "DATABASE_URL=postgres://u:p@host:5432/db" "DOKKU_POSTGRES_BLUE_URL=postgres2://postgres:$password@dokku-postgres-l:5432/l"
|
||||||
|
dokku "$PLUGIN_COMMAND_PREFIX:promote" l my_app
|
||||||
|
url=$(dokku config:get my_app DATABASE_URL)
|
||||||
|
assert_contains "$url" "postgres2://postgres:$password@dokku-postgres-l:5432/l"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user