name: pgvectorfast
on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-24.04
    strategy:
      fail-fast: false
      matrix:
        postgres: [18, 19]
        compiler: [gcc, clang]
    env:
      CC: ${{ matrix.compiler }}
    steps:
      - uses: actions/checkout@v5
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - name: Configure environment paths
        run: |
          {
            echo "PG_PREFIX=$RUNNER_TEMP/pg-${{ matrix.postgres }}-${{ matrix.compiler }}"
            echo "PG_SRC=$RUNNER_TEMP/postgres-src-${{ matrix.postgres }}-${{ matrix.compiler }}"
            echo "PG_BUILD=$RUNNER_TEMP/postgres-build-${{ matrix.postgres }}-${{ matrix.compiler }}"
            echo "PGDATA=$RUNNER_TEMP/pgdata-${{ matrix.postgres }}-${{ matrix.compiler }}"
          } >> "$GITHUB_ENV"
      - name: Install build dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y build-essential clang bison flex pkg-config liburing-dev liblz4-dev libzstd-dev libipc-run-perl
          python3 -m pip install meson ninja
      - name: Build and install PostgreSQL ${{ matrix.postgres }}
        run: |
          if [ "${{ matrix.postgres }}" = "18" ]; then
            PG_REF="REL_18_0"
          else
            PG_REF="REL_19_BETA3"
          fi
          git clone --depth 1 --branch "$PG_REF" https://github.com/postgres/postgres.git "$PG_SRC"
          meson setup "$PG_BUILD" "$PG_SRC" \
            --prefix="$PG_PREFIX" \
            -Dbuildtype=debugoptimized \
            -Dcassert=true \
            -Doptimization=3 \
            -Dicu=disabled \
            -Dreadline=disabled \
            -Dssl=none \
            -Dllvm=disabled \
            -Dtap_tests=enabled
          ninja -C "$PG_BUILD" -j4
          meson install -C "$PG_BUILD" --no-rebuild
      - name: Build and install pgvectorfast
        run: |
          export PG_CONFIG="$PG_PREFIX/bin/pg_config"
          make PG_CONFIG="$PG_CONFIG" -j4
          make PG_CONFIG="$PG_CONFIG" install
      - name: Run examples and installcheck
        run: |
          export PATH="$PG_PREFIX/bin:$PATH"
          export PG_CONFIG="$PG_PREFIX/bin/pg_config"
          initdb -D "$PGDATA" --no-locale -E UTF8
          printf "\nlisten_addresses=''\nunix_socket_directories='/tmp'\nshared_preload_libraries='vectorfast'\n" >> "$PGDATA/postgresql.conf"
          pg_ctl -D "$PGDATA" -l "$RUNNER_TEMP/postgres.log" -w start
          trap 'pg_ctl -D "$PGDATA" -m fast -w stop || true' EXIT
          psql -h /tmp -d postgres -v ON_ERROR_STOP=1 -f examples/getting-started.sql
          PGHOST=/tmp make PG_CONFIG="$PG_CONFIG" installcheck
      - name: Print diagnostics on failure
        if: failure()
        run: |
          cat regression.diffs || true
          cat "$RUNNER_TEMP/postgres.log" || true
