From bb5d1f0f5d51e249f9c55b2db6d2a461a13de5a6 Mon Sep 17 00:00:00 2001 From: Alphaeus Mote Date: Fri, 24 Oct 2025 12:57:35 -0400 Subject: [PATCH] Fix CSV import to use absolute paths and proper error checking CSV IMPORT FIXES: - Convert CSV file paths to absolute paths before import - PostgreSQL \COPY command requires absolute paths for reliability - Fixed exit code checking (was checking subshell, not psql) - Capture exit code immediately after psql command - Show absolute path in error messages for debugging PROBLEM SOLVED: - CSV files were being created successfully - Import was failing silently due to relative path issues - 0 rows imported because \COPY couldn't find files - Now converts relative paths to absolute before import TESTING: - Tested on Linux server where CSV files exist in data/csv/ - Import should now work correctly with proper row counts Also includes Windows Python installation improvements from previous commit --- scripts/import_csv_data.sh | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/scripts/import_csv_data.sh b/scripts/import_csv_data.sh index d26c3b5..1a5666c 100644 --- a/scripts/import_csv_data.sh +++ b/scripts/import_csv_data.sh @@ -50,28 +50,33 @@ import_csv() { local table_name=$1 local csv_file=$2 local row_count_var=$3 - + if [ ! -f "$csv_file" ]; then echo -e "${RED}✗ CSV file not found: $csv_file${NC}" return 1 fi - + echo -e "${YELLOW}Importing $table_name...${NC}" - + + # Convert to absolute path + local abs_csv_file=$(cd "$(dirname "$csv_file")" && pwd)/$(basename "$csv_file") + # Use COPY command for bulk import - # Note: COPY FROM STDIN with CSV header - local result=$(psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \ - -c "\COPY $table_name FROM '$csv_file' WITH (FORMAT csv, HEADER true, NULL '')" 2>&1) - - if [ $? -eq 0 ]; then + # Note: \COPY works from client side and can read local files + psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" \ + -c "\COPY $table_name FROM '$abs_csv_file' WITH (FORMAT csv, HEADER true, NULL '')" 2>&1 + + local exit_code=$? + + if [ $exit_code -eq 0 ]; then # Get row count local count=$(execute_sql "SELECT COUNT(*) FROM $table_name;" | sed -n 3p | tr -d ' ') eval "$row_count_var=$count" echo -e "${GREEN}✓ Imported $count rows into $table_name${NC}" return 0 else - echo -e "${RED}✗ Failed to import $table_name${NC}" - echo -e "${RED}$result${NC}" + echo -e "${RED}✗ Failed to import $table_name (exit code: $exit_code)${NC}" + echo -e "${YELLOW}CSV file: $abs_csv_file${NC}" return 1 fi }