apiVersion: batch/v1
kind: Job
metadata:
  name: {{ .Release.Name }}-db-migration-{{ .Release.Revision }}
  namespace: {{ .Values.global.namespace }}
  labels:
    app: {{ .Release.Name }}-db-migration
    component: migration
  annotations:
    "helm.sh/hook": post-upgrade,post-install
    "helm.sh/hook-weight": "5"
    "helm.sh/hook-delete-policy": before-hook-creation
spec:
  ttlSecondsAfterFinished: 300
  template:
    metadata:
      labels:
        app: {{ .Release.Name }}-db-migration
    spec:
      restartPolicy: Never
      containers:
        - name: migrate
          image: postgres:16-alpine
          command:
            - /bin/sh
            - -c
            - |
              echo "Waiting for database to be ready..."
              until pg_isready -h $DB_HOST -U $DB_USER; do
                echo "Database not ready, waiting..."
                sleep 2
              done
              echo "Database is ready, applying schema..."
              PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME -f /schema/schema.sql
              echo "Schema applied, running migrations..."
              PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME -f /migration/migrate.sql
              echo "Migration completed successfully"
          env:
            - name: DB_HOST
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_HOST
            - name: DB_PORT
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_PORT
            - name: DB_NAME
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_NAME
            - name: DB_USER
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_USER
            - name: DB_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: {{ .Release.Name }}-db-credentials
                  key: DB_PASSWORD
          volumeMounts:
            - name: migration-script
              mountPath: /migration
            - name: schema-script
              mountPath: /schema
      volumes:
        - name: migration-script
          configMap:
            name: {{ .Release.Name }}-db-migration
        - name: schema-script
          configMap:
            name: {{ .Release.Name }}-db-schema
