fix(migrate): rewrite entrypoint to resolve P3009 from deploy error output
This commit is contained in:
@@ -2,31 +2,42 @@
|
|||||||
set -e
|
set -e
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# 1. Print current migration status for debugging.
|
# Run prisma migrate deploy. On P3009 (a failed migration is blocking deploy),
|
||||||
|
# extract the migration name from the error, resolve it as rolled-back, and
|
||||||
|
# retry. All migration SQL in this repo is idempotent so a re-run is safe.
|
||||||
|
# Loop handles multiple blocked migrations; max retries prevents infinite loops.
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
echo "[migrate] Checking database migration status..."
|
MAX_RETRIES=10
|
||||||
STATUS_OUTPUT=$(bunx prisma migrate status 2>&1 || true)
|
ATTEMPT=0
|
||||||
echo "$STATUS_OUTPUT"
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
while [ $ATTEMPT -lt $MAX_RETRIES ]; do
|
||||||
# 2. Resolve any "Failed" migrations as rolled-back so deploy can re-apply.
|
ATTEMPT=$((ATTEMPT + 1))
|
||||||
# All migration SQL in this repo is written to be idempotent (using
|
echo "[migrate] Running prisma migrate deploy (attempt $ATTEMPT)..."
|
||||||
# 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
|
DEPLOY_OUTPUT=$(bunx prisma migrate deploy 2>&1)
|
||||||
echo "[migrate] Resolving failed migration: $MIGRATION"
|
EXIT_CODE=$?
|
||||||
bunx prisma migrate resolve --rolled-back "$MIGRATION"
|
echo "$DEPLOY_OUTPUT"
|
||||||
|
|
||||||
|
if [ $EXIT_CODE -eq 0 ]; then
|
||||||
|
echo "[migrate] All migrations applied successfully."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# P3009: a previously-failed migration is blocking deploy.
|
||||||
|
# The error message contains the migration name in backticks:
|
||||||
|
# The `20260402000000_fix_severity_typo` migration started at ... failed
|
||||||
|
if echo "$DEPLOY_OUTPUT" | grep -q "P3009"; then
|
||||||
|
FAILED=$(echo "$DEPLOY_OUTPUT" | grep -oE '\`[0-9]{14}(_[a-zA-Z_]+)?\`' | tr -d '\`' | head -1)
|
||||||
|
if [ -n "$FAILED" ]; then
|
||||||
|
echo "[migrate] Resolving failed migration as rolled-back: $FAILED"
|
||||||
|
bunx prisma migrate resolve --rolled-back "$FAILED"
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "[migrate] Migration failed with a non-recoverable error."
|
||||||
|
exit 1
|
||||||
done
|
done
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
echo "[migrate] Exceeded max retries ($MAX_RETRIES). Giving up."
|
||||||
# 3. Deploy all pending migrations from the migrations directory.
|
exit 1
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
echo "[migrate] Running prisma migrate deploy..."
|
|
||||||
bunx prisma migrate deploy
|
|
||||||
echo "[migrate] All migrations applied successfully."
|
|
||||||
|
|||||||
Reference in New Issue
Block a user