gongxiangp2/scripts/execute-migration.ps1

116 lines
4.7 KiB
PowerShell
Raw Permalink 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.

# 历史一次性生产库迁移执行脚本
# 用途:只针对 20251206000000_add_soft_delete_and_favorites 迁移
# 状态:不是通用生产迁移入口,不属于当前 Git 增量部署流程
# 注意:执行前请确保已备份生产库!
param(
[string]$DatabaseUrl = $env:DATABASE_URL,
[string]$MigrationFile = "d:\gongxiangp2\prisma\migrations\20251206000000_add_soft_delete_and_favorites\migration.sql",
[string]$ContainerName = "gongxiangpan-postgres-1"
)
if ([string]::IsNullOrWhiteSpace($DatabaseUrl)) {
throw '缺少数据库连接,请先设置 DATABASE_URL 环境变量'
}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "生产库迁移执行脚本" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
# 检查迁移文件是否存在
if (-not (Test-Path $MigrationFile)) {
Write-Host "❌ 错误:迁移文件不存在" -ForegroundColor Red
Write-Host "路径:$MigrationFile" -ForegroundColor Red
exit 1
}
Write-Host "✅ 迁移文件已找到:$MigrationFile" -ForegroundColor Green
Write-Host ""
# 显示迁移内容摘要
Write-Host "📋 迁移内容摘要:" -ForegroundColor Yellow
Write-Host " - 添加 materials.deletedAt 字段(软删除)" -ForegroundColor Yellow
Write-Host " - 创建 material_favorites 表(收藏)" -ForegroundColor Yellow
Write-Host " - 创建 material_logs 表(操作日志)" -ForegroundColor Yellow
Write-Host " - 创建 material_download_logs 表(下载日志)" -ForegroundColor Yellow
Write-Host ""
# 提示用户确认
Write-Host "⚠️ 重要提示:" -ForegroundColor Yellow
Write-Host " 1. 执行前请确保已备份生产库!" -ForegroundColor Yellow
Write-Host " 2. 迁移过程中应避免用户操作" -ForegroundColor Yellow
Write-Host " 3. 迁移失败可以回滚(使用备份恢复)" -ForegroundColor Yellow
Write-Host ""
$confirm = Read-Host "是否继续执行迁移?(yes/no)"
if ($confirm -ne "yes") {
Write-Host "❌ 已取消迁移" -ForegroundColor Red
exit 0
}
Write-Host ""
Write-Host "🚀 开始执行迁移..." -ForegroundColor Cyan
Write-Host ""
# 检查 Docker 容器是否运行
try {
$containerStatus = docker ps --filter "name=$ContainerName" --format "{{.Status}}"
if ([string]::IsNullOrEmpty($containerStatus)) {
Write-Host "⚠️ PostgreSQL 容器未运行,尝试启动..." -ForegroundColor Yellow
docker start $ContainerName
Start-Sleep -Seconds 3
} else {
Write-Host "✅ PostgreSQL 容器已运行" -ForegroundColor Green
}
} catch {
Write-Host "⚠️ 无法检查 Docker 容器,假设 PostgreSQL 可访问" -ForegroundColor Yellow
}
Write-Host ""
# 执行迁移
try {
Write-Host "📝 执行迁移 SQL 脚本..." -ForegroundColor Cyan
# 方式1:通过 Docker 容器执行(推荐)
$migrationContent = Get-Content -Path $MigrationFile -Raw
$migrationContent | docker exec -i $ContainerName psql -U gongxiangpan -d lobechat
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "✅ 迁移执行成功!" -ForegroundColor Green
Write-Host ""
Write-Host "📊 迁移结果:" -ForegroundColor Green
Write-Host " - materials 表已添加 deletedAt 字段" -ForegroundColor Green
Write-Host " - material_favorites 表已创建" -ForegroundColor Green
Write-Host " - material_logs 表已创建" -ForegroundColor Green
Write-Host " - material_download_logs 表已创建" -ForegroundColor Green
Write-Host ""
# 提示下一步
Write-Host "📋 下一步操作:" -ForegroundColor Cyan
Write-Host " 1. 重新启动应用容器:docker restart youyidian-3d-material" -ForegroundColor Cyan
Write-Host " 2. 重新生成 Prisma Client:npm run prisma:generate" -ForegroundColor Cyan
Write-Host " 3. 测试下载功能是否正常" -ForegroundColor Cyan
Write-Host ""
exit 0
} else {
Write-Host ""
Write-Host "❌ 迁移执行失败!" -ForegroundColor Red
Write-Host "请检查错误信息并重试" -ForegroundColor Red
exit 1
}
} catch {
Write-Host ""
Write-Host "❌ 执行迁移时出错:$_" -ForegroundColor Red
Write-Host ""
Write-Host "💡 故障排查:" -ForegroundColor Yellow
Write-Host " 1. 检查 PostgreSQL 容器是否运行:docker ps | grep postgres" -ForegroundColor Yellow
Write-Host " 2. 检查数据库连接:psql -h 192.168.58.20 -U gongxiangpan -d lobechat" -ForegroundColor Yellow
Write-Host " 3. 查看容器日志:docker logs $ContainerName" -ForegroundColor Yellow
Write-Host ""
exit 1
}