tidbstandalone/import-local.sh

91 lines
3.4 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
echo "🏠 Importing data to local TiDB..."
# Check if local TiDB is accessible
mysql -h 127.0.0.1 -P 4000 -u root -e "SELECT 1" >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo "❌ Cannot connect to local TiDB"
echo "📝 Make sure TiDB is running: ./start.sh"
exit 1
fi
# Check if export files exist
EXPORT_DIR="/tmp/tidb-cloud-export"
if [ ! -f "$EXPORT_DIR/schema.sql" ]; then
echo "❌ Export files not found!"
echo "📝 Run export-cloud.sh first"
exit 1
fi
# Import schema
echo "🏗️ Importing schema..."
mysql -h 127.0.0.1 -P 4000 -u root < $EXPORT_DIR/schema.sql 2>/dev/null
if [ $? -ne 0 ]; then
echo "❌ Schema import failed"
exit 1
fi
echo "✅ Schema imported successfully"
# Import data from CSV files
echo "📥 Importing data..."
for table in ${TABLES//,/ }; do
if [ -f "$EXPORT_DIR/${table}.csv" ]; then
echo " Importing data for table: $table"
# Count lines in CSV (excluding header if present)
line_count=$(wc -l < "$EXPORT_DIR/${table}.csv" | tr -d ' ')
if [ "$line_count" -gt 0 ]; then
# Use LOAD DATA LOCAL INFILE to import CSV
mysql -h 127.0.0.1 -P 4000 -u root --local-infile=1 -e "
USE ${DATABASE_NAME:-workflow_local};
LOAD DATA LOCAL INFILE '$EXPORT_DIR/${table}.csv'
INTO TABLE $table
FIELDS TERMINATED BY '\t'
LINES TERMINATED BY '\n'
IGNORE 0 LINES;
" 2>/dev/null
if [ $? -ne 0 ]; then
echo "⚠️ Warning: Failed to import data for table $table"
# Try alternative method - read CSV and generate INSERT statements
echo " Trying alternative import method..."
while IFS=$'\t' read -r col1 col2 col3 col4 col5; do
# Escape single quotes
col1_escaped=$(echo "$col1" | sed "s/'/''/g")
col2_escaped=$(echo "$col2" | sed "s/'/''/g")
col3_escaped=$(echo "$col3" | sed "s/'/''/g")
col4_escaped=$(echo "$col4" | sed "s/'/''/g")
col5_escaped=$(echo "$col5" | sed "s/'/''/g" | sed "s/NULL//")
# Handle NULL values
if [ "$col5_escaped" = "" ]; then
col5_sql="NULL"
else
col5_sql="'$col5_escaped'"
fi
# Only insert if we have data
if [ -n "$col1" ]; then
mysql -h 127.0.0.1 -P 4000 -u root -e "
USE ${DATABASE_NAME:-workflow_local};
INSERT INTO $table (id, name, description, type, parent_plan_id)
VALUES ('$col1_escaped', '$col2_escaped', '$col3_escaped', '$col4_escaped', $col5_sql);
" 2>/dev/null
fi
done < "$EXPORT_DIR/${table}.csv"
fi
# Count rows imported
row_count=$(mysql -h 127.0.0.1 -P 4000 -u root -e "USE ${DATABASE_NAME:-workflow_local}; SELECT COUNT(*) FROM $table;" -N -s 2>/dev/null)
echo " ✅ Imported $row_count rows into $table"
else
echo " No data to import for table $table"
fi
fi
done
echo "✅ Data import completed"
echo "🎉 Import completed!"