#!/usr/bin/env bash
# dryrun.sh: run a SQL migration inside a transaction, show what it does, then roll it all back.
# usage: PGUSER=migrator ./dryrun.sh migrations/0042_reset.sql
# Postgres DDL (DROP, CREATE, ALTER) is transactional, so nothing sticks. Read the output; apply it yourself.
set -euo pipefail
f="${1:?usage: dryrun.sh migration.sql}"
{
  echo "\\set ON_ERROR_STOP on"
  echo "BEGIN;"
  echo "\\echo '--- dry run of $f (will roll back) ---'"
  echo "\\i $f"
  echo "\\echo '--- rolled back: nothing was changed ---'"
  echo "ROLLBACK;"
} | psql -X --echo-queries
