Alon Bar-Lev has uploaded a new change for review. Change subject: db: cleanup: rename function names ......................................................................
db: cleanup: rename function names dbfunc_common_ - public interface dbfunc_common_hook_ - hooks _dbfunc_common_ - private custom functions are provided as hooks, the default implementation a void hook, custom can override it. Change-Id: Icce88f1d8f19da83c757f94ad661cb676bd70274 Signed-off-by: Alon Bar-Lev <[email protected]> --- M packaging/dbscripts/cleandb.sh M packaging/dbscripts/create_schema.sh M packaging/dbscripts/dbfunc-common.sh M packaging/dbscripts/dbfunc-custom.sh M packaging/dbscripts/refreshStoredProcedures.sh M packaging/dbscripts/restore.sh M packaging/dbscripts/unlock_entity.sh M packaging/dbscripts/upgrade.sh 8 files changed, 133 insertions(+), 104 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/16/24916/1 diff --git a/packaging/dbscripts/cleandb.sh b/packaging/dbscripts/cleandb.sh index 54a7bf3..81cd3e3 100755 --- a/packaging/dbscripts/cleandb.sh +++ b/packaging/dbscripts/cleandb.sh @@ -49,7 +49,7 @@ done echo "Cleaning database..." -cleandb +dbfunc_common_cleandb ret=$? echo "Done." diff --git a/packaging/dbscripts/create_schema.sh b/packaging/dbscripts/create_schema.sh index 2fd16ba..7005cab 100755 --- a/packaging/dbscripts/create_schema.sh +++ b/packaging/dbscripts/create_schema.sh @@ -62,11 +62,11 @@ dbfunc_psql_die --file="common_sp.sql" > /dev/null #inserting initial data -insert_initial_data +dbfunc_common_hook_insert_initial_data #remove checksum file in clean install in order to run views/sp creation [ -n "${DBFUNC_MD5DIR}" ] && rm -f "${DBFUNC_MD5DIR}/.${DBFUNC_DB_DATABASE}.scripts.md5" > /dev/null 2>&1 # Running upgrade scripts echo "Running upgrade scripts..." -run_upgrade_files +dbfunc_common_upgrade diff --git a/packaging/dbscripts/dbfunc-common.sh b/packaging/dbscripts/dbfunc-common.sh index 285bd40..a871eac 100644 --- a/packaging/dbscripts/dbfunc-common.sh +++ b/packaging/dbscripts/dbfunc-common.sh @@ -1,8 +1,32 @@ . ./dbfunc-base.sh +dbfunc_common_hook_insert_initial_data() { + return 0 +} + +dbfunc_common_hook_refresh_views() { + return 0 +} + +dbfunc_common_hook_install_materialized_views_func() { + return 0 +} + +dbfunc_common_hook_drop_materialized_views() { + return 0 +} + +dbfunc_common_hook_refresh_materialized_views() { + return 0 +} + +dbfunc_common_hook_update_sequence_numbers() { + return 0 +} + #cleans db by dropping all objects -cleandb() { +dbfunc_common_cleandb() { # common stored procedures are executed first (for new added functions to be valid) local file="${DBFUNC_TMPDIR}/cleandb.sql" @@ -17,8 +41,12 @@ dbfunc_psql_die --file="${file}" > /dev/null } +dbfunc_common_insert_initial_data() { + dbfunc_common_hook_insert_initial_data +} + #drops views before upgrade or refresh operations -drop_views() { +dbfunc_common_drop_views() { local file="${DBFUNC_TMPDIR}/drop_views.sql" # common stored procedures are executed first (for new added functions to be valid) @@ -28,7 +56,7 @@ } #drops sps before upgrade or refresh operations -drop_sps() { +dbfunc_common_drop_sps() { # common stored procedures are executed first (for new added functions to be valid) local file="${DBFUNC_TMPDIR}/drop_sps.sql" @@ -41,7 +69,7 @@ } #refreshes sps -refresh_sps() { +dbfunc_common_refresh_sps() { echo "Creating stored procedures..." local sql for sql in $(ls *sp.sql | sort); do @@ -51,41 +79,42 @@ dbfunc_psql_die --file="common_sp.sql" > /dev/null } -install_common_func() { +_dbfunc_common_install_common_func() { # common stored procedures are executed first (for new added functions to be valid) dbfunc_psql_die --file="common_sp.sql" > /dev/null } -delete_async_tasks_and_compensation_data() { +_dbfunc_common_delete_async_tasks_and_compensation_data() { dbfunc_psql_die --file="delete_async_tasks_and_compensation_data.sql" > /dev/null } -run_pre_upgrade() { +_dbfunc_common_run_pre_upgrade() { #Dropping all views & sps - drop_views - drop_sps - install_common_func + dbfunc_common_drop_views + dbfunc_common_drop_sps + _dbfunc_common_install_common_func #update sequence numers - update_sequence_numbers + dbfunc_common_hook_update_sequence_numbers #run pre upgrade scripts - dbfunc_psql_statements_in_dir 'pre_upgrade' 'pre-upgrade' - install_materialized_views_func + _dbfunc_common_psql_statements_in_dir 'pre_upgrade' 'pre-upgrade' + dbfunc_common_hook_install_materialized_views_func #drop materialized views to support views changesin upgrade #Materialized views are restored in the post_upgrade step - drop_materialized_views + dbfunc_common_hook_drop_materialized_views + # TODO: move this to custom? if [ -n "${CLEAN_TASKS}" ]; then echo "Cleaning tasks metadata..." - delete_async_tasks_and_compensation_data + _dbfunc_common_delete_async_tasks_and_compensation_data fi } -run_post_upgrade() { +_dbfunc_common_run_post_upgrade() { #Refreshing all views & sps & run post-upgrade scripts - refresh_views - refresh_sps + dbfunc_common_hook_refresh_views + dbfunc_common_refresh_sps #Running post-upgrade scripts - dbfunc_psql_statements_in_dir 'post_upgrade' 'post-upgrade' + _dbfunc_common_psql_statements_in_dir 'post_upgrade' 'post-upgrade' #run custom materialized views if exists custom_materialized_views_file="upgrade/post_upgrade/custom/create_materialized_views.sql" if [ -f "${custom_materialized_views_file}" ]; then @@ -96,23 +125,22 @@ echo "Illegal syntax in custom Materialized Views, Custom Materialized Views were dropped." fi fi - refresh_materialized_views - + dbfunc_common_hook_refresh_materialized_views } # Runs all the SQL scripts in directory upgrade/$1/ # The second argument is the label to use while notifying # the user about the running of the script -dbfunc_psql_statements_in_dir() { +_dbfunc_common_psql_statements_in_dir() { if [ -d "upgrade/$1" ]; then - files="$(get_files "upgrade/${1}" 1)" + files="$(_dbfunc_common_get_files "upgrade/${1}" 1)" for execFile in $(ls ${files} | sort); do - run_file "${execFile}" + _dbfunc_common_run_file "${execFile}" done fi } -run_required_scripts() { +_dbfunc_common_run_required_scripts() { local script="${1}" # check for helper functions that the script needs # source scripts must be defined in the first lines of the script @@ -131,7 +159,7 @@ done < "${script}" } -run_file() { +_dbfunc_common_run_file() { local execFile="${1}" if [ -x "${execFile}" ]; then echo "Running upgrade shell script '${execFile}'..." @@ -146,7 +174,7 @@ fi } -get_current_version() { +_dbfunc_common_get_current_version() { dbfunc_psql_statement_parsable " select version from schema_version @@ -156,7 +184,7 @@ " } -get_installed_version() { +_dbfunc_common_get_installed_version() { local cheksum="${1}" dbfunc_psql_statement_parsable " select version @@ -167,7 +195,8 @@ " } -get_last_installed_id() { +# TODO: why can't it collapse into the set_last_version? +_dbfunc_common_get_last_installed_id() { dbfunc_psql_statement_parsable " select max(id) from schema_version @@ -175,26 +204,26 @@ " } -set_last_version() { +_dbfunc_common_set_last_version() { dbfunc_psql_statement_die " update schema_version - set current=(id=$(get_last_installed_id)); + set current=(id=$(_dbfunc_common_get_last_installed_id)); " > /dev/null } -get_db_time(){ +_dbfunc_common_get_db_time(){ dbfunc_psql_statement_parsable "select now()" } # gets a directory and required depth and return all sql & sh files -get_files() { +_dbfunc_common_get_files() { local sqlFiles="$(find ${1} -maxdepth ${2} -name "*.sql" -print)" local shFiles="$(find ${1} -maxdepth ${2} -name "*.sh" -print)" echo "${sqlFiles} ${shFiles}" } -is_view_or_sp_changed() { - local files="$(get_files "upgrade" 3)" +_dbfunc_common_is_view_or_sp_changed() { + local files="$(_dbfunc_common_get_files "upgrade" 3)" local md5sum_file="${DBFUNC_MD5DIR}/.${DBFUNC_DB_DATABASE}.scripts.md5" local md5sum_tmp_file="${md5sum_file}.tmp" md5sum ${files} create_*views.sql *_sp.sql > "${md5sum_tmp_file}" @@ -211,9 +240,9 @@ return $result } -validate_version_uniqueness() { +_dbfunc_common_validate_version_uniqueness() { local prev="" - local files="$(get_files "upgrade" 1)" + local files="$(_dbfunc_common_get_files "upgrade" 1)" local file for file in $(ls ${files} | sort) ; do local ver="$(echo "${file}" | sed -e 's#upgrade/\(..........\).*#\1#' -e 's/_//g')" @@ -225,28 +254,28 @@ done } -run_upgrade_files() { +dbfunc_common_upgrade() { local res="$(find upgrade/ -name "*" | grep -E ".sql|.sh" | wc -l)" local CMD if [ "${res}" -gt 0 ]; then local state="FAILED" local comment="" local updated=0 - validate_version_uniqueness - if [ -z "${DBFUNC_MD5DIR}" ] || ! is_view_or_sp_changed; then + _dbfunc_common_validate_version_uniqueness + if [ -z "${DBFUNC_MD5DIR}" ] || ! _dbfunc_common_is_view_or_sp_changed; then echo "upgrade script detected a change in Config, View or Stored Procedure..." - run_pre_upgrade + _dbfunc_common_run_pre_upgrade updated=1 fi # get current version - local current="$(get_current_version)" + local current="$(_dbfunc_common_get_current_version)" # we should remove leading blank (from select result) and zero in order not to treat number as octal local last="$(expr substr "${current}" 3 7)" - local files="$(get_files "upgrade" 1)" + local files="$(_dbfunc_common_get_files "upgrade" 1)" local file for file in $(ls ${files} | sort); do - local before="$(get_db_time)" + local before="$(_dbfunc_common_get_db_time)" local checksum="$(md5sum "${file}" | cut -d " " -f1)" # upgrade/dd_dd_dddd* => dddddddd local ver="$(echo "${file}" | sed -e 's#upgrade/\(..........\).*#\1#' -e 's/_//g')" @@ -261,37 +290,37 @@ # check gaps only for identical major revisions if [ "${xverMajor}" -eq "${lastMajor}" ]; then if [ $((${xver} - ${last})) -gt 10 ]; then - set_last_version + _dbfunc_common_set_last_version echo "Illegal script version number ${ver},version should be in max 10 gap from last installed version: 0${last}" echo "Please fix numbering to interval 0$(( ${last} + 1)) to 0$(( ${last} + 10)) and run the upgrade script." exit 1 fi fi # check if script was already installed with other version name. - local installed_version="$(get_installed_version $checksum)" + local installed_version="$(_dbfunc_common_get_installed_version "${checksum}")" if [ -n "${installed_version}" ]; then echo "Skipping upgrade script ${file}, already installed by ${installed_version}" state="SKIPPED" - after="$(get_db_time)" + after="$(_dbfunc_common_get_db_time)" last="${xver}" comment="Installed already by ${installed_version}" else # force pre upgrade to run in case no md5 change was # found but we still upgrade, like in db restore. if [ "${updated}" = 0 ]; then - run_pre_upgrade + _dbfunc_common_run_pre_upgrade updated=1 fi - run_required_scripts "${file}" - run_file "${file}" + _dbfunc_common_run_required_scripts "${file}" + _dbfunc_common_run_file "${file}" code=$? if [ "${code}" -eq 0 ]; then state="INSTALLED" - after=$(get_db_time) + after=$(_dbfunc_common_get_db_time) last=$xver comment="" else - set_last_version + _dbfunc_common_set_last_version exit "${code}" fi fi @@ -322,11 +351,11 @@ " > /dev/null fi done - set_last_version + _dbfunc_common_set_last_version # restore views & SPs if dropped if [ "${updated}" -eq 1 ]; then - run_post_upgrade + _dbfunc_common_run_post_upgrade else echo "database is up to date." fi @@ -335,7 +364,7 @@ # gets the configuration value of the given option name and version. # usage: <some variable>=get_config_value <name> <version> -get_config_value() { +dbfunc_common_get_config_value() { local option_name="${1}" local version="${2}" @@ -351,7 +380,7 @@ } #adds a record to audit_log in case of calling unlock_entity -log_unlock_entity() { +_dbfunc_common_log_unlock_entity() { local object_type="${1}" local id="${2}" local user="${3}" @@ -377,7 +406,7 @@ #unlocks the given VM/Template and its disks or a given disk #in case of VM/Template the id is the name, in case of a disk, the id is the disk UUID -unlock_entity() { +dbfunc_common_unlock_entity() { local object_type="${1}" local id="${2}" local user="${3}" @@ -399,7 +428,7 @@ if [ -n "${CMD}" ]; then echo "${CMD}" if dbfunc_psql_statement_die "${CMD}"; then - log_unlock_entity ${object_type} ${id} ${user} + _dbfunc_common_log_unlock_entity ${object_type} ${id} ${user} echo "unlock ${object_type} ${id} completed successfully." else echo "unlock ${object_type} ${id} completed with errors." @@ -411,7 +440,7 @@ # # TODO: figure out why can't we concat statements. # -query_locked_entities() { +dbfunc_common_query_locked_entities() { local object_type="${1}" local LOCKED=2 local TEMPLATE_LOCKED=1 diff --git a/packaging/dbscripts/dbfunc-custom.sh b/packaging/dbscripts/dbfunc-custom.sh index c731716..4922fe5 100644 --- a/packaging/dbscripts/dbfunc-custom.sh +++ b/packaging/dbscripts/dbfunc-custom.sh @@ -4,7 +4,7 @@ DBFUNC_DB_USER="${DBFUNC_DB_USER:-engine}" DBFUNC_DB_DATABASE="${DBFUNC_DB_DATABASE:-engine}" -insert_initial_data() { +dbfunc_common_hook_insert_initial_data() { echo "Inserting data..." dbfunc_psql_die --file="insert_data.sql" > /dev/null echo "Inserting pre-defined roles..." @@ -12,57 +12,28 @@ } #refreshes views -refresh_views() { +dbfunc_common_hook_refresh_views() { echo "Creating views..." dbfunc_psql_die --file="create_views.sql" > /dev/null dbfunc_psql_die --file="create_dwh_views.sql" > /dev/null } -fn_db_set_dbobjects_ownership() { - local res="$( - dbfunc_psql_statement_parsable " - select c.relname - from - pg_class c join pg_roles r on r.oid = c.relowner join pg_namespace n on n.oid = c.relnamespace - where - c.relkind in ('r','v','S') and - n.nspname = 'public' and - r.rolname != '${DBFUNC_DB_USER}' - " - )" - if [ -n "${res}" ]; then - local tab - cmd="" - for tab in $(echo ${res}); do - cmd="${cmd}alter table ${tab} owner to ${DBFUNC_DB_USER}; " - done - if [ -n "${cmd}" ]; then - echo -n "Changing ownership of objects in database '${DBFUNC_DB_DATABASE}' to owner '${DBFUNC_DB_USER}'... " - if dbfunc_psql "${cmd}"; then - echo "completed successfully." - else - return 1 - fi - fi - fi -} - # Materilized views functions, override with empty implementation on DBs that not supporting that -install_materialized_views_func() { +dbfunc_common_hook_install_materialized_views_func() { dbfunc_psql_die --file="materialized_views_sp.sql" > /dev/null } -drop_materialized_views() { +dbfunc_common_hook_drop_materialized_views() { echo "Dropping materialized views..." dbfunc_psql_statement_die "select DropAllMaterializedViews();" > /dev/null } -refresh_materialized_views() { +dbfunc_common_hook_refresh_materialized_views() { echo "Refreshing materialized views..." dbfunc_psql_statement_die "select RefreshAllMaterializedViews(true);" > /dev/null } -update_sequence_numbers() { +dbfunc_common_hook_update_sequence_numbers() { dbfunc_psql_die --file="update_sequence_numbers.sql" > /dev/null } diff --git a/packaging/dbscripts/refreshStoredProcedures.sh b/packaging/dbscripts/refreshStoredProcedures.sh index bbde403..6fc255d 100755 --- a/packaging/dbscripts/refreshStoredProcedures.sh +++ b/packaging/dbscripts/refreshStoredProcedures.sh @@ -40,12 +40,12 @@ done #Dropping all views & sps -drop_views -drop_sps +dbfunc_common_drop_views +dbfunc_common_drop_sps #Refreshing all views & sps -refresh_views -refresh_sps +dbfunc_common_hook_refresh_views +dbfunc_common_refresh_sps printf "Done.\n" popd>/dev/null diff --git a/packaging/dbscripts/restore.sh b/packaging/dbscripts/restore.sh index a6d8f5a..4b3fc23 100755 --- a/packaging/dbscripts/restore.sh +++ b/packaging/dbscripts/restore.sh @@ -69,6 +69,35 @@ return $res } +fn_db_set_dbobjects_ownership() { + local res="$( + dbfunc_psql_statement_parsable " + select c.relname + from + pg_class c join pg_roles r on r.oid = c.relowner join pg_namespace n on n.oid = c.relnamespace + where + c.relkind in ('r','v','S') and + n.nspname = 'public' and + r.rolname != '${DBFUNC_DB_USER}' + " + )" + if [ -n "${res}" ]; then + local tab + cmd="" + for tab in $(echo ${res}); do + cmd="${cmd}alter table ${tab} owner to ${DBFUNC_DB_USER}; " + done + if [ -n "${cmd}" ]; then + echo -n "Changing ownership of objects in database '${DBFUNC_DB_DATABASE}' to owner '${DBFUNC_DB_USER}'... " + if dbfunc_psql "${cmd}"; then + echo "completed successfully." + else + return 1 + fi + fi + fi +} + get_query_result() { local cmd=${1} local db=${2} diff --git a/packaging/dbscripts/unlock_entity.sh b/packaging/dbscripts/unlock_entity.sh index dbbe138..063d9a0 100755 --- a/packaging/dbscripts/unlock_entity.sh +++ b/packaging/dbscripts/unlock_entity.sh @@ -70,10 +70,10 @@ if [[ -n "${TYPE}" && -n "${IDS}" ]]; then caution for ID in ${IDS} ; do - unlock_entity "${TYPE}" "${ID}" "$(whoami)" ${RECURSIVE} + dbfunc_common_unlock_entity "${TYPE}" "${ID}" "$(whoami)" ${RECURSIVE} done elif [[ -n "${TYPE}" && -n "${QUERY}" ]]; then - query_locked_entities "${TYPE}" + dbfunc_common_query_locked_entities "${TYPE}" else usage fi diff --git a/packaging/dbscripts/upgrade.sh b/packaging/dbscripts/upgrade.sh index 94a301f..14d5f92 100755 --- a/packaging/dbscripts/upgrade.sh +++ b/packaging/dbscripts/upgrade.sh @@ -90,7 +90,7 @@ where version=trim('${VERSION}'); " > /dev/null fi -run_upgrade_files +dbfunc_common_upgrade ret=$? echo "Done." -- To view, visit http://gerrit.ovirt.org/24916 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Icce88f1d8f19da83c757f94ad661cb676bd70274 Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Alon Bar-Lev <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
