run-postgres-tests.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/bin/bash
  2. # Meson test wrapper for the live PostgreSQL integration suite.
  3. #
  4. # Skips (exit 77) when no server is reachable, so default `meson test`
  5. # stays green on machines without PostgreSQL. When PG_TEST_FORCE=1 the
  6. # ensure script's failure to bring a server up is a hard error instead.
  7. set -u
  8. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  9. ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
  10. if [ "${PG_TEST_FORCE:-0}" = "1" ]; then
  11. "$SCRIPT_DIR/ensure-postgres.sh" || exit 1
  12. else
  13. "$SCRIPT_DIR/ensure-postgres.sh" || {
  14. echo "# PostgreSQL integration tests: server unavailable, skipping"
  15. exit 77
  16. }
  17. fi
  18. export PG_TEST_CONN="${PG_TEST_CONN:-postgresql://postgres@/sqltest?host=/tmp/pgrun&port=5433}"
  19. # Best-effort cleanup of leftover isolation schemas (t_<seed>_<n>) created
  20. # by the dual-backend ORM/migration/projection suites. Harmless when none
  21. # exist or when psql is unavailable.
  22. if command -v psql >/dev/null 2>&1; then
  23. # Distinct dollar-quote tags are required: nesting the same bare $$ tag
  24. # inside the DO body terminates the outer string early and the statement
  25. # silently never runs. The regex matches the t_<n> names TestBackendContext
  26. # actually creates (t_1, t_2, ...).
  27. psql -h "${PGHOST:-/tmp/pgrun}" -p "${PGPORT:-5433}" -U "${PGUSER:-postgres}" -d "${PGDATABASE:-sqltest}" -c 'DO $cleanup$ DECLARE r record; BEGIN FOR r IN SELECT nspname FROM pg_namespace WHERE nspname ~ $regex$^t_[0-9]+$$regex$ LOOP EXECUTE format($drop$DROP SCHEMA IF EXISTS %I CASCADE$drop$, r.nspname); END LOOP; END $cleanup$;' >/dev/null 2>&1 || true
  28. fi
  29. # Meson may pass the test executable as a build-dir-relative path; the
  30. # wrapper itself is invoked from the build directory, so resolve every
  31. # argument that points at an existing file to an absolute path before
  32. # exec'ing. Keeps manual invocation (absolute or $PATH) working too.
  33. args=()
  34. for a in "$@"; do
  35. if [ ! -z "$a" ] && [ ! -f "$a" ]; then
  36. if [ -f "$ROOT_DIR/$a" ]; then
  37. a="$ROOT_DIR/$a"
  38. elif [ -f "$ROOT_DIR/build/$a" ]; then
  39. a="$ROOT_DIR/build/$a"
  40. fi
  41. fi
  42. args+=("$a")
  43. done
  44. exec "${args[@]}"