#!/usr/bin/env bash # Run a SQL migration against the main DATABASE_URL from repo .env. # # Usage (from anywhere): # ./scripts/run-psql-migration.sh migrations/add-photos-web-playback-columns.sql # # Strips SQLAlchemy driver suffixes (+psycopg2, +asyncpg) so psql accepts the URL. # For remote Postgres, set DATABASE_URL in .env to postgresql://user:pass@host:port/dbname set -euo pipefail ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$ROOT" if [[ ! -f .env ]]; then echo "ERROR: .env not found at $ROOT/.env" >&2 exit 1 fi set -a # shellcheck disable=SC1091 source .env set +a if [[ -z "${DATABASE_URL:-}" ]]; then echo "ERROR: DATABASE_URL is not set in .env" >&2 exit 1 fi SQL_FILE="${1:?Usage: $0 path/to/migration.sql}" if [[ ! -f "$SQL_FILE" ]]; then echo "ERROR: SQL file not found: $SQL_FILE" >&2 exit 1 fi URL="$DATABASE_URL" URL="${URL//+psycopg2/}" URL="${URL//+asyncpg/}" exec psql "$URL" -f "$SQL_FILE"