Files
prog-belyaev/КУРСАЧ/salon-is/scripts/salon.ps1
T

72 lines
2.5 KiB
PowerShell

param(
[ValidateSet('up', 'down', 'status', 'reset', 'shell', 'logs', 'backup', 'pgadmin')]
[string]$Action = 'up'
)
$ErrorActionPreference = 'Stop'
$Root = Split-Path -Parent $PSScriptRoot
Set-Location $Root
if (-not (Test-Path -LiteralPath '.env')) {
Copy-Item -LiteralPath '.env.example' -Destination '.env'
Write-Host 'Created .env from .env.example. EDIT THE PASSWORD before first start.'
}
function Read-Env {
$result = @{}
Get-Content -LiteralPath '.env' | ForEach-Object {
$line = $_.Trim()
if ($line -and -not $line.StartsWith('#') -and $line -match '^([A-Za-z0-9_]+)=(.*)$') {
$result[$Matches[1]] = $Matches[2].Trim('"')
}
}
return $result
}
$cfg = Read-Env
$db = if ($cfg.ContainsKey('POSTGRES_DB')) { $cfg['POSTGRES_DB'] } else { 'salon' }
$user = if ($cfg.ContainsKey('POSTGRES_USER')) { $cfg['POSTGRES_USER'] } else { 'postgres' }
$port = if ($cfg.ContainsKey('DB_PORT')) { $cfg['DB_PORT'] } else { '5433' }
$pgport = if ($cfg.ContainsKey('PGADMIN_PORT')) { $cfg['PGADMIN_PORT'] } else { '5050' }
switch ($Action) {
'up' {
docker compose up -d --remove-orphans
docker compose ps
Write-Host "Connect to localhost:$port / database $db / user $user"
}
'down' {
docker compose down
}
'status' {
docker compose ps
docker compose exec -T db psql -U $user -d $db -c "SELECT count(*) AS appointments FROM salon.appointments;"
}
'reset' {
Write-Warning 'This DELETES the Postgres volume and reloads sql/salon_db.sql.'
$answer = Read-Host 'Type YES to continue'
if ($answer -ne 'YES') { Write-Host 'Cancelled.'; return }
docker compose down -v
docker compose up -d --remove-orphans
}
'shell' {
docker compose exec db psql -U $user -d $db
}
'logs' {
docker compose logs -f --tail=100 db
}
'backup' {
New-Item -ItemType Directory -Force -Path 'backups' | Out-Null
$stamp = Get-Date -Format 'yyyyMMdd_HHmmss'
$file = "backups\salon_$stamp.dump"
docker compose exec -T db sh -c "pg_dump -U $user -Fc -d $db -f /tmp/salon_dump.dump"
docker compose cp db:/tmp/salon_dump.dump $file
Write-Host "Backup saved to $file"
}
'pgadmin' {
docker compose --profile pgadmin up -d
Write-Host "pgAdmin: http://localhost:$pgport"
Write-Host 'In pgAdmin add server: Host=db, Port=5432, Database=' + $db + ', User=' + $user
}
}