From 12fc229907e669191d12f8164e4143efd918fb9a Mon Sep 17 00:00:00 2001 From: Alexey Date: Sun, 26 Jul 2026 16:27:24 +0300 Subject: [PATCH] Beta_cartrigesg_bd Beta v0.9 --- installer/initialize_inventera_database.ps1 | 121 +++++++++++++------- 1 file changed, 81 insertions(+), 40 deletions(-) diff --git a/installer/initialize_inventera_database.ps1 b/installer/initialize_inventera_database.ps1 index b1d8653..13300e4 100644 --- a/installer/initialize_inventera_database.ps1 +++ b/installer/initialize_inventera_database.ps1 @@ -57,42 +57,92 @@ function Quote-Literal { return "'" + $Value.Replace("'", "''") + "'" } +function Convert-PsqlOutput { + param([string]$Text) + + if ([string]::IsNullOrEmpty($Text)) { + return $Text + } + + # Russian PostgreSQL messages can be emitted in the Windows ANSI code page + # while Windows PowerShell 5.1 decodes them as the OEM code page. + if ($Text -match '[\u2500-\u259F]') { + try { + $culture = [System.Globalization.CultureInfo]::CurrentCulture + $oemEncoding = [System.Text.Encoding]::GetEncoding($culture.TextInfo.OEMCodePage) + $ansiEncoding = [System.Text.Encoding]::GetEncoding($culture.TextInfo.ANSICodePage) + return $ansiEncoding.GetString($oemEncoding.GetBytes($Text)) + } + catch { + return $Text + } + } + + return $Text +} + function Invoke-Psql { param( [string]$Database, [string]$Sql, + [string]$Username = $AdminUser, [switch]$Capture ) - $arguments = @( - '--host', '127.0.0.1', - '--port', [string]$PostgreSqlPort, - '--username', $AdminUser, - '--dbname', $Database, - '--no-password', - '--set', 'ON_ERROR_STOP=1', - '--tuples-only', - '--no-align', - '--command', $Sql + $sqlFilePath = Join-Path ` + ([System.IO.Path]::GetTempPath()) ` + ("inventera-{0}.sql" -f ([Guid]::NewGuid().ToString('N'))) + + [System.IO.File]::WriteAllText( + $sqlFilePath, + $Sql + [Environment]::NewLine, + [System.Text.UTF8Encoding]::new($false) ) - $output = & $PsqlPath @arguments 2>&1 - $exitCode = $LASTEXITCODE - $outputText = ($output -join [Environment]::NewLine).Trim() + try { + $arguments = @( + '--host', '127.0.0.1', + '--port', [string]$PostgreSqlPort, + '--username', $Username, + '--dbname', $Database, + '--no-password', + '--set', 'ON_ERROR_STOP=1', + '--tuples-only', + '--no-align', + '--file', $sqlFilePath + ) - if ($exitCode -ne 0) { - if ($outputText -eq '') { - $outputText = "psql завершился с кодом $exitCode." + $previousErrorActionPreference = $ErrorActionPreference + try { + $ErrorActionPreference = 'Continue' + $output = & $PsqlPath @arguments 2>&1 + $exitCode = $LASTEXITCODE + } + finally { + $ErrorActionPreference = $previousErrorActionPreference } - throw $outputText - } - if ($Capture) { - return $outputText - } + $outputText = Convert-PsqlOutput -Text ( + ($output -join [Environment]::NewLine).Trim() + ) - if ($outputText -ne '') { - Write-Host $outputText + if ($exitCode -ne 0) { + if ($outputText -eq '') { + $outputText = "psql завершился с кодом $exitCode." + } + throw $outputText + } + + if ($Capture) { + return $outputText + } + + if ($outputText -ne '') { + Write-Host $outputText + } + } + finally { + Remove-Item -LiteralPath $sqlFilePath -Force -ErrorAction SilentlyContinue } } @@ -149,23 +199,14 @@ else { Invoke-Psql -Database 'postgres' -Sql "GRANT ALL PRIVILEGES ON DATABASE $databaseIdentifier TO $userIdentifier;" $env:PGPASSWORD = $DatabasePassword -$testArguments = @( - '--host', '127.0.0.1', - '--port', [string]$PostgreSqlPort, - '--username', $DatabaseUser, - '--dbname', $DatabaseName, - '--no-password', - '--set', 'ON_ERROR_STOP=1', - '--command', 'SELECT 1;' -) -$testOutput = & $PsqlPath @testArguments 2>&1 -$testExitCode = $LASTEXITCODE -if ($testExitCode -ne 0) { - $testOutputText = ($testOutput -join [Environment]::NewLine).Trim() - if ($testOutputText -eq '') { - $testOutputText = "psql завершился с кодом $testExitCode." - } - throw "Не удалось проверить подключение с учётными данными приложения. $testOutputText" +try { + Invoke-Psql ` + -Database $DatabaseName ` + -Username $DatabaseUser ` + -Sql 'SELECT 1;' | Out-Null +} +catch { + throw "Не удалось проверить подключение с учётными данными приложения. $_" } $configDirectory = Split-Path -Parent $ConfigPath