From 132672599519a9804cb2283542ace1368528a8f5 Mon Sep 17 00:00:00 2001 From: Jackson Roberts Date: Fri, 27 Feb 2026 16:26:31 -0600 Subject: [PATCH] fix: resolve failed migrations before deploying --- prisma/migrate-entrypoint.sh | 25 +++++++++++++++++++++++-- src/index.ts | 11 +++++++---- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/prisma/migrate-entrypoint.sh b/prisma/migrate-entrypoint.sh index 91ebb5b..544625a 100755 --- a/prisma/migrate-entrypoint.sh +++ b/prisma/migrate-entrypoint.sh @@ -1,7 +1,26 @@ #!/bin/sh set -e -# Generate diff SQL between current migrations and the Prisma schema +# --------------------------------------------------------------------------- +# 1. Resolve any previously failed migrations so deploy can proceed. +# Prisma marks failed migrations in _prisma_migrations; we roll them back +# so the current run can re-apply them cleanly. +# --------------------------------------------------------------------------- +echo "[migrate] Checking for failed migrations..." +FAILED=$(bunx prisma migrate status 2>&1 || true) + +# Extract failed migration names and mark them as rolled back +echo "$FAILED" | grep -oE '[0-9]{14}_[a-z_]+' | while read -r MIGRATION; do + # Only resolve if the status output says it failed + if echo "$FAILED" | grep -q "failed"; then + echo "[migrate] Resolving failed migration: $MIGRATION" + bunx prisma migrate resolve --rolled-back "$MIGRATION" 2>/dev/null || true + fi +done + +# --------------------------------------------------------------------------- +# 2. Generate diff SQL between current migrations and the Prisma schema. +# --------------------------------------------------------------------------- DIFF_SQL=$(bunx prisma migrate diff \ --from-migrations prisma/migrations \ --to-schema-datamodel prisma/schema.prisma \ @@ -18,6 +37,8 @@ else echo "[migrate] Schema and migrations are in sync — no migration needed." fi -# Deploy all pending migrations +# --------------------------------------------------------------------------- +# 3. Deploy all pending migrations. +# --------------------------------------------------------------------------- echo "[migrate] Running prisma migrate deploy..." bunx prisma migrate deploy diff --git a/src/index.ts b/src/index.ts index c9a73f2..1d650cf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -50,7 +50,10 @@ const safeStartup = async (label: string, fn: () => Promise) => { try { await fn(); } catch (err) { - console.error(`[startup] ${label} failed — will retry on next interval`, err); + console.error( + `[startup] ${label} failed — will retry on next interval`, + err, + ); } }; @@ -91,9 +94,9 @@ setInterval(() => { await safeStartup("syncSites", () => unifiSites.syncSites()); setInterval(() => { - return unifiSites.syncSites().catch((err) => - console.error("[interval] syncSites failed", err), - ); + return unifiSites + .syncSites() + .catch((err) => console.error("[interval] syncSites failed", err)); }, 60 * 1000); Bun.serve({