gongxiangp2/scripts/execute-migration.sh

97 lines
2.9 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

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
# 历史一次性生产库迁移执行脚本(Linux/Mac 版本)
# 用途:只针对 20251206000000_add_soft_delete_and_favorites 迁移
# 状态:不是通用生产迁移入口,不属于当前 Git 增量部署流程
set -e
DATABASE_URL="postgresql://gongxiangpan:gongxiangpan123@192.168.58.20:5432/lobechat"
MIGRATION_FILE="$(dirname "$0")/../prisma/migrations/20251206000000_add_soft_delete_and_favorites/migration.sql"
CONTAINER_NAME="gongxiangpan-postgres-1"
echo "========================================"
echo "生产库迁移执行脚本"
echo "========================================"
echo ""
# 检查迁移文件
if [ ! -f "$MIGRATION_FILE" ]; then
echo "❌ 错误:迁移文件不存在"
echo "路径:$MIGRATION_FILE"
exit 1
fi
echo "✅ 迁移文件已找到"
echo ""
# 显示迁移内容摘要
echo "📋 迁移内容摘要:"
echo " - 添加 materials.deletedAt 字段(软删除)"
echo " - 创建 material_favorites 表(收藏)"
echo " - 创建 material_logs 表(操作日志)"
echo " - 创建 material_download_logs 表(下载日志)"
echo ""
# 提示用户确认
echo "⚠️ 重要提示:"
echo " 1. 执行前请确保已备份生产库!"
echo " 2. 迁移过程中应避免用户操作"
echo " 3. 迁移失败可以回滚(使用备份恢复)"
echo ""
read -p "是否继续执行迁移?(yes/no): " confirm
if [ "$confirm" != "yes" ]; then
echo "❌ 已取消迁移"
exit 0
fi
echo ""
echo "🚀 开始执行迁移..."
echo ""
# 检查 Docker 容器
if ! docker ps --filter "name=$CONTAINER_NAME" --format "{{.Status}}" | grep -q "Up"; then
echo "⚠️ PostgreSQL 容器未运行,尝试启动..."
docker start "$CONTAINER_NAME"
sleep 3
else
echo "✅ PostgreSQL 容器已运行"
fi
echo ""
echo "📝 执行迁移 SQL 脚本..."
echo ""
# 执行迁移
if docker exec -i "$CONTAINER_NAME" psql -U gongxiangpan -d lobechat < "$MIGRATION_FILE"; then
echo ""
echo "✅ 迁移执行成功!"
echo ""
echo "📊 迁移结果:"
echo " - materials 表已添加 deletedAt 字段"
echo " - material_favorites 表已创建"
echo " - material_logs 表已创建"
echo " - material_download_logs 表已创建"
echo ""
echo "📋 下一步操作:"
echo " 1. 重新启动应用容器:docker restart youyidian-3d-material"
echo " 2. 重新生成 Prisma Client:npm run prisma:generate"
echo " 3. 测试下载功能是否正常"
echo ""
exit 0
else
echo ""
echo "❌ 迁移执行失败!"
echo "请检查错误信息并重试"
echo ""
echo "💡 故障排查:"
echo " 1. 检查 PostgreSQL 容器是否运行:docker ps | grep postgres"
echo " 2. 检查数据库连接:psql -h 192.168.58.20 -U gongxiangpan -d lobechat"
echo " 3. 查看容器日志:docker logs $CONTAINER_NAME"
echo ""
exit 1
fi