001project_wildgrowth/backend/deploy/scripts/migration-fix-and-start.sh

101 lines
3.5 KiB
Bash
Raw Permalink 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
# ============================================================
# 迁移收尾:修 P3015删残缺 migration 目录、migrate deploy、启动、健康检查
# 用法:在服务器上
# cd /var/www/wildgrowth-backend/backend && bash deploy/scripts/migration-fix-and-start.sh
# 或(若脚本在 deploy 下bash deploy/scripts/migration-fix-and-start.sh
# ============================================================
set -e
GIT_ROOT="${GIT_ROOT:-/var/www/wildgrowth-backend}"
APP_ROOT="${APP_ROOT:-/var/www/wildgrowth-backend/backend}"
MIGRATIONS="$APP_ROOT/prisma/migrations"
echo "═══════════════════════════════════════════════════════════"
echo " 迁移收尾:修复 P3015、migrate、启动、健康检查"
echo " APP_ROOT=$APP_ROOT"
echo "═══════════════════════════════════════════════════════════"
if [ ! -d "$APP_ROOT" ]; then
echo "❌ 错误: $APP_ROOT 不存在"
exit 1
fi
if [ ! -f "$APP_ROOT/.env" ]; then
echo "❌ 错误: $APP_ROOT/.env 不存在"
exit 1
fi
cd "$APP_ROOT"
# 1. 删除缺 migration.sql 的目录(解决 P3015
echo ""
echo "1⃣ 检查 migrations删除残缺目录..."
if [ -d "$MIGRATIONS" ]; then
for d in "$MIGRATIONS"/*/; do
[ -d "$d" ] || continue
name=$(basename "$d")
if [ ! -f "${d}migration.sql" ]; then
echo " 删除残缺目录(无 migration.sql: $name"
rm -rf "$d"
else
echo " OK: $name"
fi
done
else
echo " ⚠️ prisma/migrations 不存在,跳过"
fi
# 2. Prisma migrate deploy
echo ""
echo "2⃣ 执行 prisma migrate deploy..."
npx prisma migrate deploy
echo "✅ 迁移完成"
# 3. 若未 build 则 build迁移后可能已有 dist避免重复
if [ ! -d "dist" ] || [ ! -f "dist/index.js" ]; then
echo ""
echo "3⃣ 构建..."
npm run build
else
echo ""
echo "3⃣ dist 已存在,跳过 build"
fi
# 4. 启动
echo ""
echo "4⃣ 启动服务..."
if pm2 describe wildgrowth-api >/dev/null 2>&1; then
pm2 restart wildgrowth-api
echo " pm2 restart wildgrowth-api"
else
# 未在 pm2 中则 start
pm2 start dist/index.js --name wildgrowth-api
echo " pm2 start dist/index.js --name wildgrowth-api"
fi
# 5. 健康检查
echo ""
echo "5⃣ 健康检查..."
sleep 3
if curl -sf http://localhost:3000/health >/dev/null; then
echo "✅ 本机 health 正常"
else
echo "❌ 本机 health 失败: curl http://localhost:3000/health"
pm2 logs wildgrowth-api --lines 30 --nostream
exit 1
fi
# 6. 记录 .deploy-last若在 Git 根可拿到 HEAD
if [ -d "$GIT_ROOT/.git" ]; then
(cd "$GIT_ROOT" && git rev-parse HEAD) > "$APP_ROOT/.deploy-last"
echo " .deploy-last 已更新"
fi
echo ""
echo "═══════════════════════════════════════════════════════════"
echo " ✅ 迁移收尾完成,服务已启动"
echo " 请自测: https://api.muststudy.xin/health"
echo " 回滚: bash deploy/deploy-from-github.sh rollback"
echo "═══════════════════════════════════════════════════════════"