From 2512f434f9c8886833a5987cc29c905c436e7a04 Mon Sep 17 00:00:00 2001 From: importer system account Date: Thu, 17 Oct 2024 13:35:43 -0400 Subject: [PATCH] separate get_database_currently_in_production.sh --- .../get_database_currently_in_production.sh | 110 ++++++++++++++++++ .../set_update_process_state.sh | 25 ---- 2 files changed, 110 insertions(+), 25 deletions(-) create mode 100755 scripts/clickhouse_import_support/get_database_currently_in_production.sh diff --git a/scripts/clickhouse_import_support/get_database_currently_in_production.sh b/scripts/clickhouse_import_support/get_database_currently_in_production.sh new file mode 100755 index 0000000..0cd0803 --- /dev/null +++ b/scripts/clickhouse_import_support/get_database_currently_in_production.sh @@ -0,0 +1,110 @@ +#!/usr/bin/env bash + +# load dependencies +unset this_script_dir +this_script_dir="$(dirname "$(readlink -f $0)")" +if ! source "$this_script_dir/parse_property_file_functions.sh" ; then + echo "Error : unable to load dependency : $this_script_dir/parse_property_file_functions.sh" >&2 + exit 1 +fi +if ! source "$this_script_dir/mysql_command_line_functions.sh" ; then + echo "Error : unable to load dependency : $this_script_dir/mysql_command_line_functions.sh" >&2 + exit 1 +fi +unset this_script_dir + +# other non-local environment variables in use +unset my_properties +unset update_management_database_name +update_management_database_name="" +unset database_currently_in_production +database_currently_in_production="" +declare -A my_properties +record_count_filepath="$(pwd)/update_status_record_count.txt" +current_production_database_filepath="$(pwd)/current_production_database.txt" + +function usage() { + echo "usage: get_database_currently_in_production.sh properties_filepath" >&2 +} + +function initialize_main() { + local properties_filepath=$1 + local state=$2 + if ! parse_property_file "$properties_filepath" my_properties ; then + usage + return 1 + fi + if ! initialize_mysql_command_line_functions ; then + usage + return 1 + fi + update_management_database_name="${my_properties['mysql_update_management_database']}" + ### TODO : fix this + ### remove_credentials_from_properties my_properties # no longer needed - remove for security + return 0 +} + +function delete_output_stream_files() { + rm -f "$record_count_filepath" + rm -f "$current_production_database_filepath" +} + +function shutdown_main_and_clean_up() { + shutdown_mysql_command_line_functions + delete_output_stream_files + unset my_properties + unset record_count_filepath + unset current_production_database_filepath +} + +function process_state_table_is_valid() { + if ! database_exists "$update_management_database_name" ; then + echo "Error : could not proceed with getting production database because database does not exist: $update_management_database_name" >&2 + return 1 + fi + if ! table_exists "$update_management_database_name" "update_status" ; then + echo "Error : could not proceed with getting production database because table 'update_status' does not exist in database : $update_management_database_name" >&2 + fi + local get_record_count_statement="SELECT count(*) AS record_count from \`$update_management_database_name\`.update_status;" + if ! execute_sql_statement_via_mysql "$get_record_count_statement" "$record_count_filepath" ; then + echo "Error : could not validate process_state table. Mysql statement failed to execute properly : $get_record_count_statement" >&2 + return 1 + fi + set_sql_data_array_from_file "$record_count_filepath" 0 + local rowcount="${sql_data_array[0]}" + if [[ "$rowcount" -ne 1 ]] ; then + echo "Error : database $update_management_database_name contains $rowcount rows instead of exactly 1 row as expected." >&2 + return 1 + fi + return 0 +} + +function set_database_currently_in_production() { + local get_current_database_statement="SELECT current_database_in_production FROM \`$update_management_database_name\`.update_status;" + if ! execute_sql_statement_via_mysql "$get_current_database_statement" "$current_production_database_filepath" ; then + echo "Error : could not retrieve the current database in production. Mysql statement failed to execute properly : $get_current_database_statement" >&2 + return 1 + fi + set_sql_data_array_from_file "$current_production_database_filepath" 0 + database_currently_in_production="${sql_data_array[0]}" + return 0 +} + +function output_database_currently_in_production() { + echo "$database_currently_in_production : current production database" +} + +function main() { + local properties_filepath=$1 + local exit_status=0 + if ! initialize_main "$properties_filepath" || + ! process_state_table_is_valid || + ! set_database_currently_in_production ; then + exit_status=1 + fi + output_database_currently_in_production + shutdown_main_and_clean_up + return $exit_status +} + +main "$1" diff --git a/scripts/clickhouse_import_support/set_update_process_state.sh b/scripts/clickhouse_import_support/set_update_process_state.sh index 73ed3cc..c712c39 100755 --- a/scripts/clickhouse_import_support/set_update_process_state.sh +++ b/scripts/clickhouse_import_support/set_update_process_state.sh @@ -17,12 +17,9 @@ unset this_script_dir unset my_properties unset update_management_database_name update_management_database_name="" -unset database_currently_in_production -database_currently_in_production="" declare -A my_properties record_count_filepath="$(pwd)/update_status_record_count.txt" update_status_filepath="$(pwd)/update_status.txt" -current_production_database_filepath="$(pwd)/current_production_database.txt" function usage() { echo "usage: set_update_process_state.sh properties_filepath state" >&2 @@ -54,7 +51,6 @@ function initialize_main() { function delete_output_stream_files() { rm -f "$record_count_filepath" rm -f "$update_status_filepath" - rm -f "$current_production_database_filepath" } function shutdown_main_and_clean_up() { @@ -63,7 +59,6 @@ function shutdown_main_and_clean_up() { unset my_properties unset record_count_filepath unset update_status_filepath - unset current_production_database_filepath } function process_state_table_is_valid() { @@ -127,35 +122,15 @@ function set_state_in_status_table() { return 0 } -function set_database_currently_in_production() { - local get_current_database_statement="SELECT current_database_in_production FROM \`$update_management_database_name\`.update_status;" - if ! execute_sql_statement_via_mysql "$get_current_database_statement" "$current_production_database_filepath" ; then - echo "Error : could not retrieve the current database in production. Mysql statement failed to execute properly : $get_current_database_statement" >&2 - return 1 - fi - set_sql_data_array_from_file "$current_production_database_filepath" 0 - database_currently_in_production="${sql_data_array[0]}" - return 0 -} - -function output_database_currently_in_production() { - local state=$1 - if [ "$state" == "running" ] ; then - echo "$database_currently_in_production : current production database" - fi -} - function main() { local properties_filepath=$1 local state=$2 local exit_status=0 if ! initialize_main "$properties_filepath" "$state" || ! process_state_table_is_valid || - ! set_database_currently_in_production || ! set_state_in_status_table "$state" ; then exit_status=1 fi - output_database_currently_in_production "$state" shutdown_main_and_clean_up return $exit_status }