gongxiangp2/scripts/run-migration.ps1

90 lines
3.1 KiB
PowerShell
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.

# S3素材无感迁移执行脚本
# 用途:执行S3素材迁移,将老素材的S3文件迁移到新位置
param(
[switch]$DryRun, # 干运行模式(只检查,不实际迁移)
[switch]$Help # 显示帮助
)
if ($Help) {
Write-Host @"
S3素材无感迁移脚本
用法:
.\scripts\run-migration.ps1 # 执行迁移
.\scripts\run-migration.ps1 -DryRun # 干运行模式(只检查)
说明:
- 迁移所有老素材的S3文件到新位置(统一使用路径前缀)
- 更新数据库中的URL
- 支持断点续传和错误重试
- 迁移日志保存在 logs/migration/ 目录
注意事项:
- 迁移前建议备份数据库
- 迁移过程中系统可正常使用(只读模式)
- 迁移后验证所有功能正常
"@
exit 0
}
Write-Host "🚀 S3素材无感迁移脚本" -ForegroundColor Cyan
Write-Host ""
# 检查环境变量
if (-not $env:S3_BUCKET -or -not $env:S3_PUBLIC_DOMAIN) {
Write-Host "❌ 错误:缺少必要的环境变量" -ForegroundColor Red
Write-Host " 请确保 .env.local 文件包含以下变量:" -ForegroundColor Yellow
Write-Host " - S3_BUCKET" -ForegroundColor Yellow
Write-Host " - S3_PUBLIC_DOMAIN" -ForegroundColor Yellow
Write-Host " - S3_FILE_PATH" -ForegroundColor Yellow
exit 1
}
# 检查数据库连接
Write-Host "🔍 检查数据库连接..." -ForegroundColor Cyan
$dbCheck = node -e "const { PrismaClient } = require('@prisma/client'); const prisma = new PrismaClient(); prisma.\$connect().then(() => { console.log('OK'); process.exit(0); }).catch(e => { console.error('FAILED:', e.message); process.exit(1); });"
if ($LASTEXITCODE -ne 0) {
Write-Host "❌ 数据库连接失败" -ForegroundColor Red
exit 1
}
Write-Host "✅ 数据库连接正常" -ForegroundColor Green
Write-Host ""
if ($DryRun) {
Write-Host "🔍 干运行模式:只检查需要迁移的素材,不实际迁移" -ForegroundColor Yellow
Write-Host ""
node -e "require('tsx/cjs/register'); require('./scripts/migrate-s3-materials.ts')" --dry-run
} else {
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 ""
# 执行迁移
npx tsx scripts/migrate-s3-materials.ts
if ($LASTEXITCODE -eq 0) {
Write-Host ""
Write-Host "✅ 迁移完成!" -ForegroundColor Green
Write-Host " 请检查迁移日志: logs/migration/" -ForegroundColor Cyan
Write-Host " 建议验证几个素材是否正常显示" -ForegroundColor Yellow
} else {
Write-Host ""
Write-Host "❌ 迁移失败,请检查错误信息" -ForegroundColor Red
exit 1
}
}