{{- if .Values.migrate.enabled }}
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "groombook.fullname" . }}-migrate
  labels:
    {{- include "groombook.labels" . | nindent 4 }}
    app.kubernetes.io/component: migrate
  annotations:
    "helm.sh/hook": pre-install,pre-upgrade
    "helm.sh/hook-weight": "1"
    "helm.sh/hook-delete-policy": before-hook-creation,hook-succeeded
spec:
  backoffLimit: {{ .Values.migrate.backoffLimit }}
  template:
    metadata:
      labels:
        {{- include "groombook.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: migrate
    spec:
      {{- with .Values.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml . | nindent 8 }}
      {{- end }}
      restartPolicy: OnFailure
      containers:
        - name: migrate
          image: {{ include "groombook.migrateImage" . }}
          imagePullPolicy: {{ .Values.migrate.image.pullPolicy }}
          command: ["pnpm", "db:migrate"]
          env:
            - name: DATABASE_URL
              valueFrom:
                secretKeyRef:
                  name: {{ include "groombook.databaseSecretName" . }}
                  key: {{ include "groombook.databaseSecretKey" . }}
{{- end }}

