Files
accounted/scripts/tool-pg/reset.sh
T
Jakob Wennberg 325c827322 test(mcp): run the tools against a real PostgREST, not a fake supabase (#1983)
All 100 files in extensions/general/mcp-server/__tests__ fake supabase.
query-journal.test.ts says out loud that its query chain is "exercised by the
live MCP smoke test", and no such test exists in CI. So the PostgREST grammar
of 157 tools, every .select() column string, every resource embed, every
or=(...) form, is gated by nothing and fails first in production.

pg-real cannot cover this: it holds a pg Pool and writes SQL, and none of that
grammar is resolved by Postgres. It is resolved by PostgREST at request time.

Adds a tool-pg vitest project, a docker-compose stack, a reset script that
replays every migration the way the pg-real CI job does, and a CI job.

The first sweep covers 74 read tools and finds no malformed query, across 87
real requests. That number is honest rather than impressive: with an empty
argument set many tools bail before querying. Per-tool fixtures are what
deepen it, and this harness is what makes writing them worth the effort.

Includes a self-test that injects a bad column and asserts the harness detects
it. That is not ceremony. It caught this file passing green while exercising
nothing, twice: once locally where supabase-js prefixes /rest/v1 onto a bare
PostgREST that does not serve it, and once on CI where Node 20 has no native
WebSocket, so every client construction threw and was swallowed by the
per-tool catch as a domain refusal. The client is now built once outside that
catch, the proof-of-life assertion counts real requests instead of being
trivially satisfiable, and realtime gets an inert transport.

Also excludes .next from all three vitest projects. These projects override
vitest's default excludes, so a local `npm run build` leaves a traced copy of
the repo that gets collected as a second set of test files.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-27 18:11:41 +02:00

83 lines
3.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# Rebuild the MCP tool-integration database from scratch.
#
# Mirrors the pg-real CI job step for step (bootstrap.sql, then every migration
# in filename order with ON_ERROR_STOP), so a schema that passes there passes
# here. Two additions:
#
# * The container is recreated rather than the schemas dropped. Dropping is the
# obvious approach and it does not work: `storage` is owned by
# supabase_storage_admin, so `DROP SCHEMA storage` fails as postgres, and
# dropping only `public` leaves the storage RLS policies that migration
# 20240101000024 creates unconditionally, which aborts the next replay
# partway through and leaves a half-migrated database that looks like a
# migration bug. A fresh volume costs about fifteen seconds and removes the
# entire class.
#
# * PostgREST caches the schema at boot, so a freshly-migrated database is
# invisible to it until it is told to look again.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
COMPOSE_FILE="$REPO_ROOT/tests/tool-pg/docker-compose.yml"
compose() { docker compose -f "$COMPOSE_FILE" "$@"; }
psql_run() {
compose exec -T postgres \
psql "postgresql://postgres:postgres@localhost:5432/postgres" -v ON_ERROR_STOP=1 -q "$@"
}
echo "==> recreating containers with a fresh volume"
compose down -v --remove-orphans >/dev/null 2>&1 || true
compose up -d --wait >/dev/null
echo "==> waiting for postgres"
for _ in $(seq 1 90); do
if compose exec -T postgres pg_isready -U postgres >/dev/null 2>&1; then break; fi
sleep 1
done
echo "==> bootstrap storage schema"
psql_run -f - < "$REPO_ROOT/tests/pg/bootstrap.sql" >/dev/null 2>&1
# The image grants these at init, but DEFAULT PRIVILEGES are what make the
# grants apply to the ~400 tables the migrations are about to create. Without
# them PostgREST answers every request with 42501 "permission denied".
echo "==> default privileges for the supabase roles"
psql_run -c "
GRANT USAGE ON SCHEMA public TO postgres, anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT ALL ON TABLES TO postgres, anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT ALL ON ROUTINES TO postgres, anon, authenticated, service_role;
ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT ALL ON SEQUENCES TO postgres, anon, authenticated, service_role;
" >/dev/null
echo "==> applying migrations"
count=0
for f in "$REPO_ROOT"/supabase/migrations/*.sql; do
if ! psql_run -f - < "$f" >/dev/null 2>/tmp/tool-pg-migrate.err; then
echo "FAILED on $(basename "$f")" >&2
tail -20 /tmp/tool-pg-migrate.err >&2
exit 1
fi
count=$((count + 1))
if [ $((count % 200)) -eq 0 ]; then echo " ... $count migrations applied"; fi
done
echo "==> $count migrations applied"
echo "==> granting on everything the migrations created"
psql_run -c "
GRANT ALL ON ALL TABLES IN SCHEMA public TO postgres, anon, authenticated, service_role;
GRANT ALL ON ALL ROUTINES IN SCHEMA public TO postgres, anon, authenticated, service_role;
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO postgres, anon, authenticated, service_role;
" >/dev/null
echo "==> reloading PostgREST schema cache"
psql_run -c "NOTIFY pgrst, 'reload schema';" >/dev/null
sleep 3
echo "==> ready"