name: CI

on:
  push:
    branches: [master]
  pull_request:
  workflow_dispatch:

jobs:
  regress:
    name: PostgreSQL ${{ matrix.pg }}
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        pg: [13, 14, 15, 16, 17, 18]

    steps:
      - uses: actions/checkout@v4

      - name: Install PostgreSQL ${{ matrix.pg }} (PGDG)
        run: |
          sudo apt-get update
          sudo apt-get install -y postgresql-common
          sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y
          sudo apt-get install -y \
            postgresql-${{ matrix.pg }} \
            postgresql-server-dev-${{ matrix.pg }}
          echo "PG_CONFIG=/usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config" >> "$GITHUB_ENV"

      - name: Build and install plx
        run: |
          make PG_CONFIG="$PG_CONFIG"
          sudo make install PG_CONFIG="$PG_CONFIG"

      - name: Start the server and prepare a superuser role
        run: |
          # PGDG packages do not auto-create the cluster on the runner; create it
          # if missing (ignore if it already exists, e.g. a preinstalled version),
          # then ensure it is running.
          sudo pg_createcluster ${{ matrix.pg }} main --start 2>/dev/null || true
          sudo pg_ctlcluster ${{ matrix.pg }} main start 2>/dev/null || true
          pg_lsclusters
          # the target cluster's port (the runner may carry another cluster too)
          port=$(pg_lsclusters -h | awk '$1=="${{ matrix.pg }}" && $2=="main"{print $3}')
          test -n "$port"
          sudo -u postgres createuser --superuser --port "$port" "$USER"
          echo "PGHOST=/var/run/postgresql" >> "$GITHUB_ENV"
          echo "PGPORT=$port" >> "$GITHUB_ENV"

      # No server needed, and it guards the release process rather than the
      # code, so it runs before anything else can fail.
      - name: Check META.json against the PGXN spec
        run: make metacheck PG_CONFIG="$PG_CONFIG"

      - name: Run the regression suite
        run: make installcheck PG_CONFIG="$PG_CONFIG"

      # Runs after installcheck, which is what creates contrib_regression and
      # leaves the extension installed in it.
      - name: Run the differential check
        run: make differentialcheck PG_CONFIG="$PG_CONFIG"

      - name: Show diffs on failure
        if: failure()
        run: |
          cat test/regression.diffs || true
          sudo cat /var/log/postgresql/postgresql-${{ matrix.pg }}-main.log || true
