Skip to content

Run database migration via a plain Job with a content-rotated name

  • Deciders: Maik Schäfer, Julian Bäume
  • Date: 2026-05-20

Status

Accepted (supersedes 0019)

Context

The migrate Job was previously a pre-install,pre-upgrade Helm hook, with the chart-managed DB Secret co-hooked at weight -10 so the Job could mount it. In production this design caused upgrades to delete the Secret on the regular→hook transition, recreate it at the start of every subsequent upgrade (any Pod restarting in the gap fails to mount), and dropped both resources out of helm diff / drift detection. A post-* revert wasn't viable either: ops deploys with --atomic --wait, the Deployment must become Ready before any post-hook fires, and Pods can't become Ready until the migration runs — so the post-hook never gets to run.

Tying the Job name to .Release.Revision was considered but rejected: under Argo CD helm template pins it to 1, so the suffix would never rotate between syncs and Argo would try to patch the immutable Job.spec.template.

Decision

Use a plain regular-resource Job. Rotate its metadata.name via a sha256-truncated suffix derived from migration.jobId (caller-provided), or fall back to .Release.Revision when unset. Drop hook annotations from the DB Secret.

{{- $ident := .Values.migration.jobId | default (.Release.Revision | toString) }}
{{- $suffix := $ident | toString | sha256sum | trunc 8 }}
name: {{ printf "%s-migrate-%s" (include "ox-common.names.fullname" .) $suffix | trunc 63 | trimSuffix "-" }}

CI sets migration.jobId to CI_JOB_ID so a pipeline retry re-runs the migration even when nothing else has changed (dev databases run on ephemeral storage and lose schema on Pod restart). Argo callers set it explicitly, since Release.Revision is pinned to 1 under helm template (see Context).

Consequences

Positive

  • No regular↔hook transitions; helm diff and drift detection see the Secret and Job again.
  • --atomic --wait is consistent: Helm waits for both Deployment readiness and Job Complete; a failed migration triggers --atomic rollback.
  • Single code path for first install and upgrades.

Negative

  • The Job and Deployment apply concurrently, so new Pods briefly CrashLoopBackOff against checkPendingMigrations until the Job finishes. Old Pods continue serving via the rolling update; no user-visible impact.
  • Migration failures surface after the Deployment is applied (vs. the pre-hook design which failed before touching it). --atomic recovers cleanly; mid-deploy operators briefly see a degraded Deployment.

Alternatives Considered

  • resource-policy: keep bridge release to repair the regular→hook transition: leaves the recreation window, drift blindness, and --atomic deadlock untouched.
  • Asymmetric pre-upgrade + post-install: works for upgrades under --atomic, deadlocks on first install for the same reason post-* does.
  • Init container that polls for migrations: pays per-Pod-lifecycle tax on every restart for the chart's lifetime, interacts poorly with Istio (init runs before istio-proxy is ready). Trades a transient deploy symptom for permanent infrastructure.
  • image.tag as an intermediate fallback for the suffix: stable tags would freeze it and block Release.Revision from rotating; volatile tags rotate via Release.Revision anyway. No gain over the two-tier scheme.

References

  • Cross-chart counterpart: switchboard ADR 0008, core-ui MR !6551
  • Superseded ADR: 0019
  • Superseded commit: 727d323