Files
faunosteo/.claude/audits/2026-10-28-v1-2-1-m7/m7-rehearsal.sh

91 lines
6.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# M7 rehearsal on the isolated db:sandbox stack only (containers faunosteo_dbsandbox_*,
# ports 57xxx; never the DEV stack). Builds the DEV state (migrations up to M3 through
# `up upgrade`, seeds, GoTrue accounts), places the historical drawing object the way the
# operator tool did on DEV (Git bytes, service-key upload, pre-M4 confirmation RPC),
# snapshots bytes and metadata, applies M4+M5 with the CLI, snapshots again, compares.
set -uo pipefail
# Usage: bash .claude/audits/2026-10-28-v1-2-1-m7/m7-rehearsal.sh [work dir]
# The work dir (default: a new mktemp directory) receives the base copy and the logs.
HERE="$(cd "$(dirname "$0")" && pwd)"
WT="$(cd "$HERE/../../.." && pwd)"
WORK="${1:-$(mktemp -d)}"
BASE=$WORK/base-$(date +%s)
OUT=$WORK/logs
STATE="$HOME/.cache/faunosteo-db-sandbox"
mkdir -p "$OUT"
# 1. A scratch copy of the repository pieces db-sandbox.sh reads, migrations stopped at M3.
mkdir "$BASE" # a new directory per run: nothing is ever deleted
mkdir -p "$BASE/scripts" "$BASE/supabase/migrations"
cp "$WT/scripts/db-sandbox.sh" "$WT/scripts/dev-env.sh" "$BASE/scripts/"
cp "$WT/supabase/config.toml" "$WT/supabase/seed.sql" "$BASE/supabase/"
ln -s "$WT/supabase/seed" "$BASE/supabase/seed"
ln -s "$WT/node_modules" "$BASE/node_modules"
for f in "$WT"/supabase/migrations/*.sql; do
[ "$(basename "$f" | cut -d_ -f1)" \< 20261028100000 ] && cp "$f" "$BASE/supabase/migrations/"
done
ls "$BASE/supabase/migrations" > "$OUT/base-migrations.txt"
# 2. The DEV state: base migrations, seeds, the two GoTrue accounts.
(cd "$WT" && bash scripts/db-sandbox.sh down >/dev/null)
(cd "$BASE" && bash scripts/db-sandbox.sh up upgrade 20261028100000) > "$OUT/up-base.log" 2>&1
set -a; . "$STATE/secrets.sh"; set +a
export PGPASSWORD="$SB_PW"
q() { docker exec -i -e PGPASSWORD faunosteo_dbsandbox_db psql -X -q -t -A -h 127.0.0.1 -U postgres -d postgres -v ON_ERROR_STOP=1; }
ASSET=7f767f74-3b36-51a7-8465-52dd7e2ead4a
SVG="$WT/supabase/seed/anatomical-sheets/equine/draft/assets/horse.svg"
SHA=$(sha256sum "$SVG" | cut -d' ' -f1)
KEY="$SHA.svg"
echo "git file: $(stat -c %s "$SVG") bytes, sha256 $SHA" > "$OUT/fixture.txt"
echo "row sha256: $(echo "select sha256 || ' ' || byte_size from public.anatomical_sheet_assets where id = '$ASSET'" | q)" >> "$OUT/fixture.txt"
# 3. The operator path of R5.5 on DEV: object written with the service key, row confirmed by the pre-M4 RPC.
curl -sS -o "$OUT/upload.json" -w '%{http_code}\n' -X POST -H @"$STATE/service-header" -H 'Content-Type: image/svg+xml' \
--data-binary @"$SVG" "http://127.0.0.1:57500/object/anatomical-sheet-assets/$KEY" > "$OUT/upload.status"
curl -sS -o "$OUT/confirm.json" -w '%{http_code}\n' -X POST -H @"$STATE/service-header" -H 'Content-Type: application/json' \
-d "{\"asset_id\":\"$ASSET\"}" "http://127.0.0.1:57300/rpc/confirm_sheet_asset_upload" > "$OUT/confirm.status"
snapshot() { # label
{
echo "== object row"
echo "select row_to_json(o) from (select id, bucket_id, name, owner, owner_id, metadata, user_metadata, version, created_at, updated_at, last_accessed_at, path_tokens from storage.objects where bucket_id = 'anatomical-sheet-assets' and name = '$KEY') o" | q
echo "== bucket"
echo "select row_to_json(b) from (select id, public, file_size_limit, allowed_mime_types from storage.buckets where id = 'anatomical-sheet-assets') b" | q
echo "== asset row"
echo "select row_to_json(a) from (select * from public.anatomical_sheet_assets where id = '$ASSET') a" | q
echo "== historical sheet and version"
echo "select row_to_json(s) from (select id, status, current_version_id, last_version_number, updated_at, slug from public.anatomical_sheets where id = '503dc301-a8b1-50d4-9e44-0919b2fc2916') s" | q
echo "select id || ' ' || status || ' ' || version_number || ' ' || md5(content::text) || ' ' || updated_at from public.anatomical_sheet_versions where sheet_id = '503dc301-a8b1-50d4-9e44-0919b2fc2916'" | q
echo "== service_role table privileges on the catalogue tables"
echo "select coalesce(string_agg(table_name || ':' || privilege_type, ',' order by table_name, privilege_type), 'none') from information_schema.role_table_grants where grantee = 'service_role' and table_schema = 'public' and table_name in ('species', 'anatomical_sheets', 'anatomical_sheet_versions', 'anatomical_sheet_assets', 'profiles', 'profile_roles')" | q
echo "== bytes through the Storage API (service key)"
curl -sS -H @"$STATE/service-header" "http://127.0.0.1:57500/object/anatomical-sheet-assets/$KEY" | tee "$OUT/bytes-$1.svg" | sha256sum
stat -c '%s bytes' "$OUT/bytes-$1.svg"
} > "$OUT/snapshot-$1.txt"
}
snapshot before
# 4. M4 + M5 with the CLI, as the owner will apply them on DEV (migration up).
(cd "$WT" && node_modules/.bin/supabase --workdir . migration up \
--db-url "postgresql://postgres@127.0.0.1:57432/postgres?sslmode=disable") > "$OUT/migrate.log" 2>&1
echo "notify pgrst, 'reload schema'" | q
sleep 2
snapshot after
# 5. Reads through the migrated policies with real tokens.
node "$HERE/m7-read.mjs" "$KEY" > "$OUT/reads.json"
# 6. The comparison: everything but the bucket limit identical.
diff "$OUT/snapshot-before.txt" "$OUT/snapshot-after.txt" > "$OUT/snapshot.diff" || true
cmp "$OUT/bytes-before.svg" "$SVG" && cmp "$OUT/bytes-after.svg" "$SVG" && echo "bytes identical to the Git file before and after" > "$OUT/bytes.result"
echo "m7 done"
# 7. The upgraded stack under the whole test set: pgTAP, the sandbox suite twice, the types.
cd "$WT"
bash scripts/db-sandbox.sh test-db > "$OUT/testdb.log" 2>&1; echo "test-db exit $?" >> "$OUT/testdb.log"
bash scripts/db-sandbox.sh test > "$OUT/sandbox-run1.log" 2>&1; echo "run1 exit $?" >> "$OUT/sandbox-run1.log"
bash scripts/db-sandbox.sh test > "$OUT/sandbox-run2.log" 2>&1; echo "run2 exit $?" >> "$OUT/sandbox-run2.log"
bash scripts/db-sandbox.sh test-db > "$OUT/testdb-after.log" 2>&1; echo "test-db-after exit $?" >> "$OUT/testdb-after.log"
bash scripts/db-sandbox.sh types --check > "$OUT/types.log" 2>&1; echo "types exit $?" >> "$OUT/types.log"
echo "m7 all done"