|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Generates or verifies a snapshot of the OpenAPI (Swagger 2.0) document |
| 3 | +# emitted by PostgREST when run against the spec test fixture schema. |
| 4 | +# |
| 5 | +# Intended invocation (assumes nix devShell with withTools available): |
| 6 | +# |
| 7 | +# # Regenerate the snapshot (write mode, default): |
| 8 | +# postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \ |
| 9 | +# postgrest-with-pgrst test/openapi-drift-check.sh |
| 10 | +# |
| 11 | +# # Verify the snapshot is in sync (CI mode): |
| 12 | +# postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \ |
| 13 | +# postgrest-with-pgrst test/openapi-drift-check.sh --check |
| 14 | +# |
| 15 | +# The script expects PGRST_SERVER_UNIX_SOCKET to be set, which |
| 16 | +# postgrest-with-pgrst provides. |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +MODE="${1:-write}" |
| 21 | +SNAPSHOT="test/spec/fixtures/openapi-snapshot.json" |
| 22 | + |
| 23 | +: "${PGRST_SERVER_UNIX_SOCKET:?PGRST_SERVER_UNIX_SOCKET is required; run this script via postgrest-with-pgrst}" |
| 24 | + |
| 25 | +TMPFILE="$(mktemp)" |
| 26 | +trap 'rm -f "$TMPFILE"' EXIT |
| 27 | + |
| 28 | +curl -sSf -H "Accept: application/openapi+json" \ |
| 29 | + --unix-socket "$PGRST_SERVER_UNIX_SOCKET" \ |
| 30 | + http://localhost/ \ |
| 31 | + | jq -S . > "$TMPFILE" |
| 32 | + |
| 33 | +case "$MODE" in |
| 34 | + --check) |
| 35 | + if ! diff -u "$SNAPSHOT" "$TMPFILE"; then |
| 36 | + cat >&2 <<EOF |
| 37 | +::error::OpenAPI snapshot is out of date. |
| 38 | +
|
| 39 | +To regenerate it, run from inside the project nix devShell: |
| 40 | +
|
| 41 | + postgrest-with-pg-17 --fixtures test/spec/fixtures/load.sql \\ |
| 42 | + postgrest-with-pgrst test/openapi-drift-check.sh |
| 43 | +
|
| 44 | +Then commit the updated $SNAPSHOT. |
| 45 | +EOF |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | + echo "OpenAPI snapshot is up to date." |
| 49 | + ;; |
| 50 | + write|"") |
| 51 | + mv "$TMPFILE" "$SNAPSHOT" |
| 52 | + trap - EXIT |
| 53 | + echo "Wrote $SNAPSHOT" |
| 54 | + ;; |
| 55 | + *) |
| 56 | + echo "Unknown mode: $MODE (expected --check or write)" >&2 |
| 57 | + exit 2 |
| 58 | + ;; |
| 59 | +esac |
0 commit comments