From c709189de4c2e525f1dc07e80fd7055ad63d865b Mon Sep 17 00:00:00 2001 From: Daniel Golle Date: Sun, 2 Jul 2017 19:38:30 +0200 Subject: [PATCH] postgresql: allow populating required DB using script Extend UCI mechanics to allow pre-populating a newly created database by executing SQL statements from a scripts file. Signed-off-by: Daniel Golle --- libs/postgresql/Makefile | 2 +- libs/postgresql/files/postgresql.sh | 48 ++++++++++++++++++++++------- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/libs/postgresql/Makefile b/libs/postgresql/Makefile index fd9ab3d772..5cd2468b39 100644 --- a/libs/postgresql/Makefile +++ b/libs/postgresql/Makefile @@ -6,7 +6,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=postgresql PKG_VERSION:=9.6.3 -PKG_RELEASE:=2 +PKG_RELEASE:=3 PKG_MAINTAINER:=Daniel Golle PKG_LICENSE:=PostgreSQL diff --git a/libs/postgresql/files/postgresql.sh b/libs/postgresql/files/postgresql.sh index 78b6ab84e9..8d4e377a4a 100644 --- a/libs/postgresql/files/postgresql.sh +++ b/libs/postgresql/files/postgresql.sh @@ -26,30 +26,56 @@ pg_server_ready() { return 1 } -# $1: dbname, $2: username, $3: password + +pg_test_db() { + if [ "$3" ]; then + echo "SHOW ALL;" | env PGPASSWORD="$3" $PSQL -U "$2" -d "$1" -q 2>/dev/null >/dev/null + return $? + else + echo "SHOW ALL;" | $PSQL -w -U "$2" -d "$1" -q 2>/dev/null >/dev/null + return $? + fi +} + +pg_include_sql() { + if [ "$3" ]; then + env PGPASSWORD="$3" $PSQL -U "$2" -d "$1" -e -f "$4" + return $? + else + $PSQL -w -U "$2" -d "$1" -e -f "$4" + return $? + fi +} + +# $1: dbname, $2: username, $3: password, $4: sql populate script pg_require_db() { + local ret + pg_test_db $@ && return 0 ( echo "CREATE DATABASE $1;" echo -n "CREATE USER $2" [ "$3" ] && echo -n " WITH PASSWORD '$3'" - echo ";" - echo "GRANT ALL PRIVILEGES ON DATABASE \"$1\" to $2;" ) | + echo " NOCREATEDB NOSUPERUSER NOCREATEROLE NOINHERIT;" + echo "GRANT ALL PRIVILEGES ON DATABASE \"$1\" TO $2;" ) | $PSQL -U postgres -d template1 -e - return $? -} + ret=$? + [ "$ret" = "0" ] || return $ret -pg_test_db() { - PGPASSWORD=$3 - echo "SHOW ALL;" | $PSQL -U $2 -d $1 -q 2>/dev/null >/dev/null - return $? + if [ "$4" ]; then + pg_include_sql "$@" + ret=$? + fi + + return $ret } uci_require_db() { - local dbname dbuser dbpass + local dbname dbuser dbpass dbscript config_get dbname $1 name config_get dbuser $1 user config_get dbpass $1 pass - pg_require_db $dbname $dbuser $dbpass + config_get dbscript $1 script + pg_require_db "$dbname" "$dbuser" "$dbpass" "$dbscript" } [ "$1" = "init" ] && { -- 2.30.2