Netpulse_SasS/db/migrate.ps1
zotac 15d22a8d8a Етап 1-2: схема БД та protobuf-контракт агент-сервер
Схема PostgreSQL 16+/TimescaleDB: 11 міграцій, 7 схем, топологія
(neighbors -> links -> maps -> nodes/edges), time-series з CAGG,
NCM, alerting, білінг з entitlements, RLS.

Контракт agent<->server: 6 proto-файлів, gRPC, інтернування серій,
at-least-once з ack, чанкування конфігів.

Перевірено на стенді Debian 13 / PG 17.11 / TimescaleDB 2.29.1:
міграції + 8 функціональних перевірок схеми, buf lint + 5 наскрізних
gRPC-тестів контракту.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-14 03:28:40 +03:00

63 lines
2.6 KiB
PowerShell
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<#
Накочує міграції по порядку номерів у транзакції (кожен файл окремо).
Використання:
.\migrate.ps1 # локальний стенд з docker-compose
.\migrate.ps1 -DbUrl "postgres://user:pw@host:5432/db"
.\migrate.ps1 -DryRun # лише показати порядок
#>
param(
[string]$DbUrl = "postgres://netpulse:netpulse@localhost:5432/netpulse",
[switch]$DryRun
)
$ErrorActionPreference = "Stop"
$dir = Join-Path $PSScriptRoot "migrations"
$files = Get-ChildItem -Path $dir -Filter "*.sql" | Sort-Object Name
if ($DryRun) {
$files | ForEach-Object { $_.Name }
return
}
if (-not (Get-Command psql -ErrorAction SilentlyContinue)) {
throw "psql не знайдено в PATH. Встанови PostgreSQL client tools або запусти через контейнер: docker compose exec -T db psql ..."
}
# Таблиця обліку застосованих міграцій
$bootstrap = @'
CREATE TABLE IF NOT EXISTS public.schema_migrations (
version text PRIMARY KEY,
checksum text NOT NULL,
applied_at timestamptz NOT NULL DEFAULT now()
);
'@
$bootstrap | psql $DbUrl -v ON_ERROR_STOP=1 -q
foreach ($f in $files) {
$version = $f.BaseName
$applied = (psql $DbUrl -tA -c "SELECT 1 FROM public.schema_migrations WHERE version = '$version'").Trim()
if ($applied -eq "1") {
Write-Host "skip $version" -ForegroundColor DarkGray
continue
}
$sum = (Get-FileHash $f.FullName -Algorithm SHA256).Hash
Write-Host "apply $version" -ForegroundColor Cyan
# Зазвичай — одна транзакція на файл, щоб не було часткових міграцій.
# Виняток: TimescaleDB забороняє CREATE MATERIALIZED VIEW WITH
# (timescaledb.continuous) всередині транзакційного блоку.
$needsAutocommit = (Select-String -Path $f.FullName -Pattern "timescaledb\.continuous" -Quiet)
if ($needsAutocommit) {
Write-Host " (autocommit: continuous aggregates)" -ForegroundColor DarkYellow
psql $DbUrl -v ON_ERROR_STOP=1 -q -f $f.FullName
} else {
psql $DbUrl -v ON_ERROR_STOP=1 --single-transaction -q -f $f.FullName
}
if ($LASTEXITCODE -ne 0) { throw "Міграція $version впала" }
psql $DbUrl -v ON_ERROR_STOP=1 -q -c `
"INSERT INTO public.schema_migrations (version, checksum) VALUES ('$version','$sum')"
}
Write-Host "OK: усі міграції застосовано" -ForegroundColor Green