Skip to content

Commit b9e2f27

Browse files
committed
feat(start-container): isolate compose env and warn on reused db data
1 parent 88b1063 commit b9e2f27

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

scripts/pwsh/devops/start-container.ps1

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,40 @@ function Resolve-ServiceComposeConfiguration {
708708
return Resolve-ConfigSources -Sources $sources -BasePath $ComposeDir -IncludeTrace
709709
}
710710

711+
function Get-DatabaseStateWarningMessage {
712+
[CmdletBinding()]
713+
param(
714+
[string]$ServiceName,
715+
[string]$DataPath
716+
)
717+
718+
$candidateFiles = switch ($ServiceName) {
719+
'postgre' {
720+
@(
721+
[System.IO.Path]::Combine($DataPath, 'postgresql', 'data', 'PG_VERSION'),
722+
[System.IO.Path]::Combine($DataPath, 'postgresql', 'PG_VERSION')
723+
)
724+
}
725+
'paradedb' {
726+
@(
727+
[System.IO.Path]::Combine($DataPath, 'paradedb', 'data', 'PG_VERSION'),
728+
[System.IO.Path]::Combine($DataPath, 'paradedb', 'PG_VERSION')
729+
)
730+
}
731+
default {
732+
@()
733+
}
734+
}
735+
736+
foreach ($candidate in $candidateFiles) {
737+
if (Test-Path -LiteralPath $candidate) {
738+
return "检测到已初始化的 $ServiceName 数据目录 ($candidate)。当前用户名、密码、库名配置仅影响新初始化实例;已有数据目录不会自动迁移内部角色、密码或默认库。"
739+
}
740+
}
741+
742+
return $null
743+
}
744+
711745
# 测试加载脚本时跳过主流程,避免 dot-source 触发真实 Docker 调用。
712746
if ($env:PWSH_TEST_SKIP_START_CONTAINER_MAIN -eq '1') {
713747
return
@@ -795,6 +829,8 @@ try {
795829
}
796830

797831
if ($Update) {
832+
$databaseWarning = Get-DatabaseStateWarningMessage -ServiceName $ServiceName -DataPath $composeEnvironment.DATA_PATH
833+
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
798834
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'pull' -Environment $composeEnvironment -DryRun:$DryRun
799835
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -Environment $composeEnvironment -DryRun:$DryRun
800836
if (-not $DryRun -and $ServiceName) {
@@ -810,6 +846,8 @@ try {
810846
if ($PullAlways) {
811847
$modeForUp = Test-DockerAvailable
812848
if ($modeForUp -eq 'sub') {
849+
$databaseWarning = Get-DatabaseStateWarningMessage -ServiceName $ServiceName -DataPath $composeEnvironment.DATA_PATH
850+
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
813851
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -ExtraArgs @('--pull', 'always') -Environment $composeEnvironment -DryRun:$DryRun
814852
if (-not $DryRun -and $ServiceName) {
815853
foreach ($tp in $targetProfiles) {
@@ -820,6 +858,8 @@ try {
820858
}
821859
}
822860
else {
861+
$databaseWarning = Get-DatabaseStateWarningMessage -ServiceName $ServiceName -DataPath $composeEnvironment.DATA_PATH
862+
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
823863
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'pull' -Environment $composeEnvironment -DryRun:$DryRun
824864
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -Environment $composeEnvironment -DryRun:$DryRun
825865
if (-not $DryRun -and $ServiceName) {
@@ -858,6 +898,8 @@ try {
858898
if ($ServiceName -eq 'beszel-suite' -or $ServiceName -eq 'rustdesk') { $isValidService = $true }
859899

860900
if ($isValidService) {
901+
$databaseWarning = Get-DatabaseStateWarningMessage -ServiceName $ServiceName -DataPath $composeEnvironment.DATA_PATH
902+
if (-not [string]::IsNullOrWhiteSpace($databaseWarning)) { Write-Warning $databaseWarning }
861903
Invoke-DockerCompose -File $composePath -Project $projectName -Profiles $targetProfiles -Action 'up -d' -Environment $composeEnvironment -DryRun:$DryRun
862904
if (-not $DryRun) {
863905
foreach ($tp in $targetProfiles) {

tests/StartContainer.ConfigIsolation.Tests.ps1

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,33 @@ Describe 'Resolve-ServiceComposeConfiguration' {
6666
$result.Sources.DEFAULT_USER | Should -Be 'ProcessEnv'
6767
}
6868
}
69+
70+
Describe 'Invoke-DockerCompose' {
71+
It 'does not leak scoped env values after a dry run' {
72+
Remove-Item Env:\DEFAULT_USER -ErrorAction SilentlyContinue
73+
74+
$null = Invoke-DockerCompose `
75+
-File (Join-Path $TestDrive 'demo-compose.yml') `
76+
-Project 'demo-project' `
77+
-Profiles @('paradedb') `
78+
-Action 'up -d' `
79+
-Environment @{ DEFAULT_USER = 'postgres' } `
80+
-DryRun
81+
82+
Test-Path Env:\DEFAULT_USER | Should -Be $false
83+
}
84+
}
85+
86+
Describe 'Get-DatabaseStateWarningMessage' {
87+
It 'returns a warning when a ParadeDB data directory already contains PG_VERSION' {
88+
$dataPath = Join-Path $TestDrive 'docker-data'
89+
$databasePath = [System.IO.Path]::Combine($dataPath, 'paradedb', 'data')
90+
New-Item -ItemType Directory -Path $databasePath -Force | Out-Null
91+
Set-Content -Path (Join-Path $databasePath 'PG_VERSION') -Value '17'
92+
93+
$message = Get-DatabaseStateWarningMessage -ServiceName 'paradedb' -DataPath $dataPath
94+
95+
$message | Should -Match '仅影响新初始化实例'
96+
$message | Should -Match 'paradedb'
97+
}
98+
}

0 commit comments

Comments
 (0)