@ -5,21 +5,21 @@ PSQL="/usr/bin/psql"
free_megs( ) {
free_megs( ) {
fsdir = $1
fsdir = $1
while [ ! -d " $fsdir " ] ; do
while [ ! -d " $fsdir " ] ; do
fsdir = $( dirname $fsdir )
fsdir = " $( dirname " $fsdir " ) "
done
done
df -m $fsdir | while read fs bl us av cap mnt; do [ " $av " = "Available" ] || echo $av ; done
df -m $fsdir | while read fs bl us av cap mnt; do [ " $av " = "Available" ] || echo $av ; done
}
}
pg_init_data( ) {
pg_init_data( ) {
# make sure we got at least 50MB of free space
# make sure we got at least 50MB of free space
[ $( free_megs $1 ) -lt 50 ] && return 1
pg_ctl initdb -U postgres -D $1
[ $( free_megs " $1 " ) -lt 50 ] && return 1
pg_ctl initdb -U postgres -D " $1 "
}
}
pg_server_ready( ) {
pg_server_ready( ) {
t = 0
t = 0
while [ $t -le 90 ] ; do
while [ $t -le 90 ] ; do
pg_ctl status -U postgres -D $1 2>/dev/null >/dev/null && return 0
psql -h /var/run/postgresql/ -U postgres -c "\q" 1>/dev/null 2 >/dev/null && return 0
t = $(( t+1))
t = $(( t+1))
sleep 1
sleep 1
done
done
@ -28,21 +28,19 @@ pg_server_ready() {
pg_test_db( ) {
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
echo " SELECT datname FROM pg_catalog.pg_database WHERE datname = ' $1 '; " |
$PSQL -h /var/run/postgresql -w -U "postgres" -d "template1" -q |
grep -q "0 rows" && return 1
return 0
}
}
pg_include_sql( ) {
pg_include_sql( ) {
if [ " $3 " ] ; then
if [ " $3 " ] ; then
env PGPASSWORD = " $3 " $PSQL -U " $2 " -d " $1 " -e -f " $4 "
env PGPASSWORD = " $3 " $PSQL -h /var/run/postgresql - U " $2 " -d " $1 " -e -f " $4 "
return $?
return $?
else
else
$PSQL -w -U " $2 " -d " $1 " -e -f " $4 "
$PSQL -w -h /var/run/postgresql - U " $2 " -d " $1 " -e -f " $4 "
return $?
return $?
fi
fi
}
}
@ -50,22 +48,37 @@ pg_include_sql() {
# $1: dbname, $2: username, $3: password, $4: sql populate script
# $1: dbname, $2: username, $3: password, $4: sql populate script
pg_require_db( ) {
pg_require_db( ) {
local ret
local ret
local dbname = " $1 "
local dbuser = " $2 "
local dbpass = " $3 "
local exuser
pg_test_db $@ && return 0
pg_test_db $@ && return 0
( echo " CREATE DATABASE $1 ; "
echo -n " CREATE USER $2 "
[ " $3 " ] && echo -n " WITH PASSWORD ' $3 ' "
echo " NOCREATEDB NOSUPERUSER NOCREATEROLE NOINHERIT;"
echo " GRANT ALL PRIVILEGES ON DATABASE \" $1 \" TO $2 ; " ) |
$PSQL -U postgres -d template1 -e
ret = $?
[ " $ret " = "0" ] || return $ret
if [ " $4 " ] ; then
pg_include_sql " $@ "
ret = $?
shift ; shift ; shift
echo " CREATE DATABASE $dbname ; " |
$PSQL -h /var/run/postgresql -U postgres -d template1 -e || return $?
if [ " $dbuser " ] ; then
echo " SELECT usename FROM pg_catalog.pg_user WHERE usename = ' $dbuser '; " |
$PSQL -h /var/run/postgresql -U postgres -d template1 -e | grep -q "0 rows" &&
( echo -n " CREATE USER $dbuser "
[ " $dbpass " ] && echo -n " WITH PASSWORD ' $dbpass ' "
echo " NOCREATEDB NOSUPERUSER NOCREATEROLE NOINHERIT;" ) |
$PSQL -h /var/run/postgresql -U postgres -d template1 -e
echo " GRANT ALL PRIVILEGES ON DATABASE \" $dbname \" TO $dbuser ; " |
$PSQL -h /var/run/postgresql -U postgres -d template1 -e
fi
fi
while [ " $1 " ] ; do
pg_include_sql " $dbname " " $dbuser " " $dbpass " " $1 "
ret = $?
[ $ret != 0 ] && break
shift
done
return $ret
return $ret
}
}
@ -75,7 +88,7 @@ uci_require_db() {
config_get dbuser $1 user
config_get dbuser $1 user
config_get dbpass $1 pass
config_get dbpass $1 pass
config_get dbscript $1 script
config_get dbscript $1 script
pg_require_db " $dbname " " $dbuser " " $dbpass " " $dbscript "
pg_require_db " $dbname " " $dbuser " " $dbpass " $dbscript
}
}
[ " $1 " = "init" ] && {
[ " $1 " = "init" ] && {