Files
faunosteo/scripts/db-sandbox.sh
T

319 lines
16 KiB
Bash
Executable File

#!/usr/bin/env bash
# Throwaway Supabase stack for testing migrations without touching the DEV
# stack (docs/development/SUPABASE.md §5.1). Same images as the CLI stack,
# its own Docker network, ports published on 127.0.0.1 only and distinct from
# DEV, no Docker volume (the images declare none and nothing is mounted), so
# `down` removes only the containers and network named below.
#
# pnpm db:sandbox up fresh all migrations, then the seeds (= db reset)
# pnpm db:sandbox up upgrade <version> [<then>]
# migrations before <version>, seeds, two GoTrue
# accounts, then `migration up` (= DEV path); with
# <then>, the migrations before <then> first, then the rest
# pnpm db:sandbox test-db the pgTAP files (`supabase test db`)
# pnpm db:sandbox types [--check] generated types (write, or diff with the committed file)
# pnpm db:sandbox test packages/supabase/tests/sandbox (HTTP, concurrency)
# pnpm db:sandbox e2e-shell [args] = pnpm e2e:shell: the /app shell in Chromium (apps/app/e2e); args go to Playwright (a spec file, --grep)
# pnpm db:sandbox verify-e2e [args] the sheet verification worker against the sandbox: an
# unpublished gateway, the worker container, then
# tests/sandbox/sheet-verification.sandbox.ts (args go to vitest)
# pnpm db:sandbox sql psql as postgres on stdin
# pnpm db:sandbox down
#
# Secrets are generated at `up` (random password, JWT secret, keys) into a
# 600 file under $FAUNOSTEO_SANDBOX_DIR (default ~/.cache/faunosteo-db-sandbox),
# outside the repository, and never printed. Every Supabase CLI call names the
# sandbox with --db-url: none may fall back to the local DEV database.
set -euo pipefail
cd "$(dirname "$0")/.."
. scripts/dev-env.sh
PG_IMAGE=public.ecr.aws/supabase/postgres:17.11.0.004
AUTH_IMAGE=public.ecr.aws/supabase/gotrue:v2.197.0
REST_IMAGE=public.ecr.aws/supabase/postgrest:v16.4
STORAGE_IMAGE=public.ecr.aws/supabase/storage-api:v1.79.36
MAIL_IMAGE=public.ecr.aws/supabase/mailpit:v1.31.3
NET=faunosteo_dbsandbox_net
DB=faunosteo_dbsandbox_db
AUTH=faunosteo_dbsandbox_auth
REST=faunosteo_dbsandbox_rest
STORAGE=faunosteo_dbsandbox_storage
MAIL=faunosteo_dbsandbox_mail
GATEWAY=faunosteo_dbsandbox_gateway
WORKER=faunosteo_dbsandbox_worker_verify
GATEWAY_IMAGE=nginx:1.31-alpine
WORKER_IMAGE=faunosteo_worker:dev
PORT_DB=57432 PORT_AUTH=57999 PORT_REST=57300 PORT_STORAGE=57500 PORT_MAIL=57025
STATE="${FAUNOSTEO_SANDBOX_DIR:-$HOME/.cache/faunosteo-db-sandbox}"
SECRETS="$STATE/secrets.sh"
CLI=node_modules/.bin/supabase
die() { echo "db-sandbox: $*" >&2; exit 1; }
case "$(realpath -m "$STATE")" in
"$PWD" | "$PWD"/*) die "FAUNOSTEO_SANDBOX_DIR must be outside the repository" ;;
esac
# Every value must be present: an empty password would turn --db-url into an
# argument the CLI might read as absent and fall back to the DEV database.
load_secrets() {
[ -f "$SECRETS" ] || die "not running (pnpm db:sandbox up fresh)"
. "$SECRETS"
[ -n "${SB_PW:-}" ] && [ -n "${SB_JWT_SECRET:-}" ] && [ -n "${SB_ANON_KEY:-}" ] && [ -n "${SB_SERVICE_KEY:-}" ] \
|| die "secrets file incomplete (pnpm db:sandbox down, then up)"
export PGPASSWORD="$SB_PW"
}
# No password in the URL: the CLI reads PGPASSWORD, so no secret is on a command line.
db_url() { printf 'postgresql://postgres@127.0.0.1:%s/postgres?sslmode=disable' "$PORT_DB"; }
sb_psql() {
docker exec -i -e PGPASSWORD "$DB" \
psql -X -q -h 127.0.0.1 -U postgres -d postgres -v ON_ERROR_STOP=1 "$@"
}
wait_for() {
local what="$1"; shift
for _ in $(seq 1 120); do "$@" >/dev/null 2>&1 && return 0; sleep 1; done
die "timeout waiting for $what"
}
new_secrets() {
mkdir -p "$STATE"; chmod 700 "$STATE"
( umask 077; node -e '
const c = require("crypto");
const secret = c.randomBytes(32).toString("hex");
const b64 = (o) => Buffer.from(JSON.stringify(o)).toString("base64url");
const jwt = (p) => { const h = b64({ alg: "HS256", typ: "JWT" }), b = b64(p);
return h + "." + b + "." + c.createHmac("sha256", secret).update(h + "." + b).digest("base64url"); };
const exp = Math.floor(Date.now() / 1000) + 30 * 86400;
console.log("SB_PW=" + c.randomBytes(24).toString("hex"));
console.log("SB_JWT_SECRET=" + secret);
console.log("SB_ANON_KEY=" + jwt({ role: "anon", iss: "db-sandbox", exp }));
console.log("SB_SERVICE_KEY=" + jwt({ role: "service_role", iss: "db-sandbox", exp }));
' > "$SECRETS.new" && mv "$SECRETS.new" "$SECRETS" )
load_secrets
# The values reach the containers through 600 env files, never through argv.
( umask 077
printf 'POSTGRES_PASSWORD=%s\nPGPASSWORD=%s\nJWT_SECRET=%s\n' "$SB_PW" "$SB_PW" "$SB_JWT_SECRET" > "$STATE/db.env"
printf 'GOTRUE_DB_DATABASE_URL=postgresql://supabase_auth_admin:%s@%s:5432/postgres\nGOTRUE_JWT_SECRET=%s\n' \
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/auth.env"
printf 'ANON_KEY=%s\nSERVICE_KEY=%s\nAUTH_JWT_SECRET=%s\nDATABASE_URL=postgresql://supabase_storage_admin:%s@%s:5432/postgres\n' \
"$SB_ANON_KEY" "$SB_SERVICE_KEY" "$SB_JWT_SECRET" "$SB_PW" "$DB" > "$STATE/storage.env"
printf 'PGRST_DB_URI=postgresql://authenticator:%s@%s:5432/postgres\nPGRST_JWT_SECRET=%s\n' \
"$SB_PW" "$DB" "$SB_JWT_SECRET" > "$STATE/rest.env"
printf 'Authorization: Bearer %s\n' "$SB_SERVICE_KEY" > "$STATE/service-header" )
}
# The role passwords the CLI sets at db start (its /etc/postgresql.schema.sql),
# reduced to what the sandbox services use.
DB_INIT='cat <<'"'"'EOF'"'"' > /etc/postgresql.schema.sql && exec docker-entrypoint.sh postgres -D /etc/postgresql
\set pgpass `echo "$PGPASSWORD"`
\set jwt_secret `echo "$JWT_SECRET"`
ALTER DATABASE postgres SET "app.settings.jwt_secret" TO :'"'"'jwt_secret'"'"';
ALTER DATABASE postgres SET "app.settings.jwt_exp" TO '"'"'3600'"'"';
ALTER USER postgres WITH PASSWORD :'"'"'pgpass'"'"';
ALTER USER authenticator WITH PASSWORD :'"'"'pgpass'"'"';
ALTER USER supabase_auth_admin WITH PASSWORD :'"'"'pgpass'"'"';
ALTER USER supabase_storage_admin WITH PASSWORD :'"'"'pgpass'"'"';
EOF'
start_services() {
docker network create "$NET" >/dev/null
docker run -d --name "$DB" --network "$NET" -p 127.0.0.1:$PORT_DB:5432 \
--env-file "$STATE/db.env" -e POSTGRES_HOST=/var/run/postgresql -e JWT_EXP=3600 \
-e POSTGRES_INITDB_ARGS="--allow-group-access --locale-provider=icu --encoding=UTF-8 --icu-locale=en_US.UTF-8" \
--entrypoint sh "$PG_IMAGE" -c "$DB_INIT" >/dev/null
wait_for "database roles" docker exec -e PGPASSWORD "$DB" \
psql -h 127.0.0.1 -U supabase_auth_admin -d postgres -c 'select 1'
wait_for "database" docker exec -e PGPASSWORD "$DB" \
psql -h 127.0.0.1 -U postgres -d postgres -c 'select 1'
docker run -d --name "$MAIL" --network "$NET" -p 127.0.0.1:$PORT_MAIL:8025 "$MAIL_IMAGE" >/dev/null
docker run -d --name "$AUTH" --network "$NET" -p 127.0.0.1:$PORT_AUTH:9999 \
-e GOTRUE_API_HOST=0.0.0.0 -e GOTRUE_API_PORT=9999 -e API_EXTERNAL_URL=http://127.0.0.1:$PORT_AUTH \
-e GOTRUE_DB_DRIVER=postgres --env-file "$STATE/auth.env" \
-e GOTRUE_DB_MIGRATIONS_PATH=/usr/local/etc/auth/migrations \
-e GOTRUE_SITE_URL=http://127.0.0.1:5173 -e GOTRUE_DISABLE_SIGNUP=false \
-e GOTRUE_JWT_EXP=3600 \
-e GOTRUE_JWT_AUD=authenticated -e GOTRUE_JWT_DEFAULT_GROUP_NAME=authenticated \
-e GOTRUE_JWT_ADMIN_ROLES=service_role \
-e GOTRUE_EXTERNAL_EMAIL_ENABLED=true -e GOTRUE_EXTERNAL_PHONE_ENABLED=false \
-e GOTRUE_EXTERNAL_ANONYMOUS_USERS_ENABLED=false -e GOTRUE_MAILER_AUTOCONFIRM=false \
-e GOTRUE_MAILER_URLPATHS_CONFIRMATION=http://127.0.0.1:$PORT_AUTH/verify \
-e GOTRUE_SMTP_HOST="$MAIL" -e GOTRUE_SMTP_PORT=1025 \
-e GOTRUE_SMTP_ADMIN_EMAIL=admin@sandbox.local -e GOTRUE_SMTP_SENDER_NAME=sandbox \
-e GOTRUE_SMTP_MAX_FREQUENCY=1s -e GOTRUE_RATE_LIMIT_EMAIL_SENT=10000 \
-e GOTRUE_RATE_LIMIT_VERIFY=10000 -e GOTRUE_RATE_LIMIT_TOKEN_REFRESH=10000 \
-e GOTRUE_PASSWORD_MIN_LENGTH=12 "$AUTH_IMAGE" >/dev/null
wait_for "auth" curl -fsS http://127.0.0.1:$PORT_AUTH/health
docker run -d --name "$STORAGE" --network "$NET" -p 127.0.0.1:$PORT_STORAGE:5000 \
--env-file "$STATE/storage.env" \
-e FILE_SIZE_LIMIT=52428800 -e STORAGE_BACKEND=file -e FILE_STORAGE_BACKEND_PATH=/mnt \
-e TENANT_ID=stub -e STORAGE_S3_REGION=local -e GLOBAL_S3_BUCKET=stub \
-e ENABLE_IMAGE_TRANSFORMATION=false -e IMAGE_TRANSFORMATION_ENABLED=false \
-e S3_PROTOCOL_ENABLED=false -e DB_MIGRATIONS_FREEZE_AT= "$STORAGE_IMAGE" >/dev/null
wait_for "storage" curl -fsS http://127.0.0.1:$PORT_STORAGE/status
}
start_rest() {
docker run -d --name "$REST" --network "$NET" -p 127.0.0.1:$PORT_REST:3000 \
--env-file "$STATE/rest.env" \
-e PGRST_DB_SCHEMAS=public -e PGRST_DB_EXTRA_SEARCH_PATH=public,extensions \
-e PGRST_DB_ANON_ROLE=anon -e PGRST_DB_MAX_ROWS=1000 \
"$REST_IMAGE" >/dev/null
wait_for "postgrest" curl -fsS -o /dev/null http://127.0.0.1:$PORT_REST/
}
# The CLI's own migration runner, always against the sandbox URL.
migrate_up() { # workdir
"$CLI" --workdir "$1" migration up --db-url "$(db_url)"
}
seed() {
local paths
paths="$(sed -n 's/^sql_paths = \[\(.*\)\]/\1/p' supabase/config.toml | tr -d '" ' | tr ',' ' ')"
for f in $paths; do
echo " seed ${f#./}"; sb_psql --single-transaction < "supabase/${f#./}" >/dev/null
done
}
gotrue_accounts() {
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/admin/users \
-H @"$STATE/service-header" -H 'Content-Type: application/json' \
-d '{"email":"pre-confirmed@sandbox.local","password":"Sandbox-only-pw-1","email_confirm":true}' >/dev/null
curl -fsS -X POST http://127.0.0.1:$PORT_AUTH/signup -H 'Content-Type: application/json' \
-d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null
}
# A CLI workdir holding only the migrations before <version>; kept in the
# state folder and refilled at each run (the CLI writes .temp there).
upgrade_workdir() { # version
local base="$STATE/upgrade-base"
mkdir -p "$base/supabase/migrations"
rm -f "$base"/supabase/migrations/*.sql
cp supabase/config.toml "$base/supabase/"
for f in supabase/migrations/*.sql; do
if [ "$(basename "$f" | cut -d_ -f1)" \< "$1" ]; then cp "$f" "$base/supabase/migrations/"; fi
done
printf '%s' "$base"
}
cmd_up() {
local mode="${1:-}"
# Arguments checked before anything starts: a refusal leaves no container or secret.
case "$mode" in
fresh) ;;
upgrade) [[ "${2:-}" =~ ^[0-9]{14}$ ]] \
|| die "up upgrade needs the first new migration version, e.g. 20261025100000"
[ -z "${3:-}" ] || { [[ "$3" =~ ^[0-9]{14}$ ]] && [ "$2" \< "$3" ]; } \
|| die "up upgrade <version> <then>: <then> is a later migration version" ;;
*) die "up fresh | up upgrade <version> [<then>]" ;;
esac
docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)"
new_secrets
start_services
case "$mode" in
fresh)
migrate_up . ; seed ;;
upgrade)
migrate_up "$(upgrade_workdir "$2")"
seed; gotrue_accounts
echo "== before: $(sb_psql -t -A -c 'select count(*) from public.profiles') profiles"
if [ -n "${3:-}" ]; then
migrate_up "$(upgrade_workdir "$3")"
echo "== between: $(sb_psql -t -A -c 'select count(*) from public.workspaces') workspaces"
fi
migrate_up . ;;
esac
start_rest
echo "== sandbox up ($mode): db 127.0.0.1:$PORT_DB, auth :$PORT_AUTH, rest :$PORT_REST, mail :$PORT_MAIL"
}
cmd_down() {
docker rm -f "$WORKER" "$GATEWAY" "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
docker network rm "$NET" >/dev/null 2>&1 || true
rm -f "$SECRETS" "$STATE/db.env" "$STATE/auth.env" "$STATE/storage.env" "$STATE/rest.env" "$STATE/service-header"
echo "== sandbox removed"
}
# A test runner gets the endpoints and keys through its environment, never argv.
with_sandbox_env() (
export SANDBOX_DB_CONTAINER="$DB" SANDBOX_PG_PASSWORD="$SB_PW"
export SANDBOX_AUTH_URL="http://127.0.0.1:$PORT_AUTH" SANDBOX_REST_URL="http://127.0.0.1:$PORT_REST"
export SANDBOX_STORAGE_URL="http://127.0.0.1:$PORT_STORAGE" SANDBOX_MAIL_URL="http://127.0.0.1:$PORT_MAIL"
export SANDBOX_ANON_KEY="$SB_ANON_KEY" SANDBOX_SERVICE_KEY="$SB_SERVICE_KEY"
"$@"
)
# The Kong routes the worker calls (/rest/v1/, /storage/v1/), on the sandbox
# network only: no host port is published.
GATEWAY_CONF='server {
listen 8000;
client_max_body_size 8m;
proxy_http_version 1.1;
location /rest/v1/ { proxy_pass http://'"$REST"':3000/; }
location /storage/v1/ { proxy_pass http://'"$STORAGE"':5000/; }
location / { return 404; }
}'
# The worker's JSON lines hold no secret by design (crates/worker log allowlist);
# they are printed only after checking that neither key appears in them.
print_worker_log() {
local lines
lines="$(docker logs "$WORKER" 2>&1)" || return 0
case "$lines" in
*"$SB_SERVICE_KEY"* | *"$SB_ANON_KEY"*) echo "== worker log withheld: it contains a key" ;;
*) echo "== worker log"; printf '%s\n' "$lines" ;;
esac
}
verify_cleanup() {
[ -n "${verify_env:-}" ] && rm -f "$verify_env"
print_worker_log
docker rm -f "$WORKER" "$GATEWAY" >/dev/null 2>&1 || true
}
cmd_verify_e2e() {
load_secrets
docker image inspect "$WORKER_IMAGE" >/dev/null 2>&1 || die "$WORKER_IMAGE missing (pnpm worker:image)"
docker rm -f "$WORKER" "$GATEWAY" >/dev/null 2>&1 || true
trap verify_cleanup EXIT
docker run -d --name "$GATEWAY" --network "$NET" --security-opt no-new-privileges:true \
--entrypoint sh "$GATEWAY_IMAGE" -c 'printf "%s\n" "$0" > /etc/nginx/conf.d/default.conf && exec nginx -g "daemon off;"' \
"$GATEWAY_CONF" >/dev/null
wait_for "gateway" docker exec "$GATEWAY" wget -q -O /dev/null http://127.0.0.1:8000/rest/v1/
# The service key reaches the container through a 600 file removed once it started.
verify_env="$(umask 077; mktemp "$STATE/worker-verify.XXXXXX")"
printf 'SUPABASE_URL=http://%s:8000\nSUPABASE_SERVICE_ROLE_KEY=%s\nWORKER_ID=sandbox-verify\nWORKER_POLL_SECONDS=1\n' \
"$GATEWAY" "$SB_SERVICE_KEY" > "$verify_env"
docker run -d --name "$WORKER" --network "$NET" --env-file "$verify_env" \
--read-only --cap-drop ALL --security-opt no-new-privileges:true \
--memory 1280m --cpus 1 --pids-limit 64 --user nonroot \
"$WORKER_IMAGE" run --jobs sheet-verify >/dev/null
rm -f "$verify_env"; verify_env=
for _ in $(seq 1 30); do
docker logs "$WORKER" 2>&1 | grep -q '"event":"worker_start"' && break
[ "$(docker inspect -f '{{.State.Running}}' "$WORKER")" = true ] || die "worker exited before worker_start"
sleep 1
done
docker logs "$WORKER" 2>&1 | grep -q '"event":"worker_start"' || die "timeout waiting for worker_start"
echo "== worker up ($WORKER_IMAGE, run --jobs sheet-verify, via $GATEWAY)"
SANDBOX_VERIFY_WORKER="$WORKER" with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase \
test:sandbox tests/sandbox/sheet-verification.sandbox.ts "$@"
}
case "${1:-}" in
up) shift; cmd_up "$@" ;;
down) cmd_down ;;
test-db) load_secrets; "$CLI" test db --db-url "$(db_url)" ;;
types)
load_secrets
out="$("$CLI" gen types typescript --db-url "$(db_url)" --schema public | node_modules/.bin/prettier --parser typescript)"
if [ "${2:-}" = --check ]; then
diff <(printf '%s\n' "$out") packages/supabase/src/database.types.ts && echo "== types match"
else
printf '%s\n' "$out" > packages/supabase/src/database.types.ts; echo "== types written"
fi ;;
test) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox "$@" ;;
e2e-shell) shift; load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell "$@" ;;
verify-e2e) shift; cmd_verify_e2e "$@" ;;
sql) load_secrets; sb_psql ;;
*) sed -n '2,26p' "$0"; exit 1 ;;
esac