#!/usr/bin/env bash # # check-direct-url.sh — sanity-check the Prisma direct-connect URL before # running a migration. Prisma Migrate must talk to Postgres directly (not # through PgBouncer in transaction pool mode), so we want a hard fail # upfront if DIRECT_URL is missing or unreachable. # # Usage: # scripts/db/check-direct-url.sh # validates DIRECT_URL # scripts/db/check-direct-url.sh --skip-ping # env-presence only (CI) # # Exits non-zero on any failure so callers can short-circuit. set -euo pipefail skip_ping=0 if [[ "${1:-}" == "--skip-ping" ]]; then skip_ping=1 fi if [[ -z "${DIRECT_URL:-}" ]]; then if [[ -n "${DATABASE_URL:-}" ]]; then echo "warning: DIRECT_URL is unset; falling back to DATABASE_URL." >&2 echo " This is only safe when DATABASE_URL bypasses PgBouncer (port 5432)." >&2 echo " See docs/ops/pgbouncer.md." >&2 exit 0 fi echo "error: DIRECT_URL (or DATABASE_URL) must be set before running prisma migrate." >&2 exit 1 fi case "$DIRECT_URL" in *pgbouncer=true*) echo "error: DIRECT_URL must NOT include 'pgbouncer=true'. Prisma Migrate cannot run through PgBouncer." >&2 echo " Configure DATABASE_URL to point at the pooler instead." >&2 exit 1 ;; *":6432/"*) echo "warning: DIRECT_URL targets port 6432 (PgBouncer's port). Migrations will likely fail." >&2 echo " Expected an unpooled Postgres URL on port 5432." >&2 ;; esac if [[ "$skip_ping" -eq 1 ]]; then exit 0 fi # Prefer `prisma migrate status` because it exercises the same driver path # the migration commands use; fall back to a Node tcp ping so we don't hard- # require prisma to be installed when this script runs as a pre-deploy hook. if command -v pnpm >/dev/null 2>&1; then if pnpm exec prisma migrate status >/dev/null 2>&1; then echo "ok: prisma migrate status passed against DIRECT_URL" exit 0 fi echo "warning: prisma migrate status failed (DB may not be migrated yet)." >&2 fi echo "ok: DIRECT_URL present and well-formed"