#!/bin/bash # Meson test wrapper for the live PostgreSQL integration suite. # # Skips (exit 77) when no server is reachable, so default `meson test` # stays green on machines without PostgreSQL. When PG_TEST_FORCE=1 the # ensure script's failure to bring a server up is a hard error instead. set -u SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)" if [ "${PG_TEST_FORCE:-0}" = "1" ]; then "$SCRIPT_DIR/ensure-postgres.sh" || exit 1 else "$SCRIPT_DIR/ensure-postgres.sh" || { echo "# PostgreSQL integration tests: server unavailable, skipping" exit 77 } fi export PG_TEST_CONN="${PG_TEST_CONN:-postgresql://postgres@/sqltest?host=/tmp/pgrun&port=5433}" # Best-effort cleanup of leftover isolation schemas (t__) created # by the dual-backend ORM/migration/projection suites. Harmless when none # exist or when psql is unavailable. if command -v psql >/dev/null 2>&1; then # Distinct dollar-quote tags are required: nesting the same bare $$ tag # inside the DO body terminates the outer string early and the statement # silently never runs. The regex matches the t_ names TestBackendContext # actually creates (t_1, t_2, ...). 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 fi # Meson may pass the test executable as a build-dir-relative path; the # wrapper itself is invoked from the build directory, so resolve every # argument that points at an existing file to an absolute path before # exec'ing. Keeps manual invocation (absolute or $PATH) working too. args=() for a in "$@"; do if [ ! -z "$a" ] && [ ! -f "$a" ]; then if [ -f "$ROOT_DIR/$a" ]; then a="$ROOT_DIR/$a" elif [ -f "$ROOT_DIR/build/$a" ]; then a="$ROOT_DIR/build/$a" fi fi args+=("$a") done exec "${args[@]}"