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 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 <previous ref>: 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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user