flnxi-Wu/scripts/tools/verify-import.sh
WuYongJie f646b71813
Some checks failed
CI Check / check (push) Has been cancelled
chore: 初始化本地仓库并同步最近改动
2025-11-18 17:50:16 +08:00

99 lines
2.8 KiB
Bash

#!/bin/bash
# 验证数据库导入结果
DB_HOST="192.168.58.20"
DB_USER="gongxiangpan"
DB_PASS="gongxiangpan123"
DB_NAME="lobechat"
echo "=========================================="
echo " 数据库验证工具"
echo "=========================================="
echo ""
export PGPASSWORD=$DB_PASS
echo "[1] 检查数据库连接..."
if psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT 1" > /dev/null 2>&1; then
echo "✅ 连接成功"
else
echo "❌ 连接失败"
exit 1
fi
echo ""
echo "[2] 数据库信息..."
echo "数据库大小:"
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT pg_size_pretty(pg_database_size('$DB_NAME'));"
echo ""
echo "[3] 表列表和记录数..."
psql -h $DB_HOST -U $DB_USER -d $DB_NAME << 'EOF'
SELECT
schemaname,
tablename,
pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS size,
(SELECT COUNT(*) FROM information_schema.columns WHERE table_name = t.tablename) AS columns
FROM pg_tables t
WHERE schemaname = 'public'
ORDER BY tablename;
EOF
echo ""
echo "[4] 主要表记录数..."
for table in users materials categories scenes material_likes system_settings; do
if psql -h $DB_HOST -U $DB_USER -d $DB_NAME -t -c "SELECT 1 FROM information_schema.tables WHERE table_name='$table'" | grep -q 1; then
COUNT=$(psql -h $DB_HOST -U $DB_USER -d $DB_NAME -t -c "SELECT COUNT(*) FROM \"$table\"" 2>/dev/null | xargs)
if [ ! -z "$COUNT" ]; then
printf " %-20s : %s 条记录\n" "$table" "$COUNT"
fi
fi
done
echo ""
echo "[5] 最近的数据..."
echo "最近创建的用户:"
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT id, username, email, created_at FROM users ORDER BY created_at DESC LIMIT 5;" 2>/dev/null || echo " 表不存在或无数据"
echo ""
echo "最近上传的素材:"
psql -h $DB_HOST -U $DB_USER -d $DB_NAME -c "SELECT id, title, created_at FROM materials ORDER BY created_at DESC LIMIT 5;" 2>/dev/null || echo " 表不存在或无数据"
echo ""
echo "[6] 索引检查..."
psql -h $DB_HOST -U $DB_USER -d $DB_NAME << 'EOF'
SELECT
tablename,
indexname,
indexdef
FROM pg_indexes
WHERE schemaname = 'public'
ORDER BY tablename, indexname;
EOF
echo ""
echo "[7] 外键约束..."
psql -h $DB_HOST -U $DB_USER -d $DB_NAME << 'EOF'
SELECT
tc.table_name,
kcu.column_name,
ccu.table_name AS foreign_table_name,
ccu.column_name AS foreign_column_name
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
ORDER BY tc.table_name;
EOF
echo ""
echo "=========================================="
echo " 验证完成"
echo "=========================================="
echo ""