Skip to content

Use a Helm Pre-Install Hook for Database Migration

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

Status

Superseded by 0020

Context

The chart's templates/migrate-job.yaml runs the schema migration on each release. The Job name embeds .Release.Revision, which gives a fresh Job per helm upgrade and avoids patching the existing one.

We deploy this chart through two paths: helmfile in CI and Argo CD in e2e-environments. Argo CD renders via helm template, which pins .Release.Revision = 1 regardless of how many syncs have run. Every Renovate-driven image-digest bump re-renders the same migrate-1 Job, Argo tries to patch the live one, and Kubernetes rejects the patch because Job.spec.template is immutable. The Application stays OutOfSync until someone manually kubectl deletes the Job. The switchboard chart had the same pattern and was fixed first via the same approach. Under helmfile, helm-diff's dry-run also uses Revision=1 while the live Job carries the real number, so every helmfile apply shows a spurious diff and triggers a redundant helm upgrade — which CI users came to rely on as the way to re-run the migration after a deploy retry, since dev databases run on ephemeral storage and lose their schema on Postgres pod restart.

We want one design that works for both deployment paths and preserves the per-retry rerun affordance that CI relies on.

Decision

Convert the migrate Job to a pre-install,pre-upgrade Helm hook with a stable name and before-hook-creation delete policy. Co-hook the chart-managed DB Secret at weight -10 so it renders before the Job (weight -5). Add an opt-in migrate.triggerValue chart value; helmfile sets it to CI_JOB_ID, re-firing the hook on every CI retry. Argo CD consumers leave it empty — Argo Syncs already re-fire PreSync hooks on every Sync operation.

Why co-hook the Secret

Helm's install lifecycle has two strictly ordered phases: pre-install hooks first, regular release resources second. The chart's DB Secret lives in the regular phase, so a pre-install Job tries to start before its Secret exists. Promoting the Secret to a hook with a more negative weight pulls it into the same phase as the Job and orders it first.

Consequences

Positive

  • Image-digest bumps no longer wedge Argo CD.
  • Hook ordering guarantees the migration runs before the Deployment is updated; a failed migration fails the release and old Pods stay on the old schema.
  • Ephemeral-DB recovery improves: a Renovate or manual Sync re-fires the hook and recreates the schema. No manual kubectl delete dance.

Negative

  • The chart-managed Secret is recreated on every upgrade even when its content has not changed.
  • Hook-managed resources are not removed on helm uninstall. Irrelevant for our ephemeral namespaces; a footgun for other consumers.
  • Force-rerunning the migration outside CI requires helmfile sync, --set teams.migrate.triggerValue=$(date +%s), or a manual Argo Sync.

Alternatives Considered

Convert the migration to an initContainer on the Deployment. Kubernetes naturally sequences container start behind Secret availability, which sidesteps the hook lifecycle entirely. Downsides: the migration runs on every Pod restart (eviction, OOMKill, node drain), multiple replicas migrate concurrently and rely on the migration tool's locking, and failures surface as Pod CrashLoopBackOff rather than a clean Job-failed signal. Different trade-off, not strictly better.

Move the DB Secret to the surrounding setup release. Architecturally cleanest — the hook then depends only on resources outside its own release. Rejected for scope: it requires extending the e2e-environments setup chart with teams-specific knowledge and breaks the chart's standalone-install contract. Reasonable follow-up if the cost of co-hooking the Secret bites later.

References