Files
financial_system_database/tools/recover-v19-role-permissions.ps1
2026-12-02 15:06:48 +08:00

45 lines
1.9 KiB
PowerShell

param(
[Parameter(Mandatory = $true)][string]$JdbcUrl,
[Parameter(Mandatory = $true)][string]$User,
[Parameter(Mandatory = $true)][string]$SnapshotDirectory,
[string]$PlanPath = (Join-Path $SnapshotDirectory "V19_RECOVERY_PLAN.tsv"),
[string]$ExpectedPlanHash,
[string]$PasswordEnv = "DB_PASSWORD",
[ValidateRange(0, 3600)][int]$LockTimeoutSeconds = 60,
[switch]$Apply
)
$ErrorActionPreference = "Stop"
$backend = (Resolve-Path (Join-Path $PSScriptRoot "..\..\financial_system_backend")).Path
$snapshot = (Resolve-Path $SnapshotDirectory).Path
$v19Script = (Resolve-Path (Join-Path $PSScriptRoot "..\migrations\mysql\V20261009_19__menu_tree_derived_authorization.sql")).Path
$plan = [System.IO.Path]::GetFullPath($PlanPath)
$classpathFile = Join-Path $backend "target\migration-classpath.txt"
$mode = if ($Apply) { "apply" } else { "validate" }
if ($Apply -and [string]::IsNullOrWhiteSpace($ExpectedPlanHash)) {
throw "V19_RECOVERY_EXPECTED_PLAN_REQUIRED"
}
if (-not $Apply -and -not [string]::IsNullOrWhiteSpace($ExpectedPlanHash)) {
throw "V19_RECOVERY_EXPECTED_PLAN_NOT_ALLOWED"
}
Push-Location $backend
try {
& mvn.cmd -q -DskipTests compile dependency:build-classpath "-Dmdep.outputFile=$classpathFile"
if ($LASTEXITCODE -ne 0) { throw "V19_RECOVERY_BUILD_FAILED" }
$dependencyClasspath = Get-Content -Raw $classpathFile
$classpath = (Join-Path $backend "target\classes") + ";" + $dependencyClasspath.Trim()
$arguments = @(
"-cp", $classpath,
"com.finance.system.migration.V19RolePermissionRecovery",
"--url=$JdbcUrl", "--user=$User", "--password-env=$PasswordEnv",
"--snapshot=$snapshot", "--v19-script=$v19Script", "--plan=$plan",
"--mode=$mode", "--lock-timeout-seconds=$LockTimeoutSeconds"
)
if ($Apply) { $arguments += "--expected-plan-hash=$ExpectedPlanHash" }
& java @arguments
if ($LASTEXITCODE -ne 0) { throw "V19_RECOVERY_FAILED" }
} finally {
Pop-Location
}