opencand.infra/deploy-db.ps1
2025-06-07 14:09:21 -03:00

92 lines
3.3 KiB
PowerShell

# OpenCand Database Deployment Script (PowerShell)
# This script deploys the database schema changes to the PostgreSQL container
param(
[string]$ContainerName = "opencand_db",
[string]$DatabaseName = "opencand",
[string]$DatabaseUser = "root",
[string]$SqlFile = ".\db\db.sql"
)
# Configuration
$ErrorActionPreference = "Stop"
Write-Host "🚀 OpenCand Database Deployment Script" -ForegroundColor Yellow
Write-Host "==================================" -ForegroundColor Yellow
try {
# Check if Docker is running
Write-Host "📋 Pre-deployment checks:" -ForegroundColor Yellow
docker info | Out-Null
Write-Host "✅ Docker is running" -ForegroundColor Green
# Check if the database container is running
$runningContainers = docker ps --format "{{.Names}}"
if ($runningContainers -notcontains $ContainerName) {
throw "Database container '$ContainerName' is not running. Please start with: docker-compose up -d"
}
Write-Host "✅ Database container is running" -ForegroundColor Green
# Check if SQL file exists
if (-not (Test-Path $SqlFile)) {
throw "SQL file '$SqlFile' not found"
}
Write-Host "✅ SQL file exists" -ForegroundColor Green
Write-Host ""
# Wait for database to be ready
Write-Host "⏳ Waiting for database to be ready..." -ForegroundColor Yellow
$timeout = 30
$counter = 0
do {
$ready = $false
try {
docker exec $ContainerName pg_isready -U $DatabaseUser -d $DatabaseName | Out-Null
$ready = $true
}
catch {
Start-Sleep -Seconds 1
$counter++
if ($counter -ge $timeout) {
throw "Database failed to become ready within $timeout seconds"
}
}
} while (-not $ready)
Write-Host "✅ Database is ready" -ForegroundColor Green
Write-Host ""
# Create backup before deployment
Write-Host "💾 Creating database backup..." -ForegroundColor Yellow
$backupFile = "backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').sql"
docker exec $ContainerName pg_dump -U $DatabaseUser -d $DatabaseName | Out-File -FilePath $backupFile -Encoding UTF8
Write-Host "✅ Backup created: $backupFile" -ForegroundColor Green
Write-Host ""
# Deploy the SQL file
Write-Host "🔧 Deploying database changes..." -ForegroundColor Yellow
Write-Host "Executing: $SqlFile"
# Execute the SQL file
Get-Content $SqlFile | docker exec -i $ContainerName psql -U $DatabaseUser -d $DatabaseName
Write-Host "✅ Database deployment completed successfully!" -ForegroundColor Green
Write-Host ""
# Show table information
Write-Host "📊 Current database tables:" -ForegroundColor Yellow
docker exec $ContainerName psql -U $DatabaseUser -d $DatabaseName -c "\dt"
Write-Host ""
Write-Host "🎉 Deployment completed successfully!" -ForegroundColor Green
Write-Host "==================================" -ForegroundColor Green
} catch {
Write-Host "❌ Error: $($_.Exception.Message)" -ForegroundColor Red
if (Test-Path $backupFile) {
Write-Host "💡 You can restore from backup using:" -ForegroundColor Yellow
Write-Host "Get-Content $backupFile | docker exec -i $ContainerName psql -U $DatabaseUser -d $DatabaseName" -ForegroundColor Yellow
}
exit 1
}