scripts/db-sandbox.sh (pnpm db:sandbox): DEV images on an own network, loopback ports distinct from DEV, no volume, secrets generated outside Git; fresh and upgrade modes through the CLI's migration up, test db and gen types with --db-url. packages/supabase/tests/sandbox: M1 sign-up and confirmation, reads and refusals per role, residual GoTrue admin path, correlation cap, concurrent sessions.
211 lines
10 KiB
Bash
Executable File
211 lines
10 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> migrations before <version>, seeds, two
|
|
# GoTrue accounts, then `migration up` (= DEV path)
|
|
# 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 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
|
|
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; }
|
|
load_secrets() { [ -f "$SECRETS" ] || die "not running (pnpm db:sandbox up fresh)"; . "$SECRETS"; }
|
|
db_url() { printf 'postgresql://postgres:%s@127.0.0.1:%s/postgres?sslmode=disable' "$SB_PW" "$PORT_DB"; }
|
|
sb_psql() {
|
|
docker exec -i -e PGPASSWORD="$SB_PW" "$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" )
|
|
}
|
|
|
|
# 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 \
|
|
-e POSTGRES_PASSWORD="$SB_PW" -e PGPASSWORD="$SB_PW" -e POSTGRES_HOST=/var/run/postgresql \
|
|
-e JWT_SECRET="$SB_JWT_SECRET" -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="$SB_PW" "$DB" \
|
|
psql -h 127.0.0.1 -U supabase_auth_admin -d postgres -c 'select 1'
|
|
wait_for "database" docker exec -e PGPASSWORD="$SB_PW" "$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 \
|
|
-e GOTRUE_DB_DATABASE_URL="postgresql://supabase_auth_admin:$SB_PW@$DB:5432/postgres" \
|
|
-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_SECRET="$SB_JWT_SECRET" -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 \
|
|
-e ANON_KEY="$SB_ANON_KEY" -e SERVICE_KEY="$SB_SERVICE_KEY" -e AUTH_JWT_SECRET="$SB_JWT_SECRET" \
|
|
-e DATABASE_URL="postgresql://supabase_storage_admin:$SB_PW@$DB:5432/postgres" \
|
|
-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 \
|
|
-e PGRST_DB_URI="postgresql://authenticator:$SB_PW@$DB:5432/postgres" \
|
|
-e PGRST_DB_SCHEMAS=public -e PGRST_DB_EXTRA_SEARCH_PATH=public,extensions \
|
|
-e PGRST_DB_ANON_ROLE=anon -e PGRST_JWT_SECRET="$SB_JWT_SECRET" -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 "Authorization: Bearer $SB_SERVICE_KEY" -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
|
|
}
|
|
|
|
cmd_up() {
|
|
local mode="${1:-}"
|
|
docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)"
|
|
new_secrets; . "$SECRETS"
|
|
start_services
|
|
case "$mode" in
|
|
fresh)
|
|
migrate_up . ; seed ;;
|
|
upgrade)
|
|
local version="${2:?upgrade needs the first new migration version, e.g. 20261025100000}"
|
|
# A CLI workdir holding only the migrations before <version>; kept in the
|
|
# state folder and refilled at each upgrade run (the CLI writes .temp there).
|
|
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)" \< "$version" ]; then cp "$f" "$base/supabase/migrations/"; fi
|
|
done
|
|
migrate_up "$base"
|
|
seed; gotrue_accounts
|
|
echo "== before: $(sb_psql -t -A -c 'select count(*) from public.profiles') profiles"
|
|
migrate_up . ;;
|
|
*) die "up fresh | up upgrade <version>" ;;
|
|
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 "$REST" "$STORAGE" "$AUTH" "$MAIL" "$DB" >/dev/null 2>&1 || true
|
|
docker network rm "$NET" >/dev/null 2>&1 || true
|
|
rm -f "$SECRETS"
|
|
echo "== sandbox removed"
|
|
}
|
|
|
|
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)
|
|
load_secrets
|
|
SANDBOX_DB_CONTAINER="$DB" SANDBOX_PG_PASSWORD="$SB_PW" SANDBOX_AUTH_URL="http://127.0.0.1:$PORT_AUTH" \
|
|
SANDBOX_REST_URL="http://127.0.0.1:$PORT_REST" SANDBOX_MAIL_URL="http://127.0.0.1:$PORT_MAIL" \
|
|
SANDBOX_ANON_KEY="$SB_ANON_KEY" SANDBOX_SERVICE_KEY="$SB_SERVICE_KEY" \
|
|
node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox ;;
|
|
sql) load_secrets; sb_psql ;;
|
|
*) sed -n '2,23p' "$0"; exit 1 ;;
|
|
esac
|