mirror of
https://github.com/freedbygrace/SQL.git
synced 2026-07-26 11:28:16 +00:00
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
This commit is contained in:
@@ -58,20 +58,25 @@ import_csv() {
|
||||
|
||||
echo -e "${YELLOW}Importing $table_name...${NC}"
|
||||
|
||||
# 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)
|
||||
# Convert to absolute path
|
||||
local abs_csv_file=$(cd "$(dirname "$csv_file")" && pwd)/$(basename "$csv_file")
|
||||
|
||||
if [ $? -eq 0 ]; then
|
||||
# Use COPY command for bulk import
|
||||
# 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user