apiVersion: argoproj.io/v1alpha1
kind: WorkflowTemplate
metadata:
  name: postgres-query
  labels:
    dagweave.io/catalog-item: postgres-query
  annotations:
    # The catalog semver this manifest was authored at. The dagweave app records
    # (catalog-item-id, semver) -> content-hash -> base-vN when it mints the tenant
    # cluster name via ResolveOrMint. Keep in sync with metadata.yaml `version`.
    dagweave.io/catalog-version: "1.0.0"
    dagweave.io/catalog-category: "Databases"
spec:
  entrypoint: query
  templates:
    - name: query
      inputs:
        parameters:
          # The SQL query to run.
          - name: query
          # PostgreSQL server host.
          - name: host
          # PostgreSQL server port.
          - name: port
            default: "5432"
          # Database name to connect to.
          - name: database
          # User to authenticate as.
          - name: user
          # Name of the Kubernetes Secret holding the user's password. Defaults let a
          # user leave them blank in the Inspector.
          - name: password-secret-name
            default: postgres-password
          - name: password-secret-key
            default: password
      container:
        # Pinned by digest per the catalog convention: an image rebuild can never
        # silently change the behaviour of an already-released item version.
        # postgres:19-alpine
        image: postgres@sha256:8b0ff4cdf221f0c53d7fcda681d3b461be725c492cd881c5ec9c0ad25023f188
        command: [sh, -c]
        # The connection values and the query are passed as env vars (below), never
        # interpolated into the shell, so a query containing quotes or shell
        # metacharacters is safe. psql reads PGHOST/PGPORT/PGDATABASE/PGUSER/PGPASSWORD
        # from the environment. ON_ERROR_STOP makes a bad query fail the step.
        args:
          - |
            set -eu
            psql -v ON_ERROR_STOP=1 -c "$QUERY"
        env:
          - name: PGHOST
            value: "{{inputs.parameters.host}}"
          - name: PGPORT
            value: "{{inputs.parameters.port}}"
          - name: PGDATABASE
            value: "{{inputs.parameters.database}}"
          - name: PGUSER
            value: "{{inputs.parameters.user}}"
          - name: QUERY
            value: "{{inputs.parameters.query}}"
          - name: PGPASSWORD
            valueFrom:
              secretKeyRef:
                name: "{{inputs.parameters.password-secret-name}}"
                key: "{{inputs.parameters.password-secret-key}}"
