Browse Source

new: [postgres] extensions are now an option list

framadate
Valentin Lab 6 years ago
parent
commit
b0ddad8997
  1. 15
      postgres/hooks/postgres_database-relation-joined
  2. 23
      postgres/lib/common

15
postgres/hooks/postgres_database-relation-joined

@ -45,20 +45,23 @@ else
fi
POSTGIS=$(relation-get postgis 2>/dev/null) || true
UNACCENT=$(relation-get unaccent 2>/dev/null) || true
array_read-0 extensions < <(relation-get extensions 2>/dev/null | shyaml get-values-0)
ensure_db_docker_running
## XXXvlab: should send all these into only one docker...
db_has_database "$DBNAME" || UNACCENT="$UNACCENT" POSTGIS="$POSTGIS" db_create "$DBNAME"
if ! db_has_database "$DBNAME"; then
db_create "$DBNAME" || exit 1
fi
if [ "${#extensions[@]}" -gt 0 ]; then
db_install_extensions "$DBNAME" "${extensions[@]}" || exit 1
fi
if ! db_has_user "$USER"; then
info "Creating a new user $USER."
db_create_user "$USER" "$PASSWORD"
db_create_user "$USER" "$PASSWORD" || exit 1
else
info "Updating password of user $USER."
db_change_password "$USER" "$PASSWORD"
db_change_password "$USER" "$PASSWORD" || exit 1
fi
db_grant_rights "$DBNAME" "$USER"

23
postgres/lib/common

@ -44,18 +44,27 @@ db_create () {
local dbname="$1"
dcmd createdb "$dbname" || return 1
info "Database '$dbname' created."
if [ "$POSTGIS" ]; then
}
db_install_extensions() {
local dbname="$1"
shift
while [ "$#" != 0 ]; do
case "$1" in
postgis)
ddb -d "$dbname" < <(echo "CREATE EXTENSION postgis; CREATE EXTENSION postgis_topology;") || return 1
dcmd /bin/bash -c "psql -d '$dbname' -f /usr/share/postgresql/*/contrib/postgis-2.1/legacy.sql" || return 1
info "Installed postgis extensions on database '$dbname'."
fi
if [ "$UNACCENT" ]; then
ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS unaccent;") || return 1
info "Installed unaccent extension on database '$dbname'."
fi
;;
*)
ddb -d "$dbname" < <(echo "CREATE EXTENSION IF NOT EXISTS $1;") || return 1
info "Installed $1 extension on database '$dbname'."
;;
esac
shift
done
}
## XXXvlab: if launched first, it'll fail handling correctly the open/close of docker
db_has_database() {
local dbname="$1"

Loading…
Cancel
Save