mirror of
https://github.com/bitbaum/loki.git
synced 2026-12-04 07:59:11 +01:00
botsmann has served 503 from `/api/health` because `public.consultations` does
not exist. It never existed: all ELEVEN of its migrations were unapplied, and
no deploy ever noticed.
The manifest is why. botsmann's `db` field was `-`, and deploy.sh reads that as
"no database" and skips the schema step entirely — so every deploy went green
while shipping code written against a schema nobody had applied. apply-schema.sh
even documents this app by name ("orangecat and botsmann use the SAME supabase
database"); it was simply never reached.
Setting `db` to a database name would have been worse than the bug. One
supabase database backs several apps, orangecat owns `public` and its 128
tables, and botsmann's 001 defines `CREATE OR REPLACE FUNCTION
update_updated_at()` — a name public ALREADY holds. Applying it there would
have silently redefined the function orangecat's triggers call. Table names
collide too (`conversations`, `documents`, `waitlist`).
So the db field learns a third form, `supabase:<schema>`, and botsmann gets its
own schema exactly as printcraft already has one:
* apply-schema.sh creates the schema, grants PostgREST's roles usage AND
default privileges (a one-off GRANT would miss every table the migrations
are about to create), and runs the batch under `SET LOCAL search_path` —
which is what actually keeps unqualified DDL out of public.
* The ledger moves beside the tables it describes. A shared
`public._deploy_schema_history` would mix two apps' tags and make "up to
date" meaningless for both.
* The baseline guard counts the app's OWN tables. Counting public let another
app's 128 tables hide an empty schema.
* Extension schemas are resolved, not hardcoded: pgvector lives in the
`printcraft` schema because printcraft's migration created it unqualified,
so `vector` is not reachable from a bare search_path.
* gen-env.sh treats `supabase:*` as having no host DATABASE_URL — those apps
have no row in ~/.db-credentials and would have failed there.
Two things that were gates in name only:
* The destructive guard scanned raw text, so `DELETE FROM rate_limits` inside
the BODY of `CREATE FUNCTION cleanup_rate_limits()` counted as a destructive
migration. Three such lines across two of botsmann's migrations would have
aborted the whole deploy on code that deletes nothing at apply time. Bodies
are now blanked before the scan, line numbers preserved. A gate that fails
red on good code is the worst kind — the only way past it is to switch it
off.
* Applied is not the same as REACHABLE. PostgREST serves only the schemas in
PGRST_DB_SCHEMAS; anything else answers PGRST205 "Could not find the table",
which is the very 503 this fixes. After applying, the step now refuses
rather than warns, and prints the exact command.
New test-apply-schema.sh, wired into `test:ops` and therefore `verify`: 28
checks, no box, no database, no checkout. Both directions are pinned — `-` still
means skip so this cannot start provisioning schemas for camille or substrata,
a real DROP TABLE still blocks, and botsmann's actual 11 migrations are run
through the guard. Mutation-checked: allowing `supabase:public` through fails a
test.
Claude-Session: https://claude.ai/code/session_01HVwg8DKHQktxJuHeLM3xpG
Co-authored-by: t <t@t.t>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
58 lines
2.5 KiB
Bash
58 lines
2.5 KiB
Bash
#!/usr/bin/env bash
|
|
# Build <repo>/<app_dir>/.env.selfhost.local for an app from its captured
|
|
# env backup ($ENV_BACKUP_DIR, default ~/dev/env-backup — where the one-time
|
|
# env snapshots were captured). Strips host-runner vars, rewrites DB URL to the
|
|
# box-local Postgres, points auth/site URLs at the new domain, adds
|
|
# AUTH_TRUST_HOST for proxied Auth.js.
|
|
# Usage: gen-env.sh <app>
|
|
source "$(dirname "${BASH_SOURCE[0]}")/lib.sh"
|
|
app_lookup "${1:?usage: gen-env.sh <app>}"
|
|
|
|
SRC_DIR="$REPO/$APP_DIR"
|
|
OUT="$SRC_DIR/.env.selfhost.local"
|
|
DOMAIN="${DOMAINS%%,*}"
|
|
|
|
# Source env: the captured per-app backup file.
|
|
RAW=$(mktemp); trap 'rm -f "$RAW"' EXIT
|
|
BACKUP="${ENV_BACKUP_DIR:-$HOME/dev/env-backup}/$NAME.env"
|
|
[ -f "$BACKUP" ] && cp "$BACKUP" "$RAW"
|
|
[ -s "$RAW" ] || { echo "WARN: no env backup found for $NAME at $BACKUP — writing minimal env"; : > "$RAW"; }
|
|
|
|
DB_URL=""
|
|
# `supabase:<schema>` names a schema inside the supabase-db container, not a
|
|
# host database — those apps reach it over SUPABASE_URL and have no row in
|
|
# ~/.db-credentials, so building a host DATABASE_URL for them would fail here.
|
|
case "$DB" in supabase:*) DB="-";; esac
|
|
if [ "$DB" != "-" ]; then
|
|
pass=$(box "grep '^$DB|' ~/.db-credentials | cut -d'|' -f2")
|
|
[ -z "$pass" ] && { echo "ERROR: no credentials for db $DB on box"; exit 1; }
|
|
DB_URL="postgresql://$DB:$pass@localhost:5432/$DB"
|
|
fi
|
|
|
|
{
|
|
echo "# Generated by gen-env.sh $(date +%F) — runtime env for $DOMAIN"
|
|
while IFS= read -r line; do
|
|
case "$line" in \#*|"") continue;; esac
|
|
key="${line%%=*}"
|
|
val="${line#*=}"
|
|
val="${val%\"}"; val="${val#\"}" # strip surrounding quotes
|
|
# Strip ALL literal \n / \r sequences and real CR/LF anywhere in the value —
|
|
# not just one trailing token. A prior incident corrupted secrets with
|
|
# multiple embedded \n, which broke all email + the private-zone PIN.
|
|
val="${val//\\n/}"; val="${val//\\r/}"
|
|
val="${val//$'\n'/}"; val="${val//$'\r'/}"
|
|
case "$key" in
|
|
VERCEL|VERCEL_*|NX_DAEMON|TURBO_*) continue ;;
|
|
DATABASE_URL|DIRECT_URL|POSTGRES_URL|POSTGRES_PRISMA_URL|POSTGRES_URL_NON_POOLING)
|
|
[ -n "$DB_URL" ] && val="$DB_URL" ;;
|
|
NEXTAUTH_URL|AUTH_URL|NEXT_PUBLIC_APP_URL|NEXT_PUBLIC_BASE_URL|NEXT_PUBLIC_SITE_URL)
|
|
val="https://$DOMAIN" ;;
|
|
esac
|
|
printf '%s="%s"\n' "$key" "${val//\"/\\\"}"
|
|
done < "$RAW"
|
|
[ -n "$DB_URL" ] && grep -qE '^(DATABASE_URL)=' "$RAW" || { [ -n "$DB_URL" ] && echo "DATABASE_URL=$DB_URL"; }
|
|
echo "AUTH_TRUST_HOST=true"
|
|
} > "$OUT"
|
|
chmod 600 "$OUT"
|
|
echo "wrote $OUT ($(grep -c '=' "$OUT") vars, db=$DB)"
|