From: Thomas Markwalder Date: Fri, 12 Jun 2015 13:03:11 +0000 (-0400) Subject: [3884] Clean up PostgreSQL admin functions X-Git-Tag: trac3830_base~13^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=f34101ecf40b2b7a39294d1dd43bf934a55c04b4;p=thirdparty%2Fkea.git [3884] Clean up PostgreSQL admin functions src/bin/admin/admin-utils.sh Revamped the PostgreSQL functions to mirror changes made to MySQL functions. Primarily, no longer capturing psql output in an environment variable. This provides greater flexibility in the caller. --- diff --git a/src/bin/admin/admin-utils.sh b/src/bin/admin/admin-utils.sh index 6edc7392ac..b118bdfaf0 100644 --- a/src/bin/admin/admin-utils.sh +++ b/src/bin/admin/admin-utils.sh @@ -16,15 +16,13 @@ # There are two ways of calling this method. # mysql_execute SQL_QUERY - This call is simpler, but requires db_user, -# db_password and db_name variables to be bet. +# db_password and db_name variables to be set. # mysql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters # may be specified. They are passed directly to mysql. This one is # more convenient to use if the script didn't parse db_user db_password # and db_name. # -# It saves mysql command exit status both to the env variable _ADMIN_STATUS -# as well as returning it as $? to the caller. - +# It returns the mysql command exit status to the caller as $? mysql_execute() { if [ $# -gt 1 ]; then QUERY="$1" @@ -44,29 +42,49 @@ mysql_version() { return $? } +# Submits given SQL text to PostgreSQL +# There are two ways of calling this method. +# pgsql_execute SQL_QUERY - This call is simpler, but requires db_user, +# db_password and db_name variables to be set. +# pgsql_execute SQL_QUERY PARAM1 PARAM2 .. PARAMN - Additional parameters +# may be specified. They are passed directly to mysql. This one is +# more convenient to use if the script didn't parse db_user db_password +# and db_name. +# +# It returns the mysql command exit status to the caller as $? pgsql_execute() { QUERY=$1 shift if [ $# -gt 0 ]; then - _RESULT=$(echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q $*) + echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q $* retcode=$? else export PGPASSWORD=$db_password - _RESULT=$(echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name) + echo $QUERY | psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name retcode=$? fi return $retcode } +# Submits SQL in a given file to PostgreSQL +# There are two ways of calling this method. +# pgsql_execute SQL_FILE - This call is simpler, but requires db_user, +# db_password and db_name variables to be set. +# pgsql_execute SQL_FILE PARAM1 PARAM2 .. PARAMN - Additional parameters +# may be specified. They are passed directly to mysql. This one is +# more convenient to use if the script didn't parse db_user db_password +# and db_name. +# +# It returns the mysql command exit status to the caller as $? pgsql_execute_script() { file=$1 shift if [ $# -gt 0 ]; then - _RESULT=$(psql --set ON_ERROR_STOP=1 -A -t -q -f $file $*) + psql --set ON_ERROR_STOP=1 -A -t -q -f $file $* retcode=$? else export PGPASSWORD=$db_password - _RESULT=$(psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name -f $file) + psql --set ON_ERROR_STOP=1 -A -t -q -U $db_user -d $db_name -f $file retcode=$? fi return $retcode @@ -77,10 +95,3 @@ pgsql_version() { pgsql_execute "SELECT version || '.' || minor FROM schema_version" "$@" return $? } - -pgsql_version_print() { - pgsql_version "$@" - retcode=$? - printf "%s" $_RESULT - return $? -} diff --git a/src/bin/admin/kea-admin.in b/src/bin/admin/kea-admin.in index 5b964101b7..07173309a1 100644 --- a/src/bin/admin/kea-admin.in +++ b/src/bin/admin/kea-admin.in @@ -180,26 +180,31 @@ pgsql_init() { # Let's try to count the number of tables. Anything above 0 means that there # is some database in place. If there is anything, we abort. - pgsql_execute "\d" - COUNT=`echo "$_RESULT" | wc -w` + RESULT=`pgsql_execute "\d"` + ERRCODE=$? + if [ "$ERRCODE" -ne 0 ]; then + log_error "pgsql_init: table query failed, status code: $ERRCODE?" + exit 1 + fi + + COUNT=`echo "$RESULT" | wc -w` if [ $COUNT -gt 0 ]; then printf "\n" - log_error "Expected empty database $db_name, but the following tables are present \n$_RESULT. Aborting." + log_error "Expected empty database $db_name, but the following tables are present \n$RESULT. Aborting." exit 2 fi init_script="$scripts_dir/pgsql/dhcpdb_create.pgsql" printf "Initializing database using script %s\n" $init_script - pgsql_execute_script $init_script + RESULT=`pgsql_execute_script $init_script` ERRCODE=$? if [ "$ERRCODE" -ne 0 ]; then log_error "Database initialization failed, status code: $ERRCODE?" exit 1 fi - printf "Lease DB version reported after initialization: " - pgsql_version_print - printf "\n" + version=`pgsql_version` + printf "Lease DB version reported after initialization: $version\n" exit 0 } @@ -254,10 +259,8 @@ mysql_upgrade() { } pgsql_upgrade() { - # @todo - When PostgreSQL has a schema greater than 1.0, this will need - # to be implemented. See ticket #3600 - pgsql_version_print - printf "\n" + version=`pgsql_version` + printf "Lease DB version reported before upgrade: $version\n" # Check if the scripts directory exists at all. if [ ! -d ${scripts_dir}/pgsql ]; then @@ -278,9 +281,8 @@ pgsql_upgrade() { sh ${script} --user=${db_user} --password=${db_password} ${db_name} done - printf "Lease DB version reported after upgrade: " - pgsql_version_print - printf "\n" + version=`pgsql_version` + printf "Lease DB version reported after upgrade: $version\n" exit 0 } @@ -410,9 +412,7 @@ mysql_dump() { ### Functions used for dump pgsql_dump() { - - # @todo use pgsql_version once implemented. See #3883 - version='1.0' + version=`pgsql_version` get_dump_query $version # Make sure they specified a file @@ -579,7 +579,7 @@ case ${command} in printf "\n" ;; pgsql) - pgsql_version_print + pgsql_version ;; esac ;; diff --git a/src/bin/admin/tests/pgsql_tests.sh.in b/src/bin/admin/tests/pgsql_tests.sh.in index 27eb97a41c..097e1ca70f 100644 --- a/src/bin/admin/tests/pgsql_tests.sh.in +++ b/src/bin/admin/tests/pgsql_tests.sh.in @@ -43,10 +43,10 @@ pgsql_wipe() { export PGPASSWORD=$db_password # Make a set of drop commands, one for each table owned by keatest - pgsql_execute "SELECT 'drop table if exists '||t.tablename || ' cascade;' as dcmd FROM pg_catalog.pg_tables t WHERE t.tableowner = 'keatest';" + RESULT=`pgsql_execute "SELECT 'drop table if exists '||t.tablename || ' cascade;' as dcmd FROM pg_catalog.pg_tables t WHERE t.tableowner = 'keatest';"` assert_eq 0 $? "pgsql_wipe select failed, expected exit code: %d, actual: %d" # Now execute the set of drop commands from the result set returned - pgsql_execute "$_RESULT" + RESULT=`pgsql_execute "$RESULT"` assert_eq 0 $? "pgsql_wipe drop failed, expected exit code: %d, actual: %d" } @@ -63,19 +63,19 @@ pgsql_lease_init_test() { # Verify that all the expected tables exist # Check schema_version table - pgsql_execute "SELECT version, minor FROM schema_version;" + RESULT=`pgsql_execute "SELECT version, minor FROM schema_version;"` assert_eq 0 $? "schema_vesion table check failed, expected exit code: %d, actual: %d" # Check lease4 table - pgsql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname FROM lease4;" + RESULT=`pgsql_execute "SELECT address, hwaddr, client_id, valid_lifetime, expire, subnet_id, fqdn_fwd, fqdn_rev, hostname FROM lease4;"` assert_eq 0 $? "lease4 table check failed, expected exit code: %d, actual: %d" # Check lease6 table - pgsql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname FROM lease6;" + RESULT=`pgsql_execute "SELECT address, duid, valid_lifetime, expire, subnet_id, pref_lifetime, lease_type, iaid, prefix_len, fqdn_fwd, fqdn_rev, hostname FROM lease6;"` assert_eq 0 $? "lease6 table check failed, expected exit code: %d, actual: %d" # Check lease6_types table - pgsql_execute "SELECT lease_type, name FROM lease6_types;" + RESULT=`pgsql_execute "SELECT lease_type, name FROM lease6_types;"` assert_eq 0 $? "lease6_types table check failed, expected exit code: %d, actual: %d" # Trying to create it again should fail. This verifies the db present