Beta_cartrigesg_bd Beta v0.9
This commit is contained in:
@@ -57,42 +57,92 @@ function Quote-Literal {
|
|||||||
return "'" + $Value.Replace("'", "''") + "'"
|
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 {
|
function Invoke-Psql {
|
||||||
param(
|
param(
|
||||||
[string]$Database,
|
[string]$Database,
|
||||||
[string]$Sql,
|
[string]$Sql,
|
||||||
|
[string]$Username = $AdminUser,
|
||||||
[switch]$Capture
|
[switch]$Capture
|
||||||
)
|
)
|
||||||
|
|
||||||
$arguments = @(
|
$sqlFilePath = Join-Path `
|
||||||
'--host', '127.0.0.1',
|
([System.IO.Path]::GetTempPath()) `
|
||||||
'--port', [string]$PostgreSqlPort,
|
("inventera-{0}.sql" -f ([Guid]::NewGuid().ToString('N')))
|
||||||
'--username', $AdminUser,
|
|
||||||
'--dbname', $Database,
|
[System.IO.File]::WriteAllText(
|
||||||
'--no-password',
|
$sqlFilePath,
|
||||||
'--set', 'ON_ERROR_STOP=1',
|
$Sql + [Environment]::NewLine,
|
||||||
'--tuples-only',
|
[System.Text.UTF8Encoding]::new($false)
|
||||||
'--no-align',
|
|
||||||
'--command', $Sql
|
|
||||||
)
|
)
|
||||||
|
|
||||||
$output = & $PsqlPath @arguments 2>&1
|
try {
|
||||||
$exitCode = $LASTEXITCODE
|
$arguments = @(
|
||||||
$outputText = ($output -join [Environment]::NewLine).Trim()
|
'--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) {
|
$previousErrorActionPreference = $ErrorActionPreference
|
||||||
if ($outputText -eq '') {
|
try {
|
||||||
$outputText = "psql завершился с кодом $exitCode."
|
$ErrorActionPreference = 'Continue'
|
||||||
|
$output = & $PsqlPath @arguments 2>&1
|
||||||
|
$exitCode = $LASTEXITCODE
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
$ErrorActionPreference = $previousErrorActionPreference
|
||||||
}
|
}
|
||||||
throw $outputText
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($Capture) {
|
$outputText = Convert-PsqlOutput -Text (
|
||||||
return $outputText
|
($output -join [Environment]::NewLine).Trim()
|
||||||
}
|
)
|
||||||
|
|
||||||
if ($outputText -ne '') {
|
if ($exitCode -ne 0) {
|
||||||
Write-Host $outputText
|
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;"
|
Invoke-Psql -Database 'postgres' -Sql "GRANT ALL PRIVILEGES ON DATABASE $databaseIdentifier TO $userIdentifier;"
|
||||||
|
|
||||||
$env:PGPASSWORD = $DatabasePassword
|
$env:PGPASSWORD = $DatabasePassword
|
||||||
$testArguments = @(
|
try {
|
||||||
'--host', '127.0.0.1',
|
Invoke-Psql `
|
||||||
'--port', [string]$PostgreSqlPort,
|
-Database $DatabaseName `
|
||||||
'--username', $DatabaseUser,
|
-Username $DatabaseUser `
|
||||||
'--dbname', $DatabaseName,
|
-Sql 'SELECT 1;' | Out-Null
|
||||||
'--no-password',
|
}
|
||||||
'--set', 'ON_ERROR_STOP=1',
|
catch {
|
||||||
'--command', 'SELECT 1;'
|
throw "Не удалось проверить подключение с учётными данными приложения. $_"
|
||||||
)
|
|
||||||
$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"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$configDirectory = Split-Path -Parent $ConfigPath
|
$configDirectory = Split-Path -Parent $ConfigPath
|
||||||
|
|||||||
Reference in New Issue
Block a user