param([switch]$SkipDownload) $ErrorActionPreference = 'Stop' $repoRoot = Split-Path $PSScriptRoot -Parent Set-Location -LiteralPath $repoRoot $localRoot = Join-Path $repoRoot '.local' $pythonExe = Join-Path $repoRoot '.venv\Scripts\python.exe' New-Item -ItemType Directory -Force -Path $localRoot | Out-Null if (!(Test-Path -LiteralPath $pythonExe)) { py -3 -m venv .venv; if ($LASTEXITCODE) { throw 'Could not create Python environment.' } } & $pythonExe -m pip install -r mockups/requirements.txt if ($LASTEXITCODE) { throw 'Python dependency installation failed.' } if (!$env:DATABASE_URL) { $postgresBin = Join-Path $localRoot 'pgsql\bin' if (!(Test-Path -LiteralPath (Join-Path $postgresBin 'initdb.exe'))) { $archive = Join-Path $localRoot 'postgresql.zip' if (!(Test-Path -LiteralPath $archive)) { if ($SkipDownload) { throw 'Local PostgreSQL binaries and archive are missing.' } Write-Host 'Downloading PostgreSQL 18.6 portable binaries from EDB...' Invoke-WebRequest 'https://get.enterprisedb.com/postgresql/postgresql-18.6-5-windows-x64-binaries.zip' -OutFile $archive } & $pythonExe -c "import zipfile; z=zipfile.ZipFile(r'.local/postgresql.zip'); [z.extract(n,r'.local') for n in z.namelist() if n.startswith(('pgsql/bin/','pgsql/lib/','pgsql/share/'))]" if ($LASTEXITCODE) { throw 'Could not extract PostgreSQL binaries.' } } $configPath = Join-Path $localRoot 'database.json' $dataPath = Join-Path $localRoot 'pgdata' if (!(Test-Path -LiteralPath (Join-Path $dataPath 'PG_VERSION'))) { $samplePassword = & $pythonExe -c "import secrets; print(secrets.token_hex(24))" $passwordFile = Join-Path $localRoot 'init-password.txt' [System.IO.File]::WriteAllText($passwordFile,$samplePassword,[System.Text.UTF8Encoding]::new($false)) & (Join-Path $postgresBin 'initdb.exe') -D $dataPath -U sample_owner --encoding=UTF8 --locale=C --auth-host=scram-sha-256 --auth-local=scram-sha-256 --pwfile=$passwordFile if ($LASTEXITCODE) { throw 'PostgreSQL initialization failed.' } Remove-Item -LiteralPath $passwordFile $settings = @{ url = "postgresql://sample_owner:${samplePassword}@127.0.0.1:55432/stat_dashboard_sample" } | ConvertTo-Json [System.IO.File]::WriteAllText($configPath,$settings,[System.Text.UTF8Encoding]::new($false)) } & "$PSScriptRoot/start-postgres.ps1" } & $pythonExe mockups/manage_database.py init if ($LASTEXITCODE) { throw 'Database migration or sample seeding failed.' } Write-Host 'Ready. Run mockups/start-mockups.ps1 to open the database-backed mockups.'