-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRun-PsqlFile.ps1
More file actions
45 lines (37 loc) · 1.37 KB
/
Copy pathRun-PsqlFile.ps1
File metadata and controls
45 lines (37 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
param (
[Parameter(Mandatory = $true)]
[string]$SqlFile,
[Parameter(Mandatory = $false)]
[string]$ConnectionString,
[Parameter(Mandatory = $false)]
[switch]$ContinueOnError
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
if ([string]::IsNullOrWhiteSpace($ConnectionString)) {
Write-Output "Using local container as target"
$config = Get-Content -Path "$PSScriptRoot/postgres.Local.json" -Raw | ConvertFrom-Json
$ConnectionString = "postgresql://postgres:$($config.PostgresPassword)@$($config.ContainerName):5432"
}
$networkName = "taylorbot-network"
$networkExists = docker network ls --filter name="^${networkName}$" --format "{{.Name}}"
if (-not $networkExists) {
docker network create $networkName
}
$sqlFileName = Split-Path -Path $SqlFile -Leaf
if ($ContinueOnError) {
docker container run `
--rm `
--network $networkName `
--mount "type=bind,source=$SqlFile,destination=/sqlscripts/$sqlFileName,readonly" `
postgres:15 `
psql --file=/sqlscripts/$sqlFileName "$ConnectionString/taylorbot"
}
else {
docker container run `
--rm `
--network $networkName `
--mount "type=bind,source=$SqlFile,destination=/sqlscripts/$sqlFileName,readonly" `
postgres:15 `
psql --variable=ON_ERROR_STOP=1 --file=/sqlscripts/$sqlFileName "$ConnectionString/taylorbot"
}