name: Update test
# This file is part of the pgRouting project.
# Copyright (c) 2021-2026 pgRouting developers
# License: GPL-2 See https://github.com/pgRouting/pgrouting/blob/main/LICENSE

# manually triggered workflow
# - 10+ jobs created and continue to grow

on:
  workflow_dispatch:
  push:
    paths:
      - '.github/workflows/ubuntu.yml'
      - 'src/**'
      - 'include/**'
      - 'sql/**'
      - 'pgtap/**'
      - 'tools/testers/**'
      - 'CMakeLists.txt'

    branches-ignore:
      - 'gh-pages'

  pull_request:
    paths:
      - '.github/workflows/check-queries.yml'
      - 'src/**'
      - 'include/**'
      - 'sql/**'
      - 'pgtap/**'
      - 'tools/testers/**'
      - 'CMakeLists.txt'

    branches-ignore:
      - 'gh-pages'

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

permissions:
  contents: read

jobs:
  update-ver:
    name: Build
    runs-on: ubuntu-latest

    strategy:
        fail-fast: false
        matrix:
          boost_minor: [56]
          old_pgr: [4.0.0, 3.8.0, 3.7.3, 3.7.2, 3.7.1, 3.7.0, 3.6.3, 3.6.2, 3.6.1, 3.6.0, 3.5.1, 3.5.0, 3.4.2, 3.4.1, 3.4.0, 3.3.5, 3.3.4, 3.3.3, 3.3.2, 3.3.1, 3.3.0, 3.0.0, 3.0.1, 3.0.2, 3.0.3, 3.0.4, 3.0.6, 3.1.0, 3.1.1, 3.1.2, 3.1.3, 3.2.0, 3.2.1, 3.2.2]

    steps:
      - uses: actions/checkout@v6

      - name: Get postgres version
        run: |
          sudo service postgresql start
          pgver=$(psql --version | grep -Po '(?<=psql \(PostgreSQL\) )[^;]+(?=\.\d+ \()')
          echo "PGVER=${pgver}" >> $GITHUB_ENV
          echo "PGIS=3" >> $GITHUB_ENV
          PG_USER=$(whoami)
          echo "PG_RUNNER_USER=${PG_USER}" >> $GITHUB_ENV
          PG_PORT=5432
          echo "PGPORT=${PG_PORT}" >> $GITHUB_ENV
          sudo -u postgres psql -p "${PG_PORT}" -c "DROP ROLE IF EXISTS \"${PG_USER}\";"
          sudo -u postgres psql -p "${PG_PORT}" -c "CREATE ROLE \"${PG_USER}\" WITH LOGIN SUPERUSER;"
          sudo -u postgres psql -p "${PG_PORT}" -c "CREATE DATABASE \"${PG_USER}\";"

      - name: Add PostgreSQL APT repository
        run: |
          sudo apt-get install curl ca-certificates gnupg
          curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
          sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ \
            $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'

      - name: Install python
        uses: actions/setup-python@v6
        with:
          python-version: '3.x'

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install -y \
            libtap-parser-sourcehandler-pgtap-perl \
            postgresql-${PGVER} \
            postgresql-${PGVER}-pgtap \
            postgresql-${PGVER}-postgis-${PGIS} \
            postgresql-${PGVER}-postgis-${PGIS}-scripts \
            postgresql-server-dev-${PGVER}

          wget -q https://sourceforge.net/projects/boost/files/boost/1.${{ matrix.boost_minor }}.0/boost_1_${{ matrix.boost_minor }}_0.tar.bz2
          sudo tar --bzip2 -xf boost_1_${{ matrix.boost_minor }}_0.tar.bz2
          sudo mv boost_1_${{ matrix.boost_minor }}_0/boost /usr/include/

      - name: get old version
        uses: actions/checkout@v6
        with:
          repository: 'pgRouting/pgrouting'
          ref: "v${{ matrix.old_pgr }}"
          path: "v${{ matrix.old_pgr }}"

      - name: build old version
        run: |
          pushd  "v${{ matrix.old_pgr }}" || exit 1
          export PATH=/usr/lib/postgresql/${PGVER}/bin:$PATH
          mkdir build
          cd build || exit 1
          cmake -DCMAKE_POLICY_VERSION_MINIMUM=3.12 -DPOSTGRESQL_VERSION=${PGVER} -DCMAKE_BUILD_TYPE=Release -DWITH_DOC=OFF ..
          make -j 4
          sudo make install

      - name: Test old version before update with current tests
        run: |
          sudo service postgresql start
          createdb -p "${PGPORT}"  ___pgr___test___
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "CREATE EXTENSION pgrouting CASCADE;"
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "SELECT * FROM pgr_full_version();"
          DIR=$(git rev-parse --show-toplevel)
          bash "${DIR}/tools/testers/setup_db.sh" "${PGPORT}" ___pgr___test___ "${PG_RUNNER_USER}" "${{ matrix.old_pgr }}"
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "SELECT * FROM pgr_full_version();"
          pg_prove  -Q -f --normalize --directives --recurse -U "${PG_RUNNER_USER}" -d ___pgr___test___ "pgtap"

      - name: Build current version
        run: |
          export PATH=/usr/lib/postgresql/${PGVER}/bin:$PATH
          mkdir build
          cd build
          cmake -DPOSTGRESQL_VERSION=${PGVER} -DCMAKE_BUILD_TYPE=Release ..
          make -j 4
          sudo make install

      - name: Test old version with currrent tests after installing current
        run: |
          sudo service postgresql start
          dropdb -p "${PGPORT}"  ___pgr___test___
          createdb -p "${PGPORT}"  ___pgr___test___
          DIR=$(git rev-parse --show-toplevel)
          bash "${DIR}/tools/testers/setup_db.sh" "${PGPORT}" ___pgr___test___ "${PG_RUNNER_USER}" "${{ matrix.old_pgr }}"
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "SELECT * FROM pgr_full_version();"
          pg_prove  -Q -f --normalize --directives --recurse -U "${PG_RUNNER_USER}" -d ___pgr___test___ "pgtap"

      - name: Test old version with current tests after update
        run: |
          sudo service postgresql start
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "SELECT * FROM pgr_full_version();"
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "ALTER EXTENSION pgrouting UPDATE  TO '4.0.1';"
          psql -p "${PGPORT}"  -d ___pgr___test___ -c "SELECT * FROM pgr_full_version();"
          pg_prove  -Q -f --normalize --directives --recurse -U "${PG_RUNNER_USER}" -d ___pgr___test___ "pgtap"
