Alon Bar-Lev has uploaded a new change for review. Change subject: db: cleanup: use single script for schema handling ......................................................................
db: cleanup: use single script for schema handling schema.sh -c apply|refresh|drop Change-Id: Ie13ee3d4ddf899f680b592ff58fb9d09573dae59 Signed-off-by: Alon Bar-Lev <[email protected]> --- D packaging/dbscripts/cleandb.sh D packaging/dbscripts/create_schema.sh M packaging/dbscripts/dbfunc-common.sh D packaging/dbscripts/refreshStoredProcedures.sh A packaging/dbscripts/schema.sh D packaging/dbscripts/upgrade.sh M packaging/setup/ovirt_engine_setup/constants.py M packaging/setup/plugins/ovirt-engine-setup/ovirt-engine/db/schema.py 8 files changed, 273 insertions(+), 459 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/23/25223/1 diff --git a/packaging/dbscripts/cleandb.sh b/packaging/dbscripts/cleandb.sh deleted file mode 100755 index a361e7b..0000000 --- a/packaging/dbscripts/cleandb.sh +++ /dev/null @@ -1,45 +0,0 @@ -#!/bin/sh - -################################################################################ -# Cleans DB by dropping all DB objects -################################################################################ - -DBFUNC_COMMON_DBSCRIPTS_DIR="$(dirname "$0")" -. "${DBFUNC_COMMON_DBSCRIPTS_DIR}/dbfunc-custom.sh" - -cleanup() { - dbfunc_cleanup -} -trap cleanup 0 -dbfunc_init - -usage() { - cat << __EOF__ -Usage: $0 [options] - - -s HOST - The database servername for the database (def. ${DBFUNC_DB_HOST}) - -p PORT - The database port for the database (def. ${DBFUNC_DB_PORT}) - -d DATABASE - The database name (def. ${DBFUNC_DB_DATABASE}) - -u USER - The username for the database (def. ${DBFUNC_DB_USER}) - -l LOGFILE - The logfile for capturing output (def. ${DBFUNC_LOGFILE} - -v - Turn on verbosity (WARNING: lots of output) - -h - This help text. - -__EOF__ -} - -while getopts hs:d:u:p:l:v option; do - case "${option}" in - s) DBFUNC_DB_HOST="${OPTARG}";; - p) DBFUNC_DB_PORT="${OPTARG}";; - d) DBFUNC_DB_DATABASE="${OPTARG}";; - u) DBFUNC_DB_USER="${OPTARG}";; - l) DBFUNC_LOGFILE="${OPTARG}";; - v) DBFUNC_VERBOSE=1;; - h) usage; exit 0;; - \?) usage; exit 1;; - esac -done - -echo "Cleaning database..." -dbfunc_common_schema_drop diff --git a/packaging/dbscripts/create_schema.sh b/packaging/dbscripts/create_schema.sh deleted file mode 100755 index 31c60d1..0000000 --- a/packaging/dbscripts/create_schema.sh +++ /dev/null @@ -1,66 +0,0 @@ -#!/bin/sh - -DBFUNC_COMMON_DBSCRIPTS_DIR="$(dirname "$0")" -. "${DBFUNC_COMMON_DBSCRIPTS_DIR}/dbfunc-custom.sh" - -cleanup() { - dbfunc_cleanup -} -trap cleanup 0 -dbfunc_init - -usage() { - cat << __EOF__ -Usage: $0 [options] - - -h - This help text. - -v - Turn on verbosity (WARNING: lots of output) - -s HOST - The database servername for the database (def. ${DBFUNC_DB_HOST}) - -p PORT - The database port for the database (def. ${DBFUNC_DB_PORT}) - -d DATABASE - The database name (def. ${DBFUNC_DB_DATABASE}) - -u USER - The username for the database (def. ${DBFUNC_DB_USER}) - -l LOGFILE - The logfile for capturing output (def. ${DBFUNC_LOGFILE}) - -m MD5DIR - The directory for generated MD5 files (def. ${DBFUNC_COMMON_MD5DIR}) - -__EOF__ -} - -while getopts :hs:d:u:p:l:m:v option; do - case "${option}" in - s) DBFUNC_DB_HOST="${OPTARG}";; - p) DBFUNC_DB_PORT="${OPTARG}";; - d) DBFUNC_DB_DATABASE="${OPTARG}";; - u) DBFUNC_DB_USER="${OPTARG}";; - l) DBFUNC_LOGFILE="${OPTARG}";; - m) DBFUNC_COMMON_MD5DIR="${OPTARG}";; - v) DBFUNC_VERBOSE=1;; - h) usage; exit 0;; - \?) usage; exit 1;; - esac -done - -echo "user name is: '${DBFUNC_DB_USER}'" - -dbfunc_psql --command="create language 'plpgsql';" > /dev/null - -#set database min error level -dbfunc_psql_die --command="ALTER DATABASE \"${DBFUNC_DB_DATABASE}\" SET client_min_messages=ERROR;" > /dev/null - -echo "Creating tables..." -dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/create_tables.sql" > /dev/null - -echo "Creating functions..." -dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/create_functions.sql" > /dev/null - -echo "Creating common functions..." -dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null - -#inserting initial data -dbfunc_common_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..." -dbfunc_common_upgrade diff --git a/packaging/dbscripts/dbfunc-common.sh b/packaging/dbscripts/dbfunc-common.sh index 837e1ff..174a84f 100644 --- a/packaging/dbscripts/dbfunc-common.sh +++ b/packaging/dbscripts/dbfunc-common.sh @@ -47,115 +47,25 @@ dbfunc_psql_die --command="${statement}" > /dev/null } -dbfunc_common_init_insert_data() { - dbfunc_common_hook_init_insert_data +dbfunc_common_schema_apply() { + # check database connection + dbfunc_psql_die --command="select 1;" > /dev/null + + echo "Creating schema ${DBFUNC_DB_USER}@${DBFUNC_DB_HOST}:${DBFUNC_DB_PORT}/${DBFUNC_DB_DATABASE}" + if [ "$(dbfunc_psql_statement_parsable " + select count(*) as count + from information_schema.tables + where table_name = 'schema_version' + ")" -eq 0 ]; then + echo "Creating fresh schema" + _dbfunc_common_schema_create + fi + _dbfunc_common_schema_upgrade } -dbfunc_common_upgrade() { - - dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/upgrade/03_02_0000_set_version.sql" > /dev/null - - local files="$(_dbfunc_common_get_files "upgrade" 1)" - local CMD - if [ -n "${files}" ]; then - local state="FAILED" - local comment="" - local updated=0 - _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..." - _dbfunc_common_run_pre_upgrade - updated=1 - fi - - # 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 file - echo "${files}" | while read file; do - before="$(_dbfunc_common_get_db_time)" - checksum="$(md5sum "${file}" | cut -d " " -f1)" - ver="$(_dbfunc_common_get_file_version "${file}")" - if [ "${ver}" -gt "${current}" ] ; then - # we should remove leading zero in order not to treat number as octal - local xver="$(expr substr "${ver}" 2 7)" - # taking major revision , i.e 03010000=>301 - local xverMajor="$(expr substr "${xver}" 1 3)" - local lastMajor="$(expr substr "${last}" 1 3)" - - # check for gaps in upgrade - # check gaps only for identical major revisions - if [ "${xverMajor}" -eq "${lastMajor}" ]; then - if [ $((${xver} - ${last})) -gt 10 ]; then - _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="$(_dbfunc_common_get_installed_version "${checksum}")" - if [ -n "${installed_version}" ]; then - echo "Skipping upgrade script ${file}, already installed by ${installed_version}" - state="SKIPPED" - 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 - _dbfunc_common_run_pre_upgrade - updated=1 - fi - _dbfunc_common_run_required_scripts "${file}" - _dbfunc_common_run_file "${file}" - code=$? - if [ "${code}" -eq 0 ]; then - state="INSTALLED" - after=$(_dbfunc_common_get_db_time) - last=$xver - comment="" - else - _dbfunc_common_set_last_version - exit "${code}" - fi - fi - dbfunc_psql_die --command=" - insert into schema_version( - version, - script, - checksum, - installed_by, - started_at, - ended_at, - state, - current, - comment - ) - values ( - trim('${ver}'), - '$(echo "${file}" | sed "s#^${DBFUNC_COMMON_DBSCRIPTS_DIR}/##")', - '${checksum}', - '${DBFUNC_DB_USER}', - cast(trim('${before}') as timestamp), - cast(trim('${after}') as timestamp), - '${state}', - false, - '${comment}' - ); - " > /dev/null - fi - done || exit $? - _dbfunc_common_set_last_version - - # restore views & SPs if dropped - if [ "${updated}" -eq 1 ]; then - _dbfunc_common_run_post_upgrade - else - echo "database is up to date." - fi - fi +dbfunc_common_schema_refresh() { + _dbfunc_common_schema_refresh_drop + _dbfunc_common_schema_refresh_create } # gets the configuration value of the given option name and version. @@ -173,38 +83,6 @@ version='${version}' " )" -} - -#drops views before upgrade or refresh operations -dbfunc_common_views_drop() { - # common stored procedures are executed first (for new added functions to be valid) - dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null - dbfunc_psql_die --command="select * from generate_drop_all_views_syntax();" | \ - dbfunc_psql_die > /dev/null -} - -#drops sps before upgrade or refresh operations -dbfunc_common_sps_drop() { - dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null - local statement - statement="$( - dbfunc_psql_die --command="select * from generate_drop_all_functions_syntax();" - )" || exit 1 - dbfunc_psql_die --command="${statement}" > /dev/null - - # recreate generic functions - dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/create_functions.sql" > /dev/null -} - -#refreshes sps -dbfunc_common_sps_refresh() { - echo "Creating stored procedures..." - local file - find "${DBFUNC_COMMON_DBSCRIPTS_DIR}" -name '*sp.sql' | sort | while read file; do - echo "Creating stored procedures from ${file}..." - dbfunc_psql_die --file="${file}" > /dev/null - done || exit $? - dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null } #unlocks the given VM/Template and its disks or a given disk @@ -336,10 +214,180 @@ fi } +_dbfunc_common_schema_refresh_drop() { + _dbfunc_common_views_drop + _dbfunc_common_sps_drop +} + +_dbfunc_common_schema_refresh_create() { + dbfunc_common_hook_views_refresh + _dbfunc_common_sps_refresh +} + +_dbfunc_common_schema_create() { + dbfunc_psql --command="create language 'plpgsql';" > /dev/null + + #set database min error level + dbfunc_psql_die --command="ALTER DATABASE \"${DBFUNC_DB_DATABASE}\" SET client_min_messages=ERROR;" > /dev/null + + echo "Creating tables..." + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/create_tables.sql" > /dev/null + + echo "Creating functions..." + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/create_functions.sql" > /dev/null + + echo "Creating common functions..." + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null + + #inserting 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 +} + +_dbfunc_common_schema_upgrade() { + + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/upgrade/03_02_0000_set_version.sql" > /dev/null + + local files="$(_dbfunc_common_get_files "upgrade" 1)" + local CMD + if [ -n "${files}" ]; then + local state="FAILED" + local comment="" + local updated=0 + _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..." + _dbfunc_common_run_pre_upgrade + updated=1 + fi + + # 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 file + echo "${files}" | while read file; do + before="$(_dbfunc_common_get_db_time)" + checksum="$(md5sum "${file}" | cut -d " " -f1)" + ver="$(_dbfunc_common_get_file_version "${file}")" + if [ "${ver}" -gt "${current}" ] ; then + # we should remove leading zero in order not to treat number as octal + local xver="$(expr substr "${ver}" 2 7)" + # taking major revision , i.e 03010000=>301 + local xverMajor="$(expr substr "${xver}" 1 3)" + local lastMajor="$(expr substr "${last}" 1 3)" + + # check for gaps in upgrade + # check gaps only for identical major revisions + if [ "${xverMajor}" -eq "${lastMajor}" ]; then + if [ $((${xver} - ${last})) -gt 10 ]; then + _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="$(_dbfunc_common_get_installed_version "${checksum}")" + if [ -n "${installed_version}" ]; then + echo "Skipping upgrade script ${file}, already installed by ${installed_version}" + state="SKIPPED" + 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 + _dbfunc_common_run_pre_upgrade + updated=1 + fi + _dbfunc_common_run_required_scripts "${file}" + _dbfunc_common_run_file "${file}" + code=$? + if [ "${code}" -eq 0 ]; then + state="INSTALLED" + after=$(_dbfunc_common_get_db_time) + last=$xver + comment="" + else + _dbfunc_common_set_last_version + exit "${code}" + fi + fi + dbfunc_psql_die --command=" + insert into schema_version( + version, + script, + checksum, + installed_by, + started_at, + ended_at, + state, + current, + comment + ) + values ( + trim('${ver}'), + '$(echo "${file}" | sed "s#^${DBFUNC_COMMON_DBSCRIPTS_DIR}/##")', + '${checksum}', + '${DBFUNC_DB_USER}', + cast(trim('${before}') as timestamp), + cast(trim('${after}') as timestamp), + '${state}', + false, + '${comment}' + ); + " > /dev/null + fi + done || exit $? + _dbfunc_common_set_last_version + + # restore views & SPs if dropped + if [ "${updated}" -eq 1 ]; then + _dbfunc_common_run_post_upgrade + else + echo "database is up to date." + fi + fi +} + +#drops views before upgrade or refresh operations +_dbfunc_common_views_drop() { + # common stored procedures are executed first (for new added functions to be valid) + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null + dbfunc_psql_die --command="select * from generate_drop_all_views_syntax();" | \ + dbfunc_psql_die > /dev/null +} + +#drops sps before upgrade or refresh operations +_dbfunc_common_sps_drop() { + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null + local statement + statement="$( + dbfunc_psql_die --command="select * from generate_drop_all_functions_syntax();" + )" || exit 1 + dbfunc_psql_die --command="${statement}" > /dev/null + + # recreate generic functions + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/create_functions.sql" > /dev/null +} + +#refreshes sps +_dbfunc_common_sps_refresh() { + echo "Creating stored procedures..." + local file + find "${DBFUNC_COMMON_DBSCRIPTS_DIR}" -name '*sp.sql' | sort | while read file; do + echo "Creating stored procedures from ${file}..." + dbfunc_psql_die --file="${file}" > /dev/null + done || exit $? + dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null +} + _dbfunc_common_run_pre_upgrade() { #Dropping all views & sps - dbfunc_common_views_drop - dbfunc_common_sps_drop + _dbfunc_common_schema_refresh_drop # common stored procedures are executed first (for new added functions to be valid) dbfunc_psql_die --file="${DBFUNC_COMMON_DBSCRIPTS_DIR}/common_sp.sql" > /dev/null #update sequence numers @@ -355,8 +403,7 @@ _dbfunc_common_run_post_upgrade() { #Refreshing all views & sps & run post-upgrade scripts - dbfunc_common_hook_views_refresh - dbfunc_common_sps_refresh + _dbfunc_common_schema_refresh_create #Running post-upgrade scripts _dbfunc_common_psql_statements_in_dir 'post_upgrade' #run custom materialized views if exists diff --git a/packaging/dbscripts/refreshStoredProcedures.sh b/packaging/dbscripts/refreshStoredProcedures.sh deleted file mode 100755 index e0a8506..0000000 --- a/packaging/dbscripts/refreshStoredProcedures.sh +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh - -DBFUNC_COMMON_DBSCRIPTS_DIR="$(dirname "$0")" -. "${DBFUNC_COMMON_DBSCRIPTS_DIR}/dbfunc-custom.sh" - -cleanup() { - dbfunc_cleanup -} -trap cleanup 0 -dbfunc_init - -usage() { - cat << __EOF__ -Usage: $0 [options] - - -h - This help text. - -v - Turn on verbosity (WARNING: lots of output) - -s HOST - The database servername for the database (def. ${DBFUNC_DB_HOST}) - -p PORT - The database port for the database (def. ${DBFUNC_DB_PORT}) - -d DATABASE - The database name (def. ${DBFUNC_DB_DATABASE}) - -u USER - The username for the database (def. ${DBFUNC_DB_USER}) - -l LOGFILE - The logfile for capturing output (def. ${DBFUNC_LOGFILE}) - -__EOF__ -} - -while getopts hs:d:u:p:l:v option; do - case $option in - s) DBFUNC_DB_HOST="${OPTARG}";; - p) DBFUNC_DB_PORT="${OPTARG}";; - d) DBFUNC_DB_DATABASE="${OPTARG}";; - u) DBFUNC_DB_USER="${OPTARG}";; - l) DBFUNC_LOGFILE="${OPTARG}";; - v) DBFUNC_VERBOSE=1;; - h) usage; exit 0;; - \?) usage; exit 1;; - esac -done - -#Dropping all views & sps -dbfunc_common_views_drop -dbfunc_common_sps_drop - -#Refreshing all views & sps -dbfunc_common_hook_views_refresh -dbfunc_common_sps_refresh diff --git a/packaging/dbscripts/schema.sh b/packaging/dbscripts/schema.sh new file mode 100755 index 0000000..462c6e9 --- /dev/null +++ b/packaging/dbscripts/schema.sh @@ -0,0 +1,64 @@ +#!/bin/sh + +DBFUNC_COMMON_DBSCRIPTS_DIR="$(dirname "$0")" +. "${DBFUNC_COMMON_DBSCRIPTS_DIR}/dbfunc-custom.sh" + +cleanup() { + dbfunc_cleanup +} +trap cleanup 0 +dbfunc_init + +usage() { + cat << __EOF__ +Usage: $0 [options] + + -h - This help text. + -v - Turn on verbosity (WARNING: lots of output) + -s HOST - The database servername for the database (def. ${DBFUNC_DB_HOST}) + -p PORT - The database port for the database (def. ${DBFUNC_DB_PORT}) + -d DATABASE - The database name (def. ${DBFUNC_DB_DATABASE}) + -u USER - The username for the database (def. ${DBFUNC_DB_USER}) + -l LOGFILE - The logfile for capturing output (def. ${DBFUNC_LOGFILE}) + -m MD5DIR - The directory for generated MD5 files (def. ${DBFUNC_COMMON_MD5DIR}) + -f VERSION - Enforce version. + -c COMMAND - Command: apply|refresh|drop + -t - Force cleaning tasks and compensation info. + +__EOF__ +} + +VERSION= + +while getopts :hs:d:u:p:l:m:f:vc:t option; do + case "${option}" in + s) DBFUNC_DB_HOST="${OPTARG}";; + p) DBFUNC_DB_PORT="${OPTARG}";; + d) DBFUNC_DB_DATABASE="${OPTARG}";; + u) DBFUNC_DB_USER="${OPTARG}";; + l) DBFUNC_LOGFILE="${OPTARG}";; + m) DBFUNC_COMMON_MD5DIR="${OPTARG}";; + v) DBFUNC_VERBOSE=1;; + f) VERSION="${OPTARG}";; + c) COMMAND="${OPTARG}";; + t) DBFUNC_CUSTOM_CLEAN_TASKS=1;; + h) usage; exit 0;; + \?) usage; exit 1;; + esac +done + +case "${COMMAND}" in + apply|refresh|drop) ;; + '') die "Please specify command";; + *) die "Invalid command '${COMMAND}'";; +esac + +eval dbfunc_common_schema_${COMMAND} + +if [ -n "${VERSION}" ]; then + dbfunc_psql_die --command=" + update schema_version + set current=true + where version=trim('${VERSION}'); + " > /dev/null +fi diff --git a/packaging/dbscripts/upgrade.sh b/packaging/dbscripts/upgrade.sh deleted file mode 100755 index 9a768b9..0000000 --- a/packaging/dbscripts/upgrade.sh +++ /dev/null @@ -1,85 +0,0 @@ -#!/bin/sh - -################################################################################ -# Upgrade script wrapper for handling each Schema or data change. -# Each upgrade change should be in a separate file formatted by MM_mm_nnnn_[Name].sql -# where: -# MM indicates Major Version number -# mm indicates Minor Version number -# nnnn are numbers starting from 0010, each having an offset of 10 from previous script -# (i.e 0010 0020 ....) -# [Name] is a short descriptive name for the script. -# All changes should be located under the upgrade/ directory -# -# When using -f <version> all scripts with version greater than the given one will -# be re-executed , for example -f 0300100 will execute all scripts from 03000110 -# and forth -# Since all views $ SP are dropped before upgrade and restored after all upgrade -# script were executed. We may have cases in which we need to run some helper -# functions before the upgrade script runs. -# In such a case and when those functions can not be put in the common_sp.sql -# because they are dependant on objects created by an upgrade script, we can put -# one or several lines at the begining of our upgrade file: -# --#source <sql_file_name>_sp.sql -# for example , putting in an upgrade script -# --#source myfunctions_sp.sql -# will run myfunctions_sp.sql before the upgrade script is executed. -# -# Each script must be re-entrant (i.e. each script checks if each step is needed -# to be executed or not, so we can run the same script many times without any -# problems) -################################################################################ - -DBFUNC_COMMON_DBSCRIPTS_DIR="$(dirname "$0")" -. "${DBFUNC_COMMON_DBSCRIPTS_DIR}/dbfunc-custom.sh" - -cleanup() { - dbfunc_cleanup -} -trap cleanup 0 -dbfunc_init - -VERSION= - -usage() { - cat << __EOF__ -Usage: $0 [options] - - -h - This help text. - -v - Turn on verbosity (WARNING: lots of output) - -s HOST - The database servername for the database (def. ${DBFUNC_DB_HOST}) - -p PORT - The database port for the database (def. ${DBFUNC_DB_PORT}) - -d DATABASE - The database name (def. ${DBFUNC_DB_DATABASE}) - -u USER - The username for the database (def. ${DBFUNC_DB_USER}) - -l LOGFILE - The logfile for capturing output (def. ${DBFUNC_LOGFILE}) - -m MD5DIR - The directory for generated MD5 files (def. ${DBFUNC_COMMON_MD5DIR}) - -f VERSION - Force upgrading from specified version (def. ${VERSION} - -c - Force cleaning tasks and compensation info. - -__EOF__ -} - -while getopts hs:d:u:p:l:f:m:cv option; do - case "${option}" in - s) DBFUNC_DB_HOST="${OPTARG}";; - p) DBFUNC_DB_PORT="${OPTARG}";; - d) DBFUNC_DB_DATABASE="${OPTARG}";; - u) DBFUNC_DB_USER="${OPTARG}";; - l) DBFUNC_LOGFILE="${OPTARG}";; - f) VERSION="${OPTARG}";; - c) DBFUNC_CUSTOM_CLEAN_TASKS=1;; - m) DBFUNC_COMMON_MD5DIR="${OPTARG}";; - v) DBFUNC_VERBOSE=1;; - h) usage; exit 0;; - \?) usage; exit 1;; - esac -done - -if [ -n "${VERSION}" ]; then - dbfunc_psql_die --command=" - update schema_version - set current=true - where version=trim('${VERSION}'); - " > /dev/null -fi -dbfunc_common_upgrade diff --git a/packaging/setup/ovirt_engine_setup/constants.py b/packaging/setup/ovirt_engine_setup/constants.py index 9803211..ee8d23d 100644 --- a/packaging/setup/ovirt_engine_setup/constants.py +++ b/packaging/setup/ovirt_engine_setup/constants.py @@ -161,13 +161,9 @@ OVIRT_ENGINE_DATADIR, 'dbscripts', ) - OVIRT_ENGINE_DB_CREATE = os.path.join( + OVIRT_ENGINE_DB_SCHMA_TOOL = os.path.join( OVIRT_ENGINE_DB_DIR, - 'create_schema.sh', - ) - OVIRT_ENGINE_DB_UPGRADE = os.path.join( - OVIRT_ENGINE_DB_DIR, - 'upgrade.sh', + 'schema.sh', ) OVIRT_ENGINE_DB_BACKUP_DIR = os.path.join( OVIRT_ENGINE_LOCALSTATEDIR, diff --git a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine/db/schema.py b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine/db/schema.py index 871273f..83f8d8e 100644 --- a/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine/db/schema.py +++ b/packaging/setup/plugins/ovirt-engine-setup/ovirt-engine/db/schema.py @@ -216,69 +216,24 @@ @plugin.event( stage=plugin.Stages.STAGE_MISC, name=osetupcons.Stages.DB_SCHEMA, - condition=lambda self: self.environment[ - osetupcons.DBEnv.NEW_DATABASE - ], - ) - def _miscInstall(self): - self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append( - self.SchemaTransaction( - parent=self, - ) - ) - - self.logger.info(_('Creating Engine database schema')) - args = [ - osetupcons.FileLocations.OVIRT_ENGINE_DB_CREATE, - '-l', self.environment[otopicons.CoreEnv.LOG_FILE_NAME], - '-s', self.environment[osetupcons.DBEnv.HOST], - '-p', str(self.environment[osetupcons.DBEnv.PORT]), - '-d', self.environment[osetupcons.DBEnv.DATABASE], - '-u', self.environment[osetupcons.DBEnv.USER], - ] - if self.environment[ - osetupcons.CoreEnv.DEVELOPER_MODE - ]: - if not os.path.exists( - osetupcons.FileLocations.OVIRT_ENGINE_DB_MD5_DIR - ): - os.makedirs( - osetupcons.FileLocations.OVIRT_ENGINE_DB_MD5_DIR - ) - args.extend( - [ - '-m', - osetupcons.FileLocations.OVIRT_ENGINE_DB_MD5_DIR, - ] - ) - self.execute( - args=args, - envAppend={ - 'DBFUNC_DB_PGPASSFILE': self.environment[ - osetupcons.DBEnv.PGPASS_FILE - ] - }, - ) - - @plugin.event( - stage=plugin.Stages.STAGE_MISC, - name=osetupcons.Stages.DB_SCHEMA, - condition=lambda self: not self.environment[ - osetupcons.DBEnv.NEW_DATABASE - ], after=( osetupcons.Stages.DB_CREDENTIALS_AVAILABLE_LATE, ), ) - def _miscUpgrade(self): - dbovirtutils = database.OvirtUtils( - plugin=self, - dbenvkeys=osetupcons.Const.ENGINE_DB_ENV_KEYS, - ) - backupFile = dbovirtutils.backup( - dir=osetupcons.FileLocations.OVIRT_ENGINE_DB_BACKUP_DIR, - prefix=osetupcons.Const.ENGINE_DB_BACKUP_PREFIX, - ) + def _misc(self): + backupFile = None + + if not self.environment[ + osetupcons.DBEnv.NEW_DATABASE + ]: + dbovirtutils = database.OvirtUtils( + plugin=self, + dbenvkeys=osetupcons.Const.ENGINE_DB_ENV_KEYS, + ) + backupFile = dbovirtutils.backup( + dir=osetupcons.FileLocations.OVIRT_ENGINE_DB_BACKUP_DIR, + prefix=osetupcons.Const.ENGINE_DB_BACKUP_PREFIX, + ) self.environment[otopicons.CoreEnv.MAIN_TRANSACTION].append( self.SchemaTransaction( @@ -287,21 +242,15 @@ ) ) - # - # TODO - # rename database - # why do we need rename? - # consider doing that via python - # - - self.logger.info(_('Updating Engine database schema')) + self.logger.info(_('Creating/refreshing Engine database schema')) args = [ - osetupcons.FileLocations.OVIRT_ENGINE_DB_UPGRADE, + osetupcons.FileLocations.OVIRT_ENGINE_DB_SCHMA_TOOL, '-s', self.environment[osetupcons.DBEnv.HOST], '-p', str(self.environment[osetupcons.DBEnv.PORT]), '-u', self.environment[osetupcons.DBEnv.USER], '-d', self.environment[osetupcons.DBEnv.DATABASE], '-l', self.environment[otopicons.CoreEnv.LOG_FILE_NAME], + '-c', 'apply', ] if self.environment[ osetupcons.CoreEnv.DEVELOPER_MODE -- To view, visit http://gerrit.ovirt.org/25223 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie13ee3d4ddf899f680b592ff58fb9d09573dae59 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
