#!/bin/sh set -e # --------------------------------------------------------------------------- # 1. Print current migration status for debugging. # --------------------------------------------------------------------------- echo "[migrate] Checking database migration status..." STATUS_OUTPUT=$(bunx prisma migrate status 2>&1 || true) echo "$STATUS_OUTPUT" # --------------------------------------------------------------------------- # 2. Resolve any "Failed" migrations as rolled-back so deploy can re-apply. # All migration SQL in this repo is written to be idempotent (using # IF NOT EXISTS / DO $$ ... $$ guards), so a re-run after resolving will # succeed even when schema drift has occurred from a prior db push. # # Regex matches both named (20260307010000_add_foo) and unnamed (20260125205653) # migration timestamps so the initial baseline migration is also covered. # --------------------------------------------------------------------------- FAILED=$(printf '%s\n' "$STATUS_OUTPUT" | grep -i "failed" | grep -oE '[0-9]{14}(_[a-zA-Z_]+)?' || true) for MIGRATION in $FAILED; do echo "[migrate] Resolving failed migration: $MIGRATION" bunx prisma migrate resolve --rolled-back "$MIGRATION" done # --------------------------------------------------------------------------- # 3. Deploy all pending migrations from the migrations directory. # --------------------------------------------------------------------------- echo "[migrate] Running prisma migrate deploy..." bunx prisma migrate deploy echo "[migrate] All migrations applied successfully."