# Synchronisiert die lokale PostgreSQL-Kopie mit der Supabase-Datenbank. # Voraussetzungen: # - PostgreSQL 17 lokal installiert (Standardpfad, Dienst läuft) # - Umgebungsvariablen: # SUPABASE_DB_PASSWORD = Datenbank-Passwort des Supabase-Projekts # LOCAL_PG_PASSWORD = Passwort des lokalen postgres-Benutzers # Aufruf: powershell -File scripts\sync-local-db.ps1 $ErrorActionPreference = "Stop" $PgBin = "C:\Program Files\PostgreSQL\17\bin" $RemoteHost = "aws-0-eu-central-1.pooler.supabase.com" $RemoteUser = "postgres.hvzikwjoyhoakedlywhj" $LocalDir = Join-Path $PSScriptRoot "..\supabase\local" if (-not $env:SUPABASE_DB_PASSWORD) { throw "SUPABASE_DB_PASSWORD ist nicht gesetzt." } if (-not $env:LOCAL_PG_PASSWORD) { throw "LOCAL_PG_PASSWORD ist nicht gesetzt." } # 1. Aktuellen Stand aus Supabase ziehen (Session-Pooler, Port 5432) $env:PGPASSWORD = $env:SUPABASE_DB_PASSWORD $SchemaFile = Join-Path $LocalDir "10_schema.sql" & "$PgBin\pg_dump" -h $RemoteHost -p 5432 -U $RemoteUser -d postgres ` --schema=public --schema-only --no-owner --no-privileges ` -f $SchemaFile & "$PgBin\pg_dump" -h $RemoteHost -p 5432 -U $RemoteUser -d postgres ` --schema=public --data-only --no-owner --disable-triggers ` -f (Join-Path $LocalDir "20_data.sql") # pg_dump legt "CREATE SCHEMA public;" und \restrict/\unrestrict-Marker an. # Das offizielle postgres-Docker-Image besitzt "public" bereits und bricht # mit ON_ERROR_STOP=1 sonst das komplette Init-Skript ab. (Get-Content $SchemaFile) ` -replace '^CREATE SCHEMA public;$', 'CREATE SCHEMA IF NOT EXISTS public;' ` -replace '^\\restrict .*$', '' ` -replace '^\\unrestrict .*$', '' | Set-Content $SchemaFile # 2. Lokale Datenbank neu aufbauen $env:PGPASSWORD = $env:LOCAL_PG_PASSWORD & "$PgBin\psql" -h localhost -U postgres -c "drop database if exists speisekarte;" & "$PgBin\psql" -h localhost -U postgres -c "create database speisekarte;" foreach ($file in @("00_stubs.sql", "10_schema.sql", "15_auth_users.sql", "20_data.sql")) { & "$PgBin\psql" -h localhost -U postgres -d speisekarte -q -f (Join-Path $LocalDir $file) } Write-Host "Lokale Datenbank 'speisekarte' wurde aktualisiert."