#!/usr/bin/env bash
# Use when `npm run db:migrate` fails with P3005 (DB has tables but no migrate history, e.g. after db push).
# Marks historical migrations as already applied, then runs `migrate deploy` to apply pending ones (e.g. organizer).
set -euo pipefail
cd "$(dirname "$0")/.."

for migration_name in \
  20260331051150_add_user_username \
  20260331053518_add_meetup_visibility \
  20260331061812_add_user_username
do
  echo "Marking as applied: $migration_name"
  # Ignore failures when already recorded or already baselined
  dotenv -e ../.env -e .env -- prisma migrate resolve --applied "$migration_name" || true
done

echo "Applying any pending migrations..."
dotenv -e ../.env -e .env -- prisma migrate deploy
