From 2479cbbd0adbab9b52df8e7d421bcbeb80ffd73f Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Tue, 17 Nov 2026 15:30:18 +0200 Subject: [PATCH] deploy: after a failed migration, mark it rolled back so later deploys aren't blocked Found in the VM test: Prisma records a failed migration and then refuses every later migrate deploy (P3009), so one broken release left a church unable to update. On rollback, failed migrations the previous release doesn't have are marked rolled back (prisma migrate resolve); the backup still has the database as it was. Co-Authored-By: Claude Opus 5.5 --- scripts/lib/deploy.sh | 6 ++++++ scripts/products/events.sh | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/scripts/lib/deploy.sh b/scripts/lib/deploy.sh index d4d6cd3..6569756 100644 --- a/scripts/lib/deploy.sh +++ b/scripts/lib/deploy.sh @@ -303,6 +303,12 @@ deploy_product() { return 0 fi warn "$p: deploy failed. Going back to $prev (database migrations aren't undone; the backup has the database as it was)." + # A migration that failed is recorded as failed, and Prisma then refuses every later deploy + # (P3009) until it's resolved. Mark this release's failed migrations as rolled back, while its + # checkout is still in place (found in the VM test). + if declare -F "${p}_resolve_failed_migrations" >/dev/null; then + "${p}_resolve_failed_migrations" "$dir" "$user" "$prev" || warn "$p: couldn't mark the failed migration as rolled back; the next deploy will say so." + fi set +e ( set -e diff --git a/scripts/products/events.sh b/scripts/products/events.sh index c944051..9c7c144 100644 --- a/scripts/products/events.sh +++ b/scripts/products/events.sh @@ -114,3 +114,31 @@ events_keep() { events_env_vars() { printf '%s\n' --var "domain=$(pvar events DOMAIN)" --var "apiPort=$(events_api_port)" --var "webPort=$(events_web_port)" --var "church=$(cenv CHURCH)" } + +# After a failed deploy, before going back to : migrations Prisma recorded as failed +# that the previous release doesn't have are marked as rolled back, so the next deploy isn't +# blocked (P3009). The database itself isn't changed: the backup taken first has it as it was. +events_resolve_failed_migrations() { + local dir="$1" user="$2" prev="$3" backend name + backend="$(events_app "$dir")/backend" + local -a failed + # shellcheck disable=SC2016 # JavaScript, not shell + mapfile -t failed < <(run_as "$user" "$backend" node -e ' + require("dotenv").config(); + const { Client } = require("pg"); + const c = new Client({ connectionString: process.env.DATABASE_URL }); + c.connect() + .then(() => c.query("select migration_name from _prisma_migrations where finished_at is null and rolled_back_at is null")) + .then(r => { r.rows.forEach(x => console.log(x.migration_name)); return c.end(); }) + .catch(e => { console.error(e.message); process.exit(1); });') + for name in "${failed[@]}"; do + [ -n "$name" ] || continue + if git_in "$dir" cat-file -e "$prev:apps/events/backend/prisma/migrations/$name/migration.sql" 2>/dev/null; then + warn "events: $name failed but $prev has it too; left for a look (npx prisma migrate status)." + continue + fi + run_as "$user" "$backend" npx prisma migrate resolve --rolled-back "$name" >/dev/null + info "events: marked the failed migration $name as rolled back." + report_result "resolvedMigration" "$name" + done +}