mirror of
https://devops.lemonos.cn/lawson/FendxPHP.git
synced 2026-06-15 23:12:49 +08:00
- 创建用户表(users)包含基本信息和认证字段 - 创建角色表(roles)用于权限控制 - 创建权限表(permissions)定义系统权限 - 创建用户角色关联表(user_roles)建立用户与角色关系 - 创建角色权限关联表(role_permissions)建立角色与权限关系 - 创建迁移记录表(migrations)追踪数据库变更 - 添加AdminController提供管理员面板功能 - 实现系统监控、配置管理、缓存清理等功能 - 添加AOP切面编程支持的各种通知类型 - 实现告警管理AlertManager支持多渠道告警 - 添加文档注解接口规范
265 lines
8.1 KiB
PowerShell
265 lines
8.1 KiB
PowerShell
# 简化的数据库检查脚本
|
|
param(
|
|
[Parameter(Mandatory=$false)]
|
|
[ValidateSet("check", "fix", "help")]
|
|
[string]$Action = "check"
|
|
)
|
|
|
|
# 颜色输出
|
|
function Write-Success { param($m) Write-Host $m -ForegroundColor Green }
|
|
function Write-Error { param($m) Write-Host $m -ForegroundColor Red }
|
|
function Write-Warning { param($m) Write-Host $m -ForegroundColor Yellow }
|
|
function Write-Info { param($m) Write-Host $m -ForegroundColor Cyan }
|
|
|
|
# 加载环境配置
|
|
function Import-Environment {
|
|
$envFile = Join-Path $PSScriptRoot "..\.env"
|
|
if (Test-Path $envFile) {
|
|
Get-Content $envFile | ForEach-Object {
|
|
if ($_ -match '^([^=]+)=(.*)$' -and -not $_.StartsWith('#')) {
|
|
[Environment]::SetEnvironmentVariable($matches[1].Trim(), $matches[2].Trim(), "Process")
|
|
}
|
|
}
|
|
Write-Info "环境配置加载完成"
|
|
} else {
|
|
Write-Warning "环境配置文件不存在: $envFile"
|
|
}
|
|
}
|
|
|
|
# 检查数据库连接
|
|
function Test-DatabaseConnection {
|
|
$dbHost = if ($env:DB_HOST) { $env:DB_HOST } else { "localhost" }
|
|
$dbPort = if ($env:DB_PORT) { $env:DB_PORT } else { "3306" }
|
|
$dbDatabase = if ($env:DB_DATABASE) { $env:DB_DATABASE } else { "fendx_php" }
|
|
$dbUsername = if ($env:DB_USERNAME) { $env:DB_USERNAME } else { "root" }
|
|
$dbPassword = if ($env:DB_PASSWORD) { $env:DB_PASSWORD } else { "" }
|
|
|
|
$config = @{
|
|
Host = $dbHost
|
|
Port = $dbPort
|
|
Database = $dbDatabase
|
|
Username = $dbUsername
|
|
Password = $dbPassword
|
|
}
|
|
|
|
Write-Info "检查数据库连接..."
|
|
Write-Info " 主机: $($config.Host):$($config.Port)"
|
|
Write-Info " 数据库: $($config.Database)"
|
|
Write-Info " 用户名: $($config.Username)"
|
|
|
|
# 尝试使用MySQL客户端
|
|
try {
|
|
& mysql -h $config.Host -P $config.Port -u $config.Username -p$config.Password -e "SELECT 1" 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Success "数据库连接成功"
|
|
return @{ Success = $true; Config = $config; Docker = $false }
|
|
}
|
|
} catch {
|
|
# 忽略错误
|
|
}
|
|
|
|
# 尝试使用Docker
|
|
if (Get-Command docker -ErrorAction SilentlyContinue) {
|
|
Write-Info "尝试使用Docker连接数据库..."
|
|
|
|
try {
|
|
& docker info >$null 2>&1
|
|
if ($LASTEXITCODE -eq 0) {
|
|
# 检查MySQL容器
|
|
$container = & docker ps --filter "name=fendx-mysql-test" --format "table {{.Names}}`t{{.Status}}" | Select-Object -Skip 1
|
|
|
|
if ($container -and $container -like "*Up*") {
|
|
# 测试Docker连接
|
|
& docker exec fendx-mysql-test mysql -u test -ptest -e "SELECT 1" >$null 2>&1
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Success "Docker数据库连接成功"
|
|
return @{ Success = $true; Config = $config; Docker = $true }
|
|
}
|
|
} else {
|
|
Write-Warning "MySQL容器未运行"
|
|
}
|
|
}
|
|
} catch {
|
|
Write-Warning "Docker连接失败"
|
|
}
|
|
}
|
|
|
|
Write-Error "数据库连接失败"
|
|
return @{ Success = $false }
|
|
}
|
|
|
|
# 检查数据库是否存在
|
|
function Test-DatabaseExists {
|
|
param($Config, [switch]$UseDocker)
|
|
|
|
$database = $Config.Database
|
|
|
|
if ($UseDocker) {
|
|
Write-Info "检查数据库是否存在 (Docker)..."
|
|
$result = & docker exec fendx-mysql-test mysql -u test -ptest -e "SHOW DATABASES LIKE '$database'" 2>$null
|
|
if ($result -match $database) {
|
|
Write-Success "数据库 '$database' 存在"
|
|
return $true
|
|
}
|
|
} else {
|
|
Write-Info "检查数据库是否存在..."
|
|
$result = & mysql -h $Config.Host -P $Config.Port -u $Config.Username -p$Config.Password -e "SHOW DATABASES LIKE '$database'" 2>$null
|
|
if ($result -match $database) {
|
|
Write-Success "数据库 '$database' 存在"
|
|
return $true
|
|
}
|
|
}
|
|
|
|
Write-Warning "数据库 '$database' 不存在"
|
|
return $false
|
|
}
|
|
|
|
# 创建数据库
|
|
function New-Database {
|
|
param($Config, [switch]$UseDocker)
|
|
|
|
$database = $Config.Database
|
|
Write-Info "创建数据库 '$database'..."
|
|
|
|
if ($UseDocker) {
|
|
$sql = "CREATE DATABASE IF NOT EXISTS `$database` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
|
|
& docker exec fendx-mysql-test mysql -u test -ptest -e $sql >$null 2>&1
|
|
} else {
|
|
$sql = "CREATE DATABASE IF NOT EXISTS `$database` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"
|
|
& mysql -h $Config.Host -P $Config.Port -u $Config.Username -p$Config.Password -e $sql >$null 2>&1
|
|
}
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Success "数据库创建成功"
|
|
return $true
|
|
} else {
|
|
Write-Error "数据库创建失败"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# 运行迁移
|
|
function Invoke-Migration {
|
|
param([switch]$UseDocker)
|
|
|
|
Write-Info "运行数据库迁移..."
|
|
|
|
if ($UseDocker) {
|
|
& docker-compose -f docker-compose.test.yml exec -T app php bin/console migrate:run
|
|
} else {
|
|
& php bin/console migrate:run
|
|
}
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Success "数据库迁移完成"
|
|
return $true
|
|
} else {
|
|
Write-Error "数据库迁移失败"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# 完整检查
|
|
function Invoke-FullCheck {
|
|
Write-Host "🚀 FendxPHP 数据库检查 (PowerShell)" -ForegroundColor Green
|
|
Write-Host "==================================================" -ForegroundColor Cyan
|
|
|
|
Import-Environment
|
|
|
|
$connectionResult = Test-DatabaseConnection
|
|
|
|
if (-not $connectionResult.Success) {
|
|
Write-Host "`n💡 解决建议:" -ForegroundColor Yellow
|
|
Write-Host " 1. 检查数据库服务是否启动" -ForegroundColor White
|
|
Write-Host " 2. 验证数据库连接配置" -ForegroundColor White
|
|
Write-Host " 3. 确认用户权限正确" -ForegroundColor White
|
|
return
|
|
}
|
|
|
|
$config = $connectionResult.Config
|
|
$useDocker = $connectionResult.Docker
|
|
|
|
Write-Host "`n--------------------------------------------------" -ForegroundColor Cyan
|
|
|
|
if (-not (Test-DatabaseExists $config -UseDocker:$useDocker)) {
|
|
Write-Host "`n🔧 数据库不存在,建议运行:" -ForegroundColor Yellow
|
|
Write-Host " .\scripts\test-database.ps1 fix" -ForegroundColor White
|
|
return
|
|
}
|
|
|
|
Write-Host "`n==================================================" -ForegroundColor Cyan
|
|
Write-Success "数据库检查完成"
|
|
}
|
|
|
|
# 修复数据库
|
|
function Repair-Database {
|
|
Write-Info "开始修复数据库..."
|
|
|
|
Import-Environment
|
|
$connectionResult = Test-DatabaseConnection
|
|
|
|
if (-not $connectionResult.Success) {
|
|
Write-Error "数据库连接失败,无法修复"
|
|
return
|
|
}
|
|
|
|
$config = $connectionResult.Config
|
|
$useDocker = $connectionResult.Docker
|
|
|
|
if (-not (Test-DatabaseExists $config -UseDocker:$useDocker)) {
|
|
New-Database $config -UseDocker:$useDocker
|
|
}
|
|
|
|
Invoke-Migration -UseDocker:$useDocker
|
|
|
|
Write-Success "数据库修复完成"
|
|
}
|
|
|
|
# 显示帮助
|
|
function Show-Help {
|
|
Write-Host @"
|
|
FendxPHP 数据库检查工具 (简化版)
|
|
|
|
用法: .\scripts\test-database.ps1 [选项]
|
|
|
|
选项:
|
|
check 检查数据库状态 (默认)
|
|
fix 修复数据库问题
|
|
help 显示帮助信息
|
|
|
|
示例:
|
|
.\scripts\test-database.ps1 # 检查数据库
|
|
.\scripts\test-database.ps1 check # 检查数据库
|
|
.\scripts\test-database.ps1 fix # 修复数据库
|
|
|
|
"@ -ForegroundColor White
|
|
}
|
|
|
|
# 主函数
|
|
function Main {
|
|
switch ($Action.ToLower()) {
|
|
"check" {
|
|
Invoke-FullCheck
|
|
}
|
|
"fix" {
|
|
Repair-Database
|
|
}
|
|
"help" {
|
|
Show-Help
|
|
}
|
|
default {
|
|
Write-Error "未知选项: $Action"
|
|
Show-Help
|
|
exit 1
|
|
}
|
|
}
|
|
}
|
|
|
|
# 运行主函数
|
|
try {
|
|
Main
|
|
} catch {
|
|
Write-Error "脚本执行失败: $($_.Exception.Message)"
|
|
exit 1
|
|
}
|