apiVersion: batch/v1
kind: Job
metadata:
  # generateName: apply-gitops-service-db-schema-
  name: apply-gitops-service-db-schema
  namespace: gitops
#  annotations:
#    argocd.argoproj.io/hook: PreSync
#    argocd.argoproj.io/hook-delete-policy: HookSucceeded
spec:
  ttlSecondsAfterFinished: 3600

  template:
    spec:
      volumes:
        - name: dbschema-volume
          configMap:
            name: gitops-service-dbschema
      containers:
      - name: psql
        image: docker.io/bitnami/postgresql:11.14.0-debian-10-r2
        command: ["/bin/bash","-c","PGPASSWORD=$POSTGRES_PASSWORD psql $POSTGRES_DB -U $POSTGRES_USER -q -f /etc/db-schema/db-schema.sql"]
        volumeMounts:
          - name: dbschema-volume
            mountPath: "/etc/db-schema"
            readOnly: true

        env:
          - name: POSTGRES_DB
            value: postgresql://gitops-postgresql-staging:5432/postgres
          - name: POSTGRESQL_PORT_NUMBER
            value: "5432"
          - name: POSTGRESQL_VOLUME_DIR
            value: /bitnami/postgresql
          - name: PGDATA
            value: /bitnami/postgresql/data
          - name: POSTGRES_USER
            value: postgres
          - name: POSTGRES_PASSWORD
            valueFrom:
              secretKeyRef:
                key: postgresql-password
                name: gitops-postgresql-staging
        
#        envFrom:
#          - configMapRef:
#            name: postgres-config
      restartPolicy: Never

