#!/usr/bin/env bash # # test/reset-test-db.sh — return the test databases to a pristine state. # # Drops and recreates every database the Go test suites use, then migrates # each one, so a run starts from exactly what the migration streams create. # The application's own database (MC_DB_DSN) is never touched: it belongs to # whatever `member-console start` is running, and keeping the two apart is # the point of this script. # # Two test databases, not one: package tests use TEST_DATABASE_URL, while the # in-process e2e suite (test/e2e/plan-management) uses E2E_DATABASE_URL, # because it commits org-type defaults that package tests assert the absence # of. The browser walkthroughs (test/e2e/operator-walkthroughs) drive the live # app and stay on the app database. # # Migrating here is load-bearing, not a convenience: each test package calls # db.RunMigrations itself, and goose takes no advisory lock, so a genuinely # empty database would have a dozen parallel packages racing to create the # same schemas. Pre-migrating leaves every one of those calls a no-op. # # Usage: ./test/reset-test-db.sh (or `make test`, which runs it first) set -euo pipefail here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" repo_root="$(cd "$here/.." && pwd)" env_file="$here/.env" if [[ ! -f "$env_file" ]]; then echo "error: $env_file not found. Run ./bootstrap-stack.sh first." >&2 exit 1 fi set -a # shellcheck source=/dev/null source "$env_file" set +a # An .env generated before this script existed carries none of these keys. # Fail loudly rather than falling back to a default, which would mean # resetting the application's database. for var in POSTGRES_ADMIN_DSN TEST_DATABASE_URL E2E_DATABASE_URL; do if [[ -z "${!var:-}" ]]; then cat >&2 <}' — that is the application database." >&2 exit 1 fi echo "Resetting ${label} database '${name}'..." # WITH (FORCE) terminates sessions still attached from a previous run; # without it a leftover connection makes the DROP wait indefinitely. psql "$POSTGRES_ADMIN_DSN" -v ON_ERROR_STOP=1 --quiet </dev/null) } reset_one "$TEST_DATABASE_URL" "package-test" reset_one "$E2E_DATABASE_URL" "e2e" echo "Test databases are pristine. The application database is untouched."