apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "iceberg-catalog.fullname" . }}-db-migration-{{ .Release.Revision }}
  namespace: {{ .Release.Namespace }}
  labels:
    {{- include "iceberg-catalog.labels" . | nindent 4 }}
    app.kubernetes.io/component: db-migration
    {{- if .Values.catalog.dbMigrations.labels }}
    {{- toYaml .Values.catalog.dbMigrations.labels | nindent 4 }}
    {{- end }}
  annotations:
    argocd.argoproj.io/hook: Sync
    argocd.argoproj.io/sync-wave: "0"
    {{- if not .Values.helmWait }}
    helm.sh/hook: post-install,post-upgrade
    helm.sh/hook-weight: "-100"
    helm.sh/hook-delete-policy: before-hook-creation
    {{- else }}
    helm-hook-enabled: "false"
    {{- end }}
    {{- if .Values.catalog.dbMigrations.annotations }}
    {{- toYaml .Values.catalog.dbMigrations.annotations | nindent 4 }}
    {{- end }}
spec:
  template:
    metadata:
      name: "{{ include "iceberg-catalog.fullname" . }}-migration"
      annotations:
        checksum/secret-config-envs: {{ include (print $.Template.BasePath "/config/secret-config-envs.yaml") . | sha256sum }}
        {{- if .Values.catalog.dbMigrations.podAnnotations -}}
        {{- toYaml .Values.catalog.dbMigrations.podAnnotations | nindent 8 }}
        {{- end }}
      labels:
        {{- include "iceberg-catalog.labels" . | nindent 8 }}
        app.kubernetes.io/component: db-migration
        {{- if .Values.catalog.dbMigrations.podLabels }}
        {{- toYaml .Values.catalog.dbMigrations.podLabels | nindent 8 }}
        {{- end }}
    spec:
      restartPolicy: OnFailure
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      {{- if .Values.catalog.nodeSelector }}
      nodeSelector:
        {{- toYaml .Values.catalog.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.catalog.affinity }}
      affinity:
        {{- toYaml .Values.catalog.affinity | nindent 8 }}
      {{- end }}
      {{- if .Values.catalog.tolerations }}
      tolerations:
        {{- toYaml .Values.catalog.tolerations | nindent 8 }}
      {{- end }}
      {{- if .Values.catalog.priorityClassName }}
      priorityClassName: {{ .Values.catalog.priorityClassName | quote }}
      {{- end }}
      serviceAccountName: {{ include "iceberg-catalog.serviceAccountName" . }}
      {{- if .Values.catalog.podSecurityContext }}
      securityContext:
        {{- toYaml .Values.catalog.podSecurityContext | nindent 8 }}
      {{- end }}
      initContainers:
        {{- if .Values.catalog.dbMigrations.extraInitContainers }}
        {{- toYaml .Values.catalog.dbMigrations.extraInitContainers | nindent 8 }}
        {{- end }}
        {{- include "iceberg-catalog.init_container.check_db" (dict "Release" .Release "Values" .Values "Chart" .Chart "awaitMigration" false) | indent 8 }}
      containers:
        - name: migration
          {{- include "iceberg-catalog.image" . | indent 10 }}
          env:
            {{- include "iceberg-catalog.env" . | indent 12 }}
          envFrom:
            {{- include "iceberg-catalog.envFrom" . | indent 12 }}
            {{- if .Values.catalog.extraEnvFrom -}}
            {{- tpl (toYaml .Values.catalog.extraEnvFrom) . | nindent 12 }}
            {{- end }}
          {{- if .Values.catalog.command }}
          command:
            {{- toYaml .Values.catalog.command | nindent 12 }}
          {{- end }}
          args:
            - migrate
          resources:
            {{- toYaml .Values.catalog.dbMigrations.resources | nindent 12 }}
          {{- with .Values.catalog.extraVolumeMounts }}
          volumeMounts:
            {{- toYaml . | nindent 12 }}
          {{- end }}
      {{- with .Values.catalog.extraVolumes }}
      volumes:
        {{- toYaml . | nindent 8 }}
      {{- end }}
