# 影子数据库迁移脚本 # 用于在影子数据库(lobechat_stage)中创建 material_skus 表 $ServerIP = "192.168.58.20" $DbHost = "192.168.58.20" $DbPort = "5432" $DbName = "lobechat_stage" $DbUser = "gongxiangpan" $DbPassword = "gongxiangpan123" Write-Host "========================================" -ForegroundColor Cyan Write-Host "影子数据库迁移脚本" -ForegroundColor Cyan Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "数据库信息:" -ForegroundColor Yellow Write-Host " 服务器:$ServerIP" -ForegroundColor Gray Write-Host " 数据库:$DbName" -ForegroundColor Gray Write-Host "" # 读取迁移 SQL 文件 $migrationFile = "prisma/migrations/20251219_add_material_sku/migration.sql" if (-not (Test-Path $migrationFile)) { Write-Host "❌ 迁移文件不存在:$migrationFile" -ForegroundColor Red exit 1 } Write-Host "📄 读取迁移文件..." -ForegroundColor Cyan $migrationSql = Get-Content $migrationFile -Raw # 修改 SQL:将驼峰命名改为小写(匹配 Prisma @map) Write-Host "🔧 调整 SQL 语句(匹配 @map 映射)..." -ForegroundColor Cyan $migrationSql = $migrationSql -replace '"materialId"', '"materialid"' $migrationSql = $migrationSql -replace '"skuName"', '"skuname"' $migrationSql = $migrationSql -replace '"skuCode"', '"skucode"' $migrationSql = $migrationSql -replace '"previewUrl"', '"previewurl"' $migrationSql = $migrationSql -replace '"zipFileName"', '"zipfilename"' $migrationSql = $migrationSql -replace '"zipFilePath"', '"zipfilepath"' $migrationSql = $migrationSql -replace '"zipFileSize"', '"zipfilesize"' $migrationSql = $migrationSql -replace '"sortOrder"', '"sortorder"' $migrationSql = $migrationSql -replace '"uploaderId"', '"uploaderid"' $migrationSql = $migrationSql -replace '"createdAt"', '"createdat"' $migrationSql = $migrationSql -replace '"updatedAt"', '"updatedat"' # 保存修改后的 SQL 到临时文件 $tempSqlFile = "migration_shadow_temp.sql" $migrationSql | Out-File -FilePath $tempSqlFile -Encoding UTF8 -NoNewline Write-Host "📤 上传迁移文件到服务器..." -ForegroundColor Cyan scp $tempSqlFile "root@${ServerIP}:/tmp/" Write-Host "🔄 执行迁移..." -ForegroundColor Cyan # 使用 docker exec 执行 SQL(假设 PostgreSQL 容器名为 gongxiangpan-postgres-1) $dockerCmd = "docker exec -i gongxiangpan-postgres-1 psql -U $DbUser -d $DbName < /tmp/$tempSqlFile" $output = ssh "root@$ServerIP" $dockerCmd 2>&1 if ($LASTEXITCODE -eq 0) { Write-Host "✅ 迁移成功!" -ForegroundColor Green Write-Host "" Write-Host "输出:" -ForegroundColor Gray Write-Host $output -ForegroundColor Gray } else { Write-Host "❌ 迁移失败!" -ForegroundColor Red Write-Host "" Write-Host "错误信息:" -ForegroundColor Red Write-Host $output -ForegroundColor Red # 清理临时文件 Remove-Item $tempSqlFile -ErrorAction SilentlyContinue ssh "root@$ServerIP" "rm -f /tmp/$tempSqlFile" 2>&1 | Out-Null exit 1 } # 验证表是否创建成功 Write-Host "" Write-Host "🔍 验证表是否创建..." -ForegroundColor Cyan $verifyCmd = "docker exec gongxiangpan-postgres-1 psql -U $DbUser -d $DbName -c 'SELECT COUNT(*) FROM information_schema.tables WHERE table_name = ''material_skus'';'" $verifyOutput = ssh "root@$ServerIP" $verifyCmd 2>&1 if ($verifyOutput -match "material_skus") { Write-Host "✅ 表 material_skus 已成功创建!" -ForegroundColor Green } else { Write-Host "⚠️ 无法验证表是否创建" -ForegroundColor Yellow } # 清理临时文件 Write-Host "" Write-Host "🧹 清理临时文件..." -ForegroundColor Cyan Remove-Item $tempSqlFile -ErrorAction SilentlyContinue ssh "root@$ServerIP" "rm -f /tmp/$tempSqlFile" 2>&1 | Out-Null Write-Host "" Write-Host "========================================" -ForegroundColor Cyan Write-Host "✅ 迁移完成!" -ForegroundColor Green Write-Host "========================================" -ForegroundColor Cyan Write-Host "" Write-Host "现在可以在影子系统测试 SKU 功能了:" -ForegroundColor Yellow Write-Host " http://192.168.58.20:5320" -ForegroundColor Cyan