#!/bin/bash set -euo pipefail APP=/opt/ai-fresh-delivery SERVER=$APP/server cd "$SERVER" export $(grep -m1 '^DATABASE_URL=' .env | xargs) echo "========== 生产数据库诊断 $(date -Iseconds) ==========" node -e " const u = new URL(process.env.DATABASE_URL); console.log('DATABASE_URL:'); console.log(' host :', u.hostname); console.log(' port :', u.port || '5432'); console.log(' database:', u.pathname.slice(1)); console.log(' user :', u.username); console.log(' password:', decodeURIComponent(u.password || '')); " echo echo "=== PostgreSQL 服务 ===" systemctl is-active postgresql 2>/dev/null || systemctl is-active postgresql@* 2>/dev/null || echo "unknown" echo echo "=== 端口探测 ===" for target in "127.0.0.1:5432" "10.206.16.13:5432"; do host=${target%%:*} port=${target##*:} if timeout 3 bash -c "cat < /dev/null > /dev/tcp/$host/$port" 2>/dev/null; then echo "$target -> 可达" else echo "$target -> 不可达/超时" fi done echo echo "=== 当前配置库连接 ===" PGPASSWORD=$(node -e "const u=new URL(process.argv[1]); console.log(decodeURIComponent(u.password||''))" "$DATABASE_URL") export PGPASSWORD if psql "$DATABASE_URL" -c 'SELECT version()' 2>/dev/null | head -3; then echo "连接成功" else echo "连接失败" exit 1 fi echo echo "=== 本机所有数据库 ===" sudo -u postgres psql -tAc "SELECT datname, pg_size_pretty(pg_database_size(datname)) FROM pg_database WHERE datistemplate=false ORDER BY datname;" echo echo "=== 迁移进度 ===" applied=$(psql "$DATABASE_URL" -tAc "SELECT COUNT(*) FROM schema_migrations" 2>/dev/null || echo 0) total=$(ls -1 "$APP/migrations"/*.sql 2>/dev/null | wc -l) echo "已应用: $applied / 文件总数: $total" psql "$DATABASE_URL" -tAc "SELECT filename FROM schema_migrations ORDER BY filename DESC LIMIT 3;" 2>/dev/null | sed 's/^/ 最近: /' echo echo "=== 核心业务表行数 ===" psql "$DATABASE_URL" <<'SQL' SELECT 'auth.users' AS tbl, COUNT(*)::text AS cnt FROM auth.users UNION ALL SELECT 'profiles', COUNT(*)::text FROM profiles UNION ALL SELECT 'user_roles', COUNT(*)::text FROM user_roles UNION ALL SELECT 'branches', COUNT(*)::text FROM branches UNION ALL SELECT 'customers', COUNT(*)::text FROM customers UNION ALL SELECT 'suppliers', COUNT(*)::text FROM suppliers UNION ALL SELECT 'skus', COUNT(*)::text FROM skus UNION ALL SELECT 'categories', COUNT(*)::text FROM categories UNION ALL SELECT 'orders', COUNT(*)::text FROM orders UNION ALL SELECT 'order_items', COUNT(*)::text FROM order_items UNION ALL SELECT 'purchase_orders', COUNT(*)::text FROM purchase_orders UNION ALL SELECT 'inventory', COUNT(*)::text FROM inventory ORDER BY tbl; SQL echo echo "=== 用户账号 ===" psql "$DATABASE_URL" -c "SELECT email, (raw_user_meta_data->>'username') AS username FROM auth.users ORDER BY email LIMIT 10;" echo echo "=== 尝试原 DB 内网 10.206.16.13 (hermes) ===" OLD_URL=$(node -e " const u = new URL(process.env.DATABASE_URL); u.hostname = '10.206.16.13'; console.log(u.toString()); ") if timeout 5 psql "$OLD_URL" -c 'SELECT COUNT(*) AS orders FROM orders' 2>/dev/null; then echo "原库可连接!建议切回 DATABASE_URL 指向 10.206.16.13" else echo "原库仍不可连接(实例可能关机或网络不通)" fi echo echo "========== 诊断结束 =========="