Add PostgresSubprocessException and refactor database initialization logic

- Introduced PostgresSubprocessException to handle errors during PostgreSQL subprocess execution.
- Refactored PostgresDatabaseProvider to improve schema initialization script discovery.
- Enhanced error handling for psql command execution, including logging of output and errors.
- Implemented methods to find the schema initialization script and the psql executable path.
This commit is contained in:
2026-07-08 11:53:36 -04:00
parent 4f651fc239
commit 1abf61a05f
52 changed files with 723 additions and 9854268 deletions
@@ -91,7 +91,8 @@ done
set -e # Exit on error
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../../.." && pwd)"
SQL_DIR="$PROJECT_ROOT/scripts/sql"
# Load database configuration
source "./db-config.sh"
@@ -116,24 +117,15 @@ invoke_diagnostic_report() {
local output_file="$1"
write_color_output "\nRunning comprehensive diagnostics..." "$CYAN"
if PGPASSWORD=$DB_PASS $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f "$PROJECT_ROOT/sql/diagnostics.sql" > "$output_file" 2>&1; then
run_sql_files_from_dir "$SQL_DIR/diagnostics" "$output_file"
if [ $? -eq 0 ]; then
write_color_output "[OK] Diagnostics completed: $output_file" "$GREEN"
else
write_color_output "[WARN] Diagnostics completed with warnings" "$YELLOW"
fi
}
invoke_query_analysis() {
local output_file="$1"
write_color_output "\nAnalyzing slow queries..." "$CYAN"
if PGPASSWORD=$DB_PASS $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f "$PROJECT_ROOT/sql/query-analysis.sql" > "$output_file" 2>&1; then
write_color_output "[OK] Query analysis completed: $output_file" "$GREEN"
else
write_color_output "[WARN] Query analysis completed with warnings" "$YELLOW"
fi
}
get_index_usage_stats() {
local output_file="$1"
write_color_output "\nGathering index usage statistics..." "$CYAN"
@@ -269,7 +261,6 @@ Week Number: $(get_week_number)
REPORTS GENERATED:
* diagnostics_$timestamp.txt : Full diagnostic report
* query_analysis_$timestamp.txt : Slow query analysis
* index_usage_$timestamp.txt : Index usage statistics
* table_sizes_$timestamp.txt : Table size and bloat info
* summary_$timestamp.txt : This summary
@@ -312,6 +303,33 @@ EOF
write_color_output "[OK] Summary generated: $summary_file" "$GREEN"
}
# ============================================================================
# Helper Functions
# ============================================================================
run_sql_files_from_dir() {
local sql_dir="$1"
local output_file="$2"
if [ ! -d "$sql_dir" ]; then
write_color_output "[ERROR] Directory not found: $sql_dir" "$RED"
return 1
fi
# Clear the output file before running scripts
> "$output_file"
for sql_file in "$sql_dir"/*.sql; do
echo -e "\n---\n-- Executing: $(basename "$sql_file")\n---\n" >> "$output_file"
write_color_output "Running SQL file: $(basename "$sql_file")" "$CYAN"
if PGPASSWORD=$DB_PASS $PSQL_PATH -h $DB_HOST -p $DB_PORT -U $DB_USER -d $DB_NAME -f "$sql_file" >> "$output_file" 2>&1; then
write_color_output "[OK] Successfully executed $(basename "$sql_file")" "$GREEN"
else
write_color_output "[WARN] Completed with warnings: $(basename "$sql_file")" "$YELLOW"
fi
done
}
# ============================================================================
# Main Execution
# ============================================================================
@@ -343,10 +361,6 @@ fi
diagnostics_file="$report_dir/diagnostics_$timestamp.txt"
invoke_diagnostic_report "$diagnostics_file"
# Run query analysis
query_analysis_file="$report_dir/query_analysis_$timestamp.txt"
invoke_query_analysis "$query_analysis_file"
# Gather index usage stats
index_usage_file="$report_dir/index_usage_$timestamp.txt"
get_index_usage_stats "$index_usage_file"