Files
easyai/scripts/Assert-RedisPersistence.ps1
T

30 lines
1.5 KiB
PowerShell

# 在 Compose 重建 Redis 前,确认现有 /data 已位于当前项目的数据目录。
function Assert-RedisPersistence {
param([Parameter(Mandatory = $true)][string]$ProjectRoot)
$null = & docker info 2>&1
if ($LASTEXITCODE -ne 0) { throw "Cannot connect to Docker; Redis data location was not checked" }
$null = & docker container inspect redis 2>&1
if ($LASTEXITCODE -ne 0) { return }
$mount = (& docker inspect --format '{{range .Mounts}}{{if eq .Destination "/data"}}{{.Type}}|{{.Source}}{{end}}{{end}}' redis).Trim()
if ($LASTEXITCODE -ne 0 -or -not $mount) {
throw "Existing Redis /data is not persisted; migrate AOF/RDB before updating (see README)"
}
$parts = $mount.Split('|', 2)
if ($parts.Count -ne 2 -or $parts[0] -ne 'bind') {
throw "Existing Redis /data uses a different storage type; migrate it before updating (see README)"
}
$source = $parts[1]
# Docker Desktop 可将 Windows C:\path 显示为 /run/desktop/mnt/host/c/path。
$source = $source -replace '^/run/desktop/mnt/host/([A-Za-z])/', '$1:/'
$source = $source -replace '^/host_mnt/([A-Za-z])/', '$1:/'
$expected = [System.IO.Path]::GetFullPath((Join-Path $ProjectRoot 'data/redis'))
$actual = [System.IO.Path]::GetFullPath($source)
if (-not [string]::Equals($actual.TrimEnd('\', '/'), $expected.TrimEnd('\', '/'), [StringComparison]::OrdinalIgnoreCase)) {
throw "Existing Redis /data uses another directory; migrate it before updating (see README)"
}
}