name: CI

on:
  push:
  pull_request:
  workflow_dispatch:

# Keep the latest run on a branch and drop the ones it superseded. The default
# branch is exempt so every commit there keeps its own result.
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/master' }}

env:
  CARGO_TERM_COLOR: always
  PGRX_VERSION: 0.19.3

jobs:
  # No Postgres needed, so this fails in about a minute when the logic breaks.
  core:
    name: core (fmt, clippy, unit tests)
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo fmt --all --check
      - run: cargo clippy -p pg_anyfk_core --all-targets -- -D warnings
      - run: cargo test -p pg_anyfk_core

  # Four Postgres versions is the expensive half, so it waits for the cheap gate.
  # A formatting slip should not burn four matrix jobs.
  ext:
    name: ext (pg${{ matrix.pg }})
    runs-on: ubuntu-latest
    needs: core
    strategy:
      fail-fast: false
      matrix:
        pg: [15, 16, 17, 18]
    steps:
      - uses: actions/checkout@v4

      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Install Postgres ${{ matrix.pg }} from PGDG
        run: |
          sudo apt-get update
          sudo apt-get install -y curl ca-certificates gnupg lsb-release
          curl -fsSL https://www.postgresql.org/media/keys/ACCC4CF8.asc \
            | sudo gpg --dearmor -o /usr/share/keyrings/pgdg.gpg
          echo "deb [signed-by=/usr/share/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" \
            | sudo tee /etc/apt/sources.list.d/pgdg.list
          sudo apt-get update
          sudo apt-get install -y \
            postgresql-${{ matrix.pg }} \
            postgresql-server-dev-${{ matrix.pg }} \
            build-essential libreadline-dev zlib1g-dev flex bison \
            libssl-dev libclang-dev clang pkg-config libicu-dev

      - name: Let the runner user install the extension
        run: |
          PG_CONFIG=/usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config
          sudo chmod a+w "$($PG_CONFIG --pkglibdir)"
          sudo chmod a+w "$($PG_CONFIG --sharedir)/extension"

      - uses: Swatinem/rust-cache@v2

      # Building cargo-pgrx from source is by far the slowest step, and the binary
      # only changes when PGRX_VERSION does.
      - name: Cache cargo-pgrx
        id: pgrx-bin
        uses: actions/cache@v4
        with:
          path: ~/.cargo/bin/cargo-pgrx
          key: cargo-pgrx-${{ env.PGRX_VERSION }}-${{ runner.os }}

      - name: Install cargo-pgrx
        if: steps.pgrx-bin.outputs.cache-hit != 'true'
        run: cargo install --locked cargo-pgrx@${{ env.PGRX_VERSION }}

      - name: cargo pgrx init
        run: |
          cargo pgrx init \
            --pg${{ matrix.pg }} /usr/lib/postgresql/${{ matrix.pg }}/bin/pg_config

      - name: clippy
        working-directory: ext
        run: |
          cargo clippy --all-targets \
            --no-default-features --features pg${{ matrix.pg }} -- -D warnings

      - name: cargo pgrx test
        working-directory: ext
        run: cargo pgrx test pg${{ matrix.pg }}

      # Proving the parent row lock works needs two sessions at once, which
      # #[pg_test] cannot provide.
      - name: concurrency tests
        run: ./scripts/race_test.sh ${{ matrix.pg }}
