run-postgres-tests.sh 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. psql -h "${PGHOST:-/tmp/pgrun}" -p "${PGPORT:-5433}" -U "${PGUSER:-postgres}" -d "${PGDATABASE:-sqltest}" -c 'DROP SCHEMA IF EXISTS t_schema_cleanup_dummy CASCADE; DO $$ DECLARE r record; BEGIN FOR r IN SELECT nspname FROM pg_namespace WHERE nspname ~ $$^t_[0-9]+_[0-9]+$$ LOOP EXECUTE $$DROP SCHEMA IF EXISTS $$ || quote_ident(r.nspname) || $$ CASCADE$$; END LOOP; END $$;' >/dev/null 2>&1 || true
  24. fi
  25. # Meson may pass the test executable as a build-dir-relative path; the
  26. # wrapper itself is invoked from the build directory, so resolve every
  27. # argument that points at an existing file to an absolute path before
  28. # exec'ing. Keeps manual invocation (absolute or $PATH) working too.
  29. args=()
  30. for a in "$@"; do
  31. if [ ! -z "$a" ] && [ ! -f "$a" ]; then
  32. if [ -f "$ROOT_DIR/$a" ]; then
  33. a="$ROOT_DIR/$a"
  34. elif [ -f "$ROOT_DIR/build/$a" ]; then
  35. a="$ROOT_DIR/build/$a"
  36. fi
  37. fi
  38. args+=("$a")
  39. done
  40. exec "${args[@]}"