cleanup-test-users.ps1 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. # 清理测试用户脚本(PowerShell 版本)
  2. Write-Host "🔍 检查测试用户数量..." -ForegroundColor Cyan
  3. # 统计测试用户
  4. docker exec claude-code-hub-db-dev psql -U postgres -d claude_code_hub -c @"
  5. SELECT COUNT(*) as 测试用户数量
  6. FROM users
  7. WHERE (name LIKE '测试用户%' OR name LIKE '%test%' OR name LIKE 'Test%')
  8. AND deleted_at IS NULL;
  9. "@
  10. Write-Host ""
  11. Write-Host "📋 预览将要删除的用户(前 10 个)..." -ForegroundColor Cyan
  12. docker exec claude-code-hub-db-dev psql -U postgres -d claude_code_hub -c @"
  13. SELECT id, name, created_at
  14. FROM users
  15. WHERE (name LIKE '测试用户%' OR name LIKE '%test%' OR name LIKE 'Test%')
  16. AND deleted_at IS NULL
  17. ORDER BY created_at DESC
  18. LIMIT 10;
  19. "@
  20. Write-Host ""
  21. $confirm = Read-Host "⚠️ 确认删除这些测试用户吗?(y/N)"
  22. if ($confirm -eq 'y' -or $confirm -eq 'Y') {
  23. Write-Host "🗑️ 开始清理..." -ForegroundColor Yellow
  24. # 软删除关联的 keys
  25. docker exec claude-code-hub-db-dev psql -U postgres -d claude_code_hub -c @"
  26. UPDATE keys
  27. SET deleted_at = NOW(), updated_at = NOW()
  28. WHERE user_id IN (
  29. SELECT id FROM users
  30. WHERE (name LIKE '测试用户%' OR name LIKE '%test%' OR name LIKE 'Test%')
  31. AND deleted_at IS NULL
  32. )
  33. AND deleted_at IS NULL;
  34. "@
  35. # 软删除测试用户
  36. $result = docker exec claude-code-hub-db-dev psql -U postgres -d claude_code_hub -c @"
  37. UPDATE users
  38. SET deleted_at = NOW(), updated_at = NOW()
  39. WHERE (name LIKE '测试用户%' OR name LIKE '%test%' OR name LIKE 'Test%')
  40. AND deleted_at IS NULL
  41. RETURNING id, name;
  42. "@
  43. Write-Host "✅ 清理完成!" -ForegroundColor Green
  44. Write-Host ""
  45. Write-Host "📊 剩余用户统计:" -ForegroundColor Cyan
  46. docker exec claude-code-hub-db-dev psql -U postgres -d claude_code_hub -c @"
  47. SELECT COUNT(*) as 总用户数 FROM users WHERE deleted_at IS NULL;
  48. "@
  49. } else {
  50. Write-Host "❌ 取消清理" -ForegroundColor Red
  51. }