Add company rename to copy mode, add current name when renaming a company

This commit is contained in:
2026-06-24 12:28:05 +02:00
parent ba59f8d039
commit bb3de1ed55
2 changed files with 58 additions and 8 deletions
+56 -6
View File
@@ -1,5 +1,5 @@
#!/bin/bash #!/bin/bash
# Version: 1.8.0 # Version: 1.9.0
# Author: Tomi Eckert # Author: Tomi Eckert
# ============================================================================== # ==============================================================================
# HANA Database Manager Menu (hanamgr.sh) # HANA Database Manager Menu (hanamgr.sh)
@@ -28,6 +28,15 @@ check_hdbsql() {
fi fi
} }
# Function to fetch the current company name from a schema
get_company_name() {
local key="$1"
local schema="$2"
local result
result=$("$HDBSQL_CMD" -U "$key" -a -x "SELECT \"CompnyName\" FROM \"${schema}\".OADM;" 2>/dev/null | tr -d '"' | xargs)
echo "$result"
}
# Function to execute a SQL query and capture the exit code # Function to execute a SQL query and capture the exit code
execute_sql() { execute_sql() {
local key="$1" local key="$1"
@@ -315,13 +324,32 @@ do_copy() {
read -p "Replace existing objects in target schema? (y/N): " replace_opt read -p "Replace existing objects in target schema? (y/N): " replace_opt
read -p "Rename company name in the copied schema? [Y/n]: " rename_company_opt
rename_company_opt="${rename_company_opt:-Y}"
local new_compny_name=""
if [[ "$rename_company_opt" =~ ^[Yy]$ ]]; then
local current_name
current_name=$(get_company_name "$GLOBAL_USER_KEY" "$source_schema")
if [[ -n "$current_name" ]]; then
echo " Current company name (source): ${current_name}"
fi
read -p "Enter NEW Company Name: " new_compny_name
if [[ -z "$new_compny_name" ]]; then
echo -e "\033[33m[WARN] No company name entered, skipping rename.\033[0m"
rename_company_opt="N"
fi
fi
if [[ -z "$GLOBAL_USER_KEY" || -z "$target_schema" || -z "$export_path" ]]; then if [[ -z "$GLOBAL_USER_KEY" || -z "$target_schema" || -z "$export_path" ]]; then
echo -e "\033[31m[ERROR] Key, Target Schema, and Path are required.\033[0m" echo -e "\033[31m[ERROR] Key, Target Schema, and Path are required.\033[0m"
return return
fi fi
local total_steps=2
[[ "$rename_company_opt" =~ ^[Yy]$ ]] && total_steps=3
# 1. Export # 1. Export
echo -e "\n-Step 1/2: Exporting source schema '${source_schema}' to '${export_path}'..." echo -e "\n- Step 1/${total_steps}: Exporting source schema '${source_schema}' to '${export_path}'..."
mkdir -p "$export_path" mkdir -p "$export_path"
local temp_export_dir=$(mktemp -d "${export_path}/copy_export_${source_schema}_XXXXXXXX") local temp_export_dir=$(mktemp -d "${export_path}/copy_export_${source_schema}_XXXXXXXX")
echo "- Using temporary export directory: ${temp_export_dir}" echo "- Using temporary export directory: ${temp_export_dir}"
@@ -336,7 +364,7 @@ do_copy() {
echo -e "\033[32m[DONE] Export phase completed successfully.\033[0m" echo -e "\033[32m[DONE] Export phase completed successfully.\033[0m"
# 2. Import-Rename # 2. Import-Rename
echo -e "\n-Step 2/2: Importing and renaming to '${target_schema}'..." echo -e "\n- Step 2/${total_steps}: Importing and renaming schema to '${target_schema}'..."
local import_options="IGNORE EXISTING" local import_options="IGNORE EXISTING"
if [[ "$replace_opt" =~ ^[Yy]$ ]]; then if [[ "$replace_opt" =~ ^[Yy]$ ]]; then
import_options="REPLACE" import_options="REPLACE"
@@ -345,15 +373,32 @@ do_copy() {
local import_query="IMPORT \"${source_schema}\".\"*\" AS BINARY FROM '${temp_export_dir}' WITH ${import_options} THREADS ${threads};" local import_query="IMPORT \"${source_schema}\".\"*\" AS BINARY FROM '${temp_export_dir}' WITH ${import_options} THREADS ${threads};"
if execute_sql "$GLOBAL_USER_KEY" "$import_query"; then if ! execute_sql "$GLOBAL_USER_KEY" "$import_query"; then
echo -e "\033[32m[DONE] Successfully copied schema '${source_schema}' to '${target_schema}'.\033[0m"
else
echo -e "\033[31m[ERROR] Import phase failed.\033[0m" echo -e "\033[31m[ERROR] Import phase failed.\033[0m"
echo "- Cleaning up temporary files..."
rm -rf "$temp_export_dir"
return
fi fi
echo -e "\033[32m[DONE] Successfully copied schema '${source_schema}' to '${target_schema}'.\033[0m"
# Cleanup # Cleanup
echo "- Cleaning up temporary files..." echo "- Cleaning up temporary files..."
rm -rf "$temp_export_dir" rm -rf "$temp_export_dir"
# 3. Rename company name (optional)
if [[ "$rename_company_opt" =~ ^[Yy]$ ]]; then
echo -e "\n- Step 3/${total_steps}: Renaming company name in '${target_schema}' to '${new_compny_name}'..."
echo " -> Updating CINF table..."
local q_cinf="UPDATE \"${target_schema}\".CINF SET \"CompnyName\" = '${new_compny_name}';"
execute_sql "$GLOBAL_USER_KEY" "$q_cinf" > /dev/null
echo " -> Updating OADM table..."
local q_oadm="UPDATE \"${target_schema}\".OADM SET \"CompnyName\" = '${new_compny_name}', \"PrintHeadr\" = '${new_compny_name}';"
execute_sql "$GLOBAL_USER_KEY" "$q_oadm" > /dev/null
echo -e "\033[32m[DONE] Company name updated successfully.\033[0m"
fi
} }
do_backup() { do_backup() {
@@ -429,6 +474,11 @@ do_rename_db() {
echo -e "\n\033[1m=== Rename Database (Company Name) ===\033[0m" echo -e "\n\033[1m=== Rename Database (Company Name) ===\033[0m"
select_schema "$GLOBAL_USER_KEY" select_schema "$GLOBAL_USER_KEY"
local schema_name="$SELECTED_SCHEMA" local schema_name="$SELECTED_SCHEMA"
local current_name
current_name=$(get_company_name "$GLOBAL_USER_KEY" "$schema_name")
if [[ -n "$current_name" ]]; then
echo " Current company name: ${current_name}"
fi
read -p "Enter NEW Company Name: " new_compny_name read -p "Enter NEW Company Name: " new_compny_name
if [[ -z "$GLOBAL_USER_KEY" || -z "$schema_name" || -z "$new_compny_name" ]]; then if [[ -z "$GLOBAL_USER_KEY" || -z "$schema_name" || -z "$new_compny_name" ]]; then
+1 -1
View File
@@ -16,4 +16,4 @@ SCRIPT_PACKAGES["monitor"]="Monitor Suite|1.3.1|Scripts for monitoring system he
SCRIPT_PACKAGES["keymanager"]="Key Manager|1.2.3|A utility for managing HDB user keys for SAP HANA.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/keymanager.sh" SCRIPT_PACKAGES["keymanager"]="Key Manager|1.2.3|A utility for managing HDB user keys for SAP HANA.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/keymanager.sh"
SCRIPT_PACKAGES["cleaner"]="File Cleaner|1.1.0|A simple script to clean up temporary files and logs.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/cleaner.sh" SCRIPT_PACKAGES["cleaner"]="File Cleaner|1.1.0|A simple script to clean up temporary files and logs.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/cleaner.sh"
SCRIPT_PACKAGES["hanatool"]="HANA Tool|1.6.0|A command-line tool for various SAP HANA administration tasks.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanatool.sh" SCRIPT_PACKAGES["hanatool"]="HANA Tool|1.6.0|A command-line tool for various SAP HANA administration tasks.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanatool.sh"
SCRIPT_PACKAGES["hanamgr"]="HANA Manager UI|1.8.0|An interactive command-line menu for managing SAP HANA schemas and databases.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanamgr.sh" SCRIPT_PACKAGES["hanamgr"]="HANA Manager UI|1.9.0|An interactive command-line menu for managing SAP HANA schemas and databases.|https://git.technopunk.space/tomi/Scripts/raw/branch/main/hanamgr.sh"