Beta_cartrigesg_bd Beta v0.9
This commit is contained in:
@@ -57,28 +57,74 @@ 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
|
||||
)
|
||||
|
||||
$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)
|
||||
)
|
||||
|
||||
try {
|
||||
$arguments = @(
|
||||
'--host', '127.0.0.1',
|
||||
'--port', [string]$PostgreSqlPort,
|
||||
'--username', $AdminUser,
|
||||
'--username', $Username,
|
||||
'--dbname', $Database,
|
||||
'--no-password',
|
||||
'--set', 'ON_ERROR_STOP=1',
|
||||
'--tuples-only',
|
||||
'--no-align',
|
||||
'--command', $Sql
|
||||
'--file', $sqlFilePath
|
||||
)
|
||||
|
||||
$previousErrorActionPreference = $ErrorActionPreference
|
||||
try {
|
||||
$ErrorActionPreference = 'Continue'
|
||||
$output = & $PsqlPath @arguments 2>&1
|
||||
$exitCode = $LASTEXITCODE
|
||||
$outputText = ($output -join [Environment]::NewLine).Trim()
|
||||
}
|
||||
finally {
|
||||
$ErrorActionPreference = $previousErrorActionPreference
|
||||
}
|
||||
|
||||
$outputText = Convert-PsqlOutput -Text (
|
||||
($output -join [Environment]::NewLine).Trim()
|
||||
)
|
||||
|
||||
if ($exitCode -ne 0) {
|
||||
if ($outputText -eq '') {
|
||||
@@ -94,6 +140,10 @@ function Invoke-Psql {
|
||||
if ($outputText -ne '') {
|
||||
Write-Host $outputText
|
||||
}
|
||||
}
|
||||
finally {
|
||||
Remove-Item -LiteralPath $sqlFilePath -Force -ErrorAction SilentlyContinue
|
||||
}
|
||||
}
|
||||
|
||||
if (-not (Test-Path -LiteralPath $PsqlPath -PathType Leaf)) {
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user