From bb3de1ed55670c3ac77ef2cbf3ced2f6e4c55ff3 Mon Sep 17 00:00:00 2001 From: TomiEckert Date: Wed, 24 Jun 2026 12:28:05 +0200 Subject: [PATCH] Add company rename to copy mode, add current name when renaming a company --- hanamgr.sh | 64 +++++++++++++++++++++++++++++++++++++++++++++------ packages.conf | 2 +- 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/hanamgr.sh b/hanamgr.sh index fd8b41d..ceac6a7 100755 --- a/hanamgr.sh +++ b/hanamgr.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Version: 1.8.0 +# Version: 1.9.0 # Author: Tomi Eckert # ============================================================================== # HANA Database Manager Menu (hanamgr.sh) @@ -28,6 +28,15 @@ check_hdbsql() { 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 execute_sql() { local key="$1" @@ -315,16 +324,35 @@ do_copy() { 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 echo -e "\033[31m[ERROR] Key, Target Schema, and Path are required.\033[0m" return fi + local total_steps=2 + [[ "$rename_company_opt" =~ ^[Yy]$ ]] && total_steps=3 + # 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" 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}" local export_query="EXPORT \"${source_schema}\".\"*\" AS BINARY INTO '${temp_export_dir}' WITH REPLACE THREADS ${threads} NO DEPENDENCIES;" @@ -336,7 +364,7 @@ do_copy() { echo -e "\033[32m[DONE] Export phase completed successfully.\033[0m" # 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" if [[ "$replace_opt" =~ ^[Yy]$ ]]; then 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};" - 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 + if ! execute_sql "$GLOBAL_USER_KEY" "$import_query"; then echo -e "\033[31m[ERROR] Import phase failed.\033[0m" + echo "- Cleaning up temporary files..." + rm -rf "$temp_export_dir" + return fi + echo -e "\033[32m[DONE] Successfully copied schema '${source_schema}' to '${target_schema}'.\033[0m" # Cleanup echo "- Cleaning up temporary files..." 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() { @@ -429,6 +474,11 @@ do_rename_db() { echo -e "\n\033[1m=== Rename Database (Company Name) ===\033[0m" select_schema "$GLOBAL_USER_KEY" 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 if [[ -z "$GLOBAL_USER_KEY" || -z "$schema_name" || -z "$new_compny_name" ]]; then diff --git a/packages.conf b/packages.conf index abb88da..9afd077 100644 --- a/packages.conf +++ b/packages.conf @@ -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["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["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"