feat(deploy): run migrations as part of the release, and report schema state
All checks were successful
CI Smoke / host-smoke (push) Successful in 0s
CI Smoke / container-smoke (push) Successful in 1s
Build and Publish Multi-Context Image / build-and-push (push) Successful in 27s

Central was serving two revisions behind the code it shipped: review_contracts
did not exist there although its migration was inside the running image. There
was no migration mechanism at all — bare uvicorn CMD, nothing chart-declared —
and nothing surfaced the mismatch. The API starts happily against a schema it
was not built for and only fails when a request touches a missing table.

Adds a chart-managed Helm pre-install/pre-upgrade hook running alembic upgrade
head, weighted to complete before the API rolls. A hook rather than an init
container: init containers run per pod, so more than one replica means
concurrent alembic upgrade with no locking. Failed jobs are deliberately
retained — a migration that fails and vanishes is how this drifted in the first
place.

/state/health now reports applied and expected revisions. "unknown" is
deliberately not "ok": an instance that cannot establish agreement must not
claim it, the same principle as instance_role defaulting to unknown.

Refs STATE-WP-0083-T07

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

Assistant: claude-code
Assistant-Model: opus
Assistant-Process: 2583210@bnt-lap001
Assistant-Session: f2bff2d5-e9b2-4338-92ca-10282a927006
This commit is contained in:
tegwick 2026-08-26 00:00:09 +02:00
parent 89ff2b2ea3
commit 97c8762a71
5 changed files with 178 additions and 0 deletions

View file

@ -0,0 +1,40 @@
{{- if .Values.migrations.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
name: {{ include "statehub.fullname" . }}-migrate
labels: {{- include "statehub.labels" . | nindent 4 }}
annotations:
# Run before the API starts serving, and before an upgrade swaps the image.
# A deployment that can serve against a schema it was not built for is the
# same class of defect as a projection that cannot name its source commit
# (STATE-WP-0083-T07).
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "-5"
# Keep a failed job for inspection; a silent migration failure is how the
# schema drifted two revisions behind the code in the first place.
"helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
backoffLimit: {{ .Values.migrations.backoffLimit }}
template:
metadata:
labels: {{- include "statehub.labels" . | nindent 8 }}
spec:
restartPolicy: Never
{{- with .Values.imagePullSecrets }}
imagePullSecrets: {{- toYaml . | nindent 8 }}
{{- end }}
containers:
- name: migrate
image: {{ include "statehub.image" . | quote }}
imagePullPolicy: {{ .Values.image.pullPolicy }}
command: ["/app/.venv/bin/python", "-m", "alembic", "upgrade", "head"]
envFrom:
{{- if .Values.config.enabled }}
- configMapRef:
name: {{ .Values.config.name | quote }}
{{- end }}
- secretRef:
name: {{ .Values.secret.name | quote }}
resources: {{- toYaml .Values.migrations.resources | nindent 12 }}
{{- end }}

View file

@ -51,6 +51,21 @@ ingress:
traefik.ingress.kubernetes.io/router.tls: "true"
cert-manager.io/cluster-issuer: letsencrypt-prod
# Database migrations (STATE-WP-0083-T07). Runs as a Helm pre-install/pre-upgrade
# hook rather than an init container: an init container runs per pod, so more
# than one replica means concurrent `alembic upgrade` with no locking. A hook
# runs once per release and fails the upgrade if the migration fails.
migrations:
enabled: true
backoffLimit: 1
resources:
requests:
cpu: 50m
memory: 256Mi
limits:
cpu: 500m
memory: 1Gi
# Classification allowed-values (CUST-WP-0067-T09). The API validates repo
# classification against the-custodian canon; a container has no such checkout,
# so the file travels with the release as a ConfigMap. Without it every