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.

reorder public interface to have <prefix>_<group>_<verb>

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/unlock_entity.sh
M packaging/dbscripts/upgrade.sh
7 files changed, 101 insertions(+), 102 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/29/25129/1

diff --git a/packaging/dbscripts/cleandb.sh b/packaging/dbscripts/cleandb.sh
index d8d0afa..1d17f5a 100755
--- a/packaging/dbscripts/cleandb.sh
+++ b/packaging/dbscripts/cleandb.sh
@@ -43,4 +43,4 @@
 done
 
 echo "Cleaning database..."
-cleandb
+dbfunc_database_clean
diff --git a/packaging/dbscripts/create_schema.sh 
b/packaging/dbscripts/create_schema.sh
index 0a82fa2..52fad60 100755
--- a/packaging/dbscripts/create_schema.sh
+++ b/packaging/dbscripts/create_schema.sh
@@ -56,11 +56,11 @@
 dbfunc_psql_die --file="common_sp.sql" > /dev/null
 
 #inserting initial data
-insert_initial_data
+dbfunc_common_hook_init_insert_data
 
 #remove checksum file in clean install in order to run views/sp creation
 [ -n "${DBFUNC_COMMON_MD5DIR}" ] && rm -f 
"${DBFUNC_COMMON_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 97d0855..d42aa36 100644
--- a/packaging/dbscripts/dbfunc-common.sh
+++ b/packaging/dbscripts/dbfunc-common.sh
@@ -3,8 +3,32 @@
 
 #DBFUNC_COMMON_MD5DIR=
 
+dbfunc_common_hook_init_insert_data() {
+    return 0
+}
+
+dbfunc_common_hook_views_refresh() {
+    return 0
+}
+
+dbfunc_common_hook_materialized_views_install() {
+    return 0
+}
+
+dbfunc_common_hook_materialized_views_drop() {
+    return 0
+}
+
+dbfunc_common_hook_materialized_viewsrefresh_() {
+    return 0
+}
+
+dbfunc_common_hook_sequence_numbers_update() {
+    return 0
+}
+
 #cleans db by dropping all objects
-cleandb() {
+dbfunc_database_clean() {
     dbfunc_psql_die --file="common_sp.sql" > /dev/null
     local statement
     statement="$(
@@ -17,8 +41,12 @@
     dbfunc_psql_die --command="${statement}" > /dev/null
 }
 
+dbfunc_common_init_insert_data() {
+    dbfunc_common_hook_init_insert_data
+}
+
 #drops views before upgrade or refresh operations
-drop_views() {
+dbfunc_common_views_drop() {
     # common stored procedures are executed first (for new added functions to 
be valid)
     dbfunc_psql_die --file="common_sp.sql" > /dev/null
     dbfunc_psql_die --command="select * from 
generate_drop_all_views_syntax();" | \
@@ -26,7 +54,7 @@
 }
 
 #drops sps before upgrade or refresh operations
-drop_sps() {
+dbfunc_common_sps_drop() {
     dbfunc_psql_die --file="common_sp.sql" > /dev/null
     local statement
     statement="$(
@@ -39,7 +67,7 @@
 }
 
 #refreshes sps
-refresh_sps() {
+dbfunc_common_sps_refresh() {
     echo "Creating stored procedures..."
     local sql
     for sql in $(ls *sp.sql | sort); do
@@ -49,41 +77,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_views_drop
+    dbfunc_common_sps_drop
+    _dbfunc_common_install_common_func
     #update sequence numers
-    update_sequence_numbers
+    dbfunc_common_hook_sequence_numbers_update
     #run pre upgrade scripts
-    dbfunc_psql_statements_in_dir 'pre_upgrade'
-    install_materialized_views_func
+    _dbfunc_common_psql_statements_in_dir 'pre_upgrade'
+    dbfunc_common_hook_materialized_views_install
     #drop materialized views to support views changesin upgrade
     #Materialized views are restored in the post_upgrade step
-    drop_materialized_views
+    dbfunc_common_hook_materialized_views_drop
 
+    # 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_views_refresh
+    dbfunc_common_sps_refresh
     #Running post-upgrade scripts
-    dbfunc_psql_statements_in_dir 'post_upgrade'
+    _dbfunc_common_psql_statements_in_dir '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
@@ -94,24 +123,23 @@
             echo "Illegal syntax in custom Materialized Views, Custom 
Materialized Views were dropped."
         fi
     fi
-    refresh_materialized_views
-
+    dbfunc_common_hook_materialized_viewsrefresh_
 }
 
 # 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() {
     local dir="$1"
     if [ -d "upgrade/${dir}" ]; then
-        files="$(get_files "upgrade/${dir}" 1)"
+        files="$(_dbfunc_common_get_files "upgrade/${dir}" 1)"
         for file in $(ls ${files} | sort); do
-           run_file "${file}"
+           _dbfunc_common_run_file "${file}"
         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
@@ -127,7 +155,7 @@
     done < "${script}"
 }
 
-run_file() {
+_dbfunc_common_run_file() {
     local file="$1"
     if [ -x "${file}" ]; then
         # delegate all DBFUNC_ vars in subshell
@@ -142,7 +170,7 @@
     fi
 }
 
-get_current_version() {
+_dbfunc_common_get_current_version() {
     dbfunc_psql_statement_parsable "
         select version
         from schema_version
@@ -152,7 +180,7 @@
     "
 }
 
-get_installed_version() {
+_dbfunc_common_get_installed_version() {
     local cheksum="$1"
     dbfunc_psql_statement_parsable "
         select version
@@ -163,7 +191,7 @@
     "
 }
 
-set_last_version() {
+_dbfunc_common_set_last_version() {
     local id="$(
         dbfunc_psql_statement_parsable "
             select max(id)
@@ -177,12 +205,12 @@
     " > /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 dir="$1"
     local maxdepth="$2"
     find "${dir}" \
@@ -191,8 +219,8 @@
         sort
 }
 
-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_COMMON_MD5DIR}/.${DBFUNC_DB_DATABASE}.scripts.md5"
     local md5sum_tmp_file="${md5sum_file}.tmp"
     md5sum ${files} create_*views.sql *_sp.sql > "${md5sum_tmp_file}"
@@ -214,9 +242,9 @@
     basename "${file}" | sed -e 's#\(..........\).*#\1#' -e 's/_//g'
 }
 
-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="$(_dbfunc_common_get_file_version "${file}")"
@@ -225,28 +253,28 @@
     done
 }
 
-run_upgrade_files() {
+dbfunc_common_upgrade() {
     local res="$(find upgrade/ -name '*.sql' -or -name '*.sh' | wc -l)"
     local CMD
     if [ "${res}" -gt 0 ]; then
         local state="FAILED"
         local comment=""
         local updated=0
-        validate_version_uniqueness
-        if [ -z "${DBFUNC_COMMON_MD5DIR}" ] || ! is_view_or_sp_changed; then
+        _dbfunc_common_validate_version_uniqueness
+        if [ -z "${DBFUNC_COMMON_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="$(_dbfunc_common_get_file_version "${file}")"
@@ -261,36 +289,36 @@
                 # 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
                        die "Illegal script version number ${ver},version 
should be in max 10 gap from last installed version: 0${last}
 Please fix numbering to interval 0$(( ${last} + 1)) to 0$(( ${last} + 10)) and 
run the upgrade script."
                     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
@@ -320,11 +348,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
@@ -333,7 +361,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_config_get_value() {
     local option_name="$1"
     local version="$2"
 
@@ -349,7 +377,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"
@@ -375,7 +403,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_entity_unlock() {
     local object_type="$1"
     local id="$2"
     local user="$3"
@@ -395,7 +423,7 @@
     if [ -n "${CMD}" ]; then
         echo "${CMD}"
         if dbfunc_psql_die --command="${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."
@@ -404,7 +432,7 @@
 }
 
 #Displays locked entities
-query_locked_entities() {
+dbfunc_common_entity_query() {
     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 422807a..1a190b5 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_init_insert_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_views_refresh() {
     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_materialized_views_install() {
     dbfunc_psql_die --file="materialized_views_sp.sql" > /dev/null
 }
 
-drop_materialized_views() {
+dbfunc_common_hook_materialized_views_drop() {
     echo "Dropping materialized views..."
     dbfunc_psql_die --command="select DropAllMaterializedViews();" > /dev/null
 }
 
-refresh_materialized_views() {
+dbfunc_common_hook_materialized_viewsrefresh_() {
     echo "Refreshing materialized views..."
     dbfunc_psql_die --command="select RefreshAllMaterializedViews(true);" > 
/dev/null
 }
 
-update_sequence_numbers() {
+dbfunc_common_hook_sequence_numbers_update() {
     dbfunc_psql_die --file="update_sequence_numbers.sql" > /dev/null
 }
diff --git a/packaging/dbscripts/refreshStoredProcedures.sh 
b/packaging/dbscripts/refreshStoredProcedures.sh
index 15a6370..c2117e0 100755
--- a/packaging/dbscripts/refreshStoredProcedures.sh
+++ b/packaging/dbscripts/refreshStoredProcedures.sh
@@ -37,9 +37,9 @@
 done
 
 #Dropping all views & sps
-drop_views
-drop_sps
+dbfunc_common_views_drop
+dbfunc_common_sps_drop
 
 #Refreshing  all views & sps
-refresh_views
-refresh_sps
+dbfunc_common_hook_views_refresh
+dbfunc_common_sps_refresh
diff --git a/packaging/dbscripts/unlock_entity.sh 
b/packaging/dbscripts/unlock_entity.sh
index 2ad91be..6bdae78 100755
--- a/packaging/dbscripts/unlock_entity.sh
+++ b/packaging/dbscripts/unlock_entity.sh
@@ -71,8 +71,8 @@
 if [ -n "${IDS}" ]; then
     caution
     for ID in ${IDS} ; do
-        unlock_entity "${TYPE}" "${ID}" "$(whoami)" ${RECURSIVE}
+        dbfunc_common_entity_unlock "${TYPE}" "${ID}" "$(whoami)" ${RECURSIVE}
     done
 elif [ -n "${QUERY}" ]; then
-    query_locked_entities "${TYPE}"
+    dbfunc_common_entity_query "${TYPE}"
 fi
diff --git a/packaging/dbscripts/upgrade.sh b/packaging/dbscripts/upgrade.sh
index 6cf3a34..0f480a4 100755
--- a/packaging/dbscripts/upgrade.sh
+++ b/packaging/dbscripts/upgrade.sh
@@ -84,4 +84,4 @@
         where version=trim('${VERSION}');
     " > /dev/null
 fi
-run_upgrade_files
+dbfunc_common_upgrade


-- 
To view, visit http://gerrit.ovirt.org/25129
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

Reply via email to