$ErrorActionPreference = "Stop"
$ProjectRoot = Split-Path -Parent $PSScriptRoot
Push-Location (Join-Path $ProjectRoot "backend")
try {
    uv run alembic -c alembic.ini --name alembic_core upgrade head
    if ($LASTEXITCODE -ne 0) { throw "Core 数据库迁移失败。" }
    uv run alembic -c alembic.ini --name alembic_agent upgrade head
    if ($LASTEXITCODE -ne 0) { throw "Agent 数据库迁移失败。" }
    uv run alembic -c alembic.ini --name alembic_analytics upgrade head
    if ($LASTEXITCODE -ne 0) { throw "Analytics 数据库迁移失败。" }
}
finally {
    Pop-Location
}
