-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRUN_SANITY.ps1
More file actions
351 lines (298 loc) · 13.1 KB
/
RUN_SANITY.ps1
File metadata and controls
351 lines (298 loc) · 13.1 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<#
.SYNOPSIS
Runs the sanity check pack against any environment (local, staging, production).
.DESCRIPTION
Executes 17 cross-environment validation checks.
Checks 1–16 are SQL queries from supabase/sanity/sanity_checks.sql.
Check 17 is a filesystem check verifying BACKUP.ps1 exists and is syntactically valid.
All SQL checks are READ-ONLY SELECT queries — safe to run against production at any time
(§8.1A single-cloud guardrail compliance).
1. Required tables exist
2. Required views exist
3. Required functions exist
4. Reference data populated
5. Product row count threshold (>= 1000)
6. EAN uniqueness — no duplicate active EANs
7. Country scoping integrity
8. Category integrity
9. Deprecated product logic
10. Scoring coverage
11. Nutri-Score coverage
12. Nutrition facts completeness
13. Health profile invariant (one active per user)
14. RLS enabled on core tables
15. Materialized views populated
16. Foreign key constraints validated
17. BACKUP.ps1 exists and is syntactically valid
Each check returns 0 rows on success. Any rows indicate failures.
.PARAMETER Env
Target environment: local, staging, or production.
.PARAMETER Json
Output results as machine-readable JSON instead of colored text.
.PARAMETER OutFile
Write JSON output to this file path (implies -Json).
.NOTES
Prerequisites:
- Local: Docker Desktop + Supabase running
- Staging: psql on PATH + SUPABASE_STAGING_DB_PASSWORD
- Production: psql on PATH + SUPABASE_DB_PASSWORD
Usage:
.\RUN_SANITY.ps1 -Env local
.\RUN_SANITY.ps1 -Env staging
.\RUN_SANITY.ps1 -Env production
.\RUN_SANITY.ps1 -Env local -Json -OutFile sanity.json
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, HelpMessage = "Target environment: local, staging, or production.")]
[ValidateSet("local", "staging", "production")]
[string]$Env,
[Parameter(HelpMessage = "Output results as JSON.")]
[switch]$Json,
[Parameter(HelpMessage = "Write JSON output to this file path (implies -Json).")]
[string]$OutFile
)
if ($OutFile) { $Json = $true }
# ─── Configuration ───────────────────────────────────────────────────────────
$PRODUCTION_PROJECT_REF = "uskvezwftkkudvksmken"
$POOLER_HOST = "aws-1-eu-west-1.pooler.supabase.com"
$DOCKER_CONTAINER = "supabase_db_tryvit"
$DB_NAME = "postgres"
$DB_USER = "postgres"
$DB_PORT = "5432"
$SANITY_FILE = Join-Path $PSScriptRoot "supabase" "sanity" "sanity_checks.sql"
if (-not (Test-Path $SANITY_FILE)) {
Write-Host "ERROR: Sanity check file not found: $SANITY_FILE" -ForegroundColor Red
exit 1
}
# ─── Load .env if present ────────────────────────────────────────────────────
$dotenvPath = Join-Path $PSScriptRoot ".env"
if (Test-Path $dotenvPath) {
Get-Content $dotenvPath | ForEach-Object {
if ($_ -match '^\s*([A-Z_][A-Z0-9_]*)\s*=\s*(.+)$') {
$key = $Matches[1]
$val = $Matches[2].Trim().Trim('"', "'")
if (-not [System.Environment]::GetEnvironmentVariable($key)) {
[System.Environment]::SetEnvironmentVariable($key, $val)
}
}
}
}
# ─── Environment-specific configuration ──────────────────────────────────────
$usePsql = $false
# CI mode: if PGHOST is set, use psql directly regardless of -Env value
$ciMode = [bool]$env:PGHOST
switch ($Env) {
"local" {
if ($ciMode) {
$envLabel = "CI (psql via PGHOST=$($env:PGHOST))"
$envColor = "Cyan"
$usePsql = $true
}
else {
$envLabel = "LOCAL (Docker)"
$envColor = "Green"
}
}
"staging" {
$stagingRef = [System.Environment]::GetEnvironmentVariable("SUPABASE_STAGING_PROJECT_REF")
if (-not $stagingRef) {
Write-Host "ERROR: SUPABASE_STAGING_PROJECT_REF not set." -ForegroundColor Red
exit 1
}
$dbHost = $POOLER_HOST
$DB_USER = "postgres.$stagingRef"
$dbPassword = [System.Environment]::GetEnvironmentVariable("SUPABASE_STAGING_DB_PASSWORD")
$envLabel = "STAGING ($stagingRef)"
$envColor = "Yellow"
$usePsql = $true
}
"production" {
$dbHost = $POOLER_HOST
$DB_USER = "postgres.$PRODUCTION_PROJECT_REF"
$dbPassword = [System.Environment]::GetEnvironmentVariable("SUPABASE_DB_PASSWORD")
$envLabel = "PRODUCTION ($PRODUCTION_PROJECT_REF)"
$envColor = "Red"
$usePsql = $true
}
}
# ─── Banner ──────────────────────────────────────────────────────────────────
if (-not $Json) {
Write-Host ""
Write-Host "================================================" -ForegroundColor $envColor
Write-Host " TryVit — Sanity Check Pack" -ForegroundColor Cyan
Write-Host " Target: $envLabel" -ForegroundColor $envColor
Write-Host "================================================" -ForegroundColor $envColor
Write-Host ""
}
# ─── Connect ─────────────────────────────────────────────────────────────────
if ($usePsql) {
$psqlCmd = Get-Command psql -ErrorAction SilentlyContinue
if (-not $psqlCmd) {
Write-Host "ERROR: psql not found on PATH." -ForegroundColor Red
exit 1
}
# In CI mode, PGPASSWORD env var handles authentication — no prompt needed
if (-not $ciMode -and -not $dbPassword) {
Write-Host "Enter the database password for $envLabel :" -ForegroundColor Yellow
$securePassword = Read-Host -AsSecureString
$dbPassword = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($securePassword)
)
}
}
else {
$dockerCmd = Get-Command docker -ErrorAction SilentlyContinue
if (-not $dockerCmd) {
Write-Host "ERROR: docker not found on PATH." -ForegroundColor Red
exit 1
}
}
# ─── Parse sanity check file into individual checks ─────────────────────────
$fileContent = Get-Content $SANITY_FILE -Raw
# Split on check headers (-- CHECK N: ...)
$checkBlocks = [regex]::Split($fileContent, '(?=-- ═{5,}\s*\r?\n-- CHECK \d+:)')
$checkBlocks = $checkBlocks | Where-Object { $_ -match '-- CHECK \d+:' }
$checks = @()
foreach ($block in $checkBlocks) {
if ($block -match '-- CHECK (\d+):\s*(.+)') {
$checks += @{
Number = [int]$Matches[1]
Name = $Matches[2].Trim()
SQL = $block.Trim()
}
}
}
if ($checks.Count -eq 0) {
Write-Host "ERROR: No checks parsed from $SANITY_FILE" -ForegroundColor Red
exit 1
}
# ─── Execute checks ─────────────────────────────────────────────────────────
$results = @()
$passCount = 0
$failCount = 0
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
foreach ($check in $checks) {
$checkTimer = [System.Diagnostics.Stopwatch]::StartNew()
if ($usePsql) {
if ($ciMode) {
# CI mode: rely on PGHOST/PGPORT/PGUSER/PGPASSWORD/PGDATABASE env vars
$output = $check.SQL | & psql --tuples-only --no-align -v ON_ERROR_STOP=1 2>&1
}
else {
$env:PGPASSWORD = $dbPassword
$output = $check.SQL | & psql -h $dbHost -p $DB_PORT -U $DB_USER -d $DB_NAME --tuples-only --no-align -v ON_ERROR_STOP=1 2>&1
}
}
else {
$output = $check.SQL | docker exec -i $DOCKER_CONTAINER psql -U $DB_USER -d $DB_NAME --tuples-only --no-align -v ON_ERROR_STOP=1 2>&1
}
$checkTimer.Stop()
$exitCode = $LASTEXITCODE
# Parse output: non-empty output (excluding blank lines) = failure
$outputLines = ($output -join "`n").Trim()
$hasFailures = ($exitCode -ne 0) -or ($outputLines.Length -gt 0 -and $outputLines -ne "")
$status = if ($hasFailures) { "FAIL" } else { "PASS" }
if ($hasFailures) { $failCount++ } else { $passCount++ }
if (-not $Json) {
$icon = if ($hasFailures) { "✗" } else { "✓" }
$color = if ($hasFailures) { "Red" } else { "Green" }
$ms = $checkTimer.ElapsedMilliseconds
Write-Host " $icon Check $($check.Number): $($check.Name) ($($ms)ms)" -ForegroundColor $color
if ($hasFailures -and $outputLines.Length -gt 0) {
$outputLines -split "`n" | Select-Object -First 5 | ForEach-Object {
Write-Host " $_" -ForegroundColor DarkRed
}
}
}
$results += @{
check = $check.Number
name = $check.Name
status = $status
runtime_ms = $checkTimer.ElapsedMilliseconds
violations = if ($hasFailures -and $outputLines.Length -gt 0) {
($outputLines -split "`n" | Select-Object -First 10)
}
else { @() }
}
}
# ─── Check 17: BACKUP.ps1 exists and is syntactically valid (filesystem) ────
$check17Timer = [System.Diagnostics.Stopwatch]::StartNew()
$backupScript = Join-Path $PSScriptRoot "BACKUP.ps1"
$check17Violations = @()
if (-not (Test-Path $backupScript)) {
$check17Violations += "BACKUP.ps1 not found at $backupScript"
}
else {
# Validate PowerShell syntax by parsing the script
$parseErrors = $null
[System.Management.Automation.Language.Parser]::ParseFile($backupScript, [ref]$null, [ref]$parseErrors) | Out-Null
if ($parseErrors.Count -gt 0) {
foreach ($err in $parseErrors) {
$check17Violations += "Syntax error: $($err.Message) (line $($err.Extent.StartLineNumber))"
}
}
}
$check17Timer.Stop()
$check17Status = if ($check17Violations.Count -gt 0) { "FAIL" } else { "PASS" }
if ($check17Violations.Count -gt 0) { $failCount++ } else { $passCount++ }
if (-not $Json) {
$icon = if ($check17Violations.Count -gt 0) { "✗" } else { "✓" }
$color = if ($check17Violations.Count -gt 0) { "Red" } else { "Green" }
Write-Host " $icon Check 17: BACKUP.ps1 exists and is syntactically valid ($($check17Timer.ElapsedMilliseconds)ms)" -ForegroundColor $color
if ($check17Violations.Count -gt 0) {
$check17Violations | ForEach-Object { Write-Host " $_" -ForegroundColor DarkRed }
}
}
$results += @{
check = 17
name = "BACKUP.ps1 exists and is syntactically valid"
status = $check17Status
runtime_ms = $check17Timer.ElapsedMilliseconds
violations = $check17Violations
}
$stopwatch.Stop()
# ─── Cleanup ─────────────────────────────────────────────────────────────────
if ($usePsql) {
Remove-Item Env:\PGPASSWORD -ErrorAction SilentlyContinue
}
# ─── Output ──────────────────────────────────────────────────────────────────
$overall = if ($failCount -eq 0) { "pass" } else { "fail" }
if ($Json) {
$jsonResult = @{
timestamp = (Get-Date -Format "o")
environment = $Env
target = $envLabel
checks = $results
summary = @{
total = $checks.Count + 1
passed = $passCount
failed = $failCount
}
overall = $overall
runtime_ms = $stopwatch.ElapsedMilliseconds
}
$jsonString = $jsonResult | ConvertTo-Json -Depth 5
if ($OutFile) {
$jsonString | Set-Content -Path $OutFile -Encoding UTF8
Write-Host "Results written to: $OutFile"
}
else {
Write-Output $jsonString
}
}
else {
Write-Host ""
Write-Host "================================================" -ForegroundColor Cyan
Write-Host " Sanity Check Summary" -ForegroundColor Cyan
Write-Host "================================================" -ForegroundColor Cyan
Write-Host " Environment: $envLabel" -ForegroundColor $envColor
Write-Host " Checks: $($checks.Count + 1)" -ForegroundColor White
Write-Host " Passed: $passCount" -ForegroundColor Green
Write-Host " Failed: $failCount" -ForegroundColor $(if ($failCount -gt 0) { "Red" } else { "Green" })
Write-Host " Duration: $($stopwatch.Elapsed.TotalSeconds.ToString('F1'))s" -ForegroundColor White
Write-Host " Overall: $($overall.ToUpper())" -ForegroundColor $(if ($overall -eq "pass") { "Green" } else { "Red" })
Write-Host ""
}
if ($failCount -gt 0) { exit 1 }
exit 0