Hello Eli Mesika, I'd like you to do a code review. Please visit
http://gerrit.ovirt.org/21277 to review the following change. Change subject: core: adding utility for db objects owner change ...................................................................... core: adding utility for db objects owner change This utility change objects in the engine database to be owned by the engine user. Related-To: https://bugzilla.redhat.com/1022691 Change-Id: I56c59c0fe749389bd110bcd1a39faae74e71174b Signed-off-by: Eli Mesika <emes...@redhat.com> --- M packaging/dbscripts/common_sp.sql M packaging/dbscripts/dbfunctions.sh A packaging/setup/dbutils/changedbowner.sh 3 files changed, 65 insertions(+), 3 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/77/21277/1 diff --git a/packaging/dbscripts/common_sp.sql b/packaging/dbscripts/common_sp.sql index 567c391..63df62d 100644 --- a/packaging/dbscripts/common_sp.sql +++ b/packaging/dbscripts/common_sp.sql @@ -257,7 +257,7 @@ Create or replace FUNCTION generate_drop_all_functions_syntax() RETURNS SETOF text STABLE AS $procedure$ BEGIN -RETURN QUERY select 'drop function if exists ' || ns.nspname || '.' || proname || '(' || oidvectortypes(proargtypes) || ') cascade;' from pg_proc inner join pg_namespace ns on (pg_proc.pronamespace=ns.oid) where ns.nspname = 'public' and proname not ilike 'uuid%' order by proname; +RETURN QUERY select 'drop function if exists ' || ns.nspname || '.' || proname || '(' || oidvectortypes(proargtypes) || ') cascade;' from pg_proc inner join pg_namespace ns on (pg_proc.pronamespace=ns.oid) where ns.nspname = 'public' order by proname; END; $procedure$ LANGUAGE plpgsql; diff --git a/packaging/dbscripts/dbfunctions.sh b/packaging/dbscripts/dbfunctions.sh index 2125fc6..2a470e5 100755 --- a/packaging/dbscripts/dbfunctions.sh +++ b/packaging/dbscripts/dbfunctions.sh @@ -82,8 +82,6 @@ CMD="select * from generate_drop_all_functions_syntax();" execute_command "$CMD" ${DATABASE} ${SERVERNAME} ${PORT} >> $file execute_file "${file}" ${DATABASE} ${SERVERNAME} ${PORT} > /dev/null - CMD="DROP FUNCTION IF EXISTS uuid_generate_v1();" - execute_command "$CMD" ${DATABASE} ${SERVERNAME} ${PORT} > /dev/null } #drops views before upgrade or refresh operations diff --git a/packaging/setup/dbutils/changedbowner.sh b/packaging/setup/dbutils/changedbowner.sh new file mode 100755 index 0000000..3850b62 --- /dev/null +++ b/packaging/setup/dbutils/changedbowner.sh @@ -0,0 +1,64 @@ +#!/bin/sh +################################################################ +# This script change the ownership of objects in the ${DATABASE} +# database from ${FROM_USER} to ${TO_USER} +################################################################ + +#include db general functions +cd "$(dirname "$0")" +source ./common.sh + +#setting defaults +set_defaults + +usage() { + cat << __EOF__ +Usage: ${ME} [-h] [-s SERVERNAME [-p PORT]] [-d DATABASE] -f FROM_USER -t TO_USER + -s SERVERNAME - The database servername for the database (def. ${SERVERNAME}) + -p PORT - The database port for the database (def. ${PORT}) + -d DATABASE - The database name (def. ${DATABASE}) + -f FROM_USER - The current owner for the database + -t TO_USER - The new owner for the database + -h - This help text. +__EOF__ + exit $ret +} + + +while getopts hs:p:d:f:t: option; do + case $option in + s) SERVERNAME=$OPTARG;; + p) PORT=$OPTARG;; + d) DATABASE=$OPTARG;; + f) FROM_USER=$OPTARG;; + t) TO_USER=$OPTARG;; + h) ret=0 && usage;; + \?) ret=1 && usage;; + esac +done + +if [[ ! -n "${FROM_USER}" || ! -n "${TO_USER}" ]]; then + usage +fi + +# Change all schema objects ownership +echo "Changing database ${DATABASE} objects ownership" +pg_dump -s -h "${SERVERNAME}" -p ${PORT} -U ${FROM_USER} "${DATABASE}" | grep -i 'owner to' | sed "s/OWNER TO ${FROM_USER};/OWNER TO ${TO_USER};/i" | psql -h "${SERVERNAME}" -p ${PORT} -U ${FROM_USER} "${DATABASE}" + +if [[ $PIPESTATUS -ne 0 ]]; then + echo "Failed to change DB ${DATABASE} objects ownership." + exit 1 +fi + +#change the DB ownership +echo "Changing database ${DATABASE} ownership" +cmd="ALTER DATABASE ${DATABASE} OWNER TO ${TO_USER};" +psql -w -h "${SERVERNAME}" -p ${PORT} --pset=tuples_only=on --set ON_ERROR_STOP=1 -c "${cmd}" -U ${FROM_USER} -d "${DATABASE}" + +if [ $? -ne 0 ]; then + echo "Failed to change DB ${DATABASE} ownership." + exit 2 +fi + +echo "Changing database ${DATABASE} ownership from ${FROM_USER} to ${TO_USER} completed successfully." +exit 0 -- To view, visit http://gerrit.ovirt.org/21277 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I56c59c0fe749389bd110bcd1a39faae74e71174b Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: ovirt-engine-3.3 Gerrit-Owner: Alon Bar-Lev <alo...@redhat.com> Gerrit-Reviewer: Eli Mesika <emes...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches