diff --git a/postgres/hooks/postgres_database-relation-joined b/postgres/hooks/postgres_database-relation-joined index c560fe2..35d61f5 100755 --- a/postgres/hooks/postgres_database-relation-joined +++ b/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" diff --git a/postgres/lib/common b/postgres/lib/common index d78c259..34e351b 100644 --- a/postgres/lib/common +++ b/postgres/lib/common @@ -44,17 +44,26 @@ db_create () { local dbname="$1" dcmd createdb "$dbname" || return 1 info "Database '$dbname' created." - if [ "$POSTGIS" ]; then - 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 } +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'." + ;; + *) + 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() {