{{- if .Values.migration.enabled }}
---
apiVersion: batch/v1
kind: Job
metadata:
  name: {{ include "formbricks.name" . }}-migration
  labels:
    {{- include "formbricks.labels" . | nindent 4 }}
  annotations:
    # ArgoCD sync hooks
    argocd.argoproj.io/hook: PreSync
    argocd.argoproj.io/hook-delete-policy: HookSucceeded
    argocd.argoproj.io/sync-wave: "-1"
    {{- if .Values.migration.annotations }}
    {{- toYaml .Values.migration.annotations | nindent 4 }}
    {{- end }}
spec:
  ttlSecondsAfterFinished: {{ .Values.migration.ttlSecondsAfterFinished | default 300 }}
  backoffLimit: {{ .Values.migration.backoffLimit | default 3 }}
  template:
    metadata:
      labels:
        {{- include "formbricks.selectorLabels" . | nindent 8 }}
        app.kubernetes.io/component: migration
    spec:
      restartPolicy: Never
      {{- if .Values.deployment.nodeSelector }}
      nodeSelector:
        {{- toYaml .Values.deployment.nodeSelector | nindent 8 }}
      {{- end }}
      {{- if .Values.deployment.tolerations }}
      tolerations:
        {{- toYaml .Values.deployment.tolerations | nindent 8 }}
      {{- end }}
      {{- if .Values.deployment.imagePullSecrets }}
      imagePullSecrets:
        {{- toYaml .Values.deployment.imagePullSecrets | nindent 8 }}
      {{- end }}
      {{- if .Values.rbac.serviceAccount.enabled }}
      serviceAccountName: {{ .Values.rbac.serviceAccount.name | default (include "formbricks.name" .) }}
      {{- end }}
      {{- if .Values.deployment.securityContext }}
      securityContext:
        {{- toYaml .Values.deployment.securityContext | nindent 8 }}
      {{- end }}
      containers:
        - name: migration
          image: {{ .Values.deployment.image.repository }}:{{ .Values.deployment.image.tag | default .Chart.AppVersion | default "latest" }}
          imagePullPolicy: {{ .Values.deployment.image.pullPolicy }}
          command:
            - node
            - packages/database/dist/scripts/apply-migrations.js
          {{- if or .Values.deployment.envFrom (or (and .Values.externalSecret.enabled (index .Values.externalSecret.files "app-secrets")) .Values.secret.enabled) }}
          envFrom:
          {{- if or .Values.secret.enabled (and .Values.externalSecret.enabled (index .Values.externalSecret.files "app-secrets")) }}
          - secretRef:
              name: {{ template "formbricks.name" . }}-app-secrets
          {{- end }}
          {{- range $value := .Values.deployment.envFrom }}
          {{- if (eq .type "configmap") }}
          - configMapRef:
              {{- if .name }}
              name: {{ include "formbricks.tplvalues.render" ( dict "value" $value.name "context" $ ) }}
              {{- else if .nameSuffix }}
              name: {{ template "formbricks.name" $ }}-{{ include "formbricks.tplvalues.render" ( dict "value" $value.nameSuffix "context" $ ) }}
              {{- else }}
              name: {{ template "formbricks.name" $ }}
              {{- end }}
          {{- end }}
          {{- if (eq .type "secret") }}
          - secretRef:
              {{- if .name }}
              name: {{ include "formbricks.tplvalues.render" ( dict "value" $value.name "context" $ ) }}
              {{- else if .nameSuffix }}
              name: {{ template "formbricks.name" $ }}-{{ include "formbricks.tplvalues.render" ( dict "value" $value.nameSuffix "context" $ ) }}
              {{- else }}
              name: {{ template "formbricks.name" $ }}
              {{- end }}
          {{- end }}
          {{- end }}
          {{- end }}
          env:
            {{- range $key, $value := .Values.deployment.env }}
            - name: {{ include "formbricks.tplvalues.render" ( dict "value" $key "context" $ ) }}
              {{- if kindIs "string" $value }}
              value: {{ include "formbricks.tplvalues.render" ( dict "value" $value "context" $ ) | quote }}
              {{- else }}
              {{- toYaml $value | nindent 14 }}
              {{- end }}
            {{- end }}
          {{- if .Values.migration.resources }}
          resources:
            {{- toYaml .Values.migration.resources | nindent 12 }}
          {{- end }}
{{- end }}
