diff --git a/scripts/db-sandbox.sh b/scripts/db-sandbox.sh index b9bb0002..7aa43789 100755 --- a/scripts/db-sandbox.sh +++ b/scripts/db-sandbox.sh @@ -6,8 +6,10 @@ # `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 migrations before , seeds, two -# GoTrue accounts, then `migration up` (= DEV path) +# pnpm db:sandbox up upgrade [] +# migrations before , seeds, two GoTrue +# accounts, then `migration up` (= DEV path); with +# , the migrations before 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) @@ -172,14 +174,29 @@ gotrue_accounts() { -d '{"email":"pre-unconfirmed@sandbox.local","password":"Sandbox-only-pw-1"}' >/dev/null } +# A CLI workdir holding only the migrations before ; 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" ;; - *) die "up fresh | up upgrade " ;; + || die "up upgrade needs the first new migration version, e.g. 20261025100000" + [ -z "${3:-}" ] || { [[ "$3" =~ ^[0-9]{14}$ ]] && [ "$2" \< "$3" ]; } \ + || die "up upgrade : is a later migration version" ;; + *) die "up fresh | up upgrade []" ;; esac docker ps -a --format '{{.Names}}' | grep -q '^faunosteo_dbsandbox_' && die "already up (pnpm db:sandbox down)" new_secrets @@ -188,19 +205,13 @@ cmd_up() { fresh) migrate_up . ; seed ;; upgrade) - local version="$2" - # A CLI workdir holding only the migrations before ; 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" + 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 @@ -238,5 +249,5 @@ case "${1:-}" in test) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/supabase test:sandbox ;; e2e-shell) load_secrets; with_sandbox_env node_modules/.bin/pnpm --filter @faunosteo/app e2e:shell ;; sql) load_secrets; sb_psql ;; - *) sed -n '2,24p' "$0"; exit 1 ;; + *) sed -n '2,23p' "$0"; exit 1 ;; esac