Skip to content

Commit

Permalink
separate get_database_currently_in_production.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
importer system account committed Oct 17, 2024
1 parent bc39952 commit 2512f43
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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"
25 changes: 0 additions & 25 deletions scripts/clickhouse_import_support/set_update_process_state.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2512f43

Please sign in to comment.