name: Run pg_stat_kcache tests

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

env:
  DATADIR: /dev/shm/data
  LOGFILE: /dev/shm/data/logfile

jobs:
  pg_stat_kcache_tests:
    name: pg_stat_kcache_tests
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        postgres_major_version: [
          "9.4",
          "9.5",
          "9.6",
          "10",
          "11",
          "12",
          "13",
          "14",
          "15",
          "16",
          "17",
          "18",
          "19beta"
        ]
        os: ["ubuntu-22.04"]

    steps:
    - uses: actions/checkout@v4

    - name: Set up prerequisites and environment
      run: |
        echo "*********** ENVIRONMENT ************"
        if [[ "${{ matrix.postgres_major_version }}" == *"devel" ]]; then
          pg_major=$( echo "${{ matrix.postgres_major_version }}" | grep -Eo "[0-9]+")
          devel="yes"
          beta="no"
        elif [[ "${{ matrix.postgres_major_version }}" == *"beta" ]]; then
          pg_major=$( echo "${{ matrix.postgres_major_version }}" | grep -Eo "[0-9]+")
          devel="no"
          beta="yes"
        else
          pg_major="${{ matrix.postgres_major_version }}"
          devel="no"
          beta="no"
        fi

        export PG_MAJOR_VERSION=${pg_major}
        echo "PG_MAJOR_VERSION=$PG_MAJOR_VERSION" >> $GITHUB_ENV
        echo "MAKEFLAGS=$MAKEFLAGS -j $(grep -c ^processor /proc/cpuinfo)" >> $GITHUB_ENV
        echo ""

        echo "************ CLEAN IMAGE ***********"
        sudo apt remove -y '^postgres.*' '^libpq.*'
        echo ""

        echo "********* REPOSITORY SET UP ********"
        sudo apt-get install -y wget gnupg

        if [[ "${devel}" == "yes" ]]; then
          sudo add-apt-repository "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg-snapshot main ${PG_MAJOR_VERSION}"
        elif [[ "${beta}" == "yes" ]]; then
          sudo sh -c "echo \"deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main ${PG_MAJOR_VERSION}\" > /etc/apt/sources.list.d/pgdg.list"
        else
        sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
        fi

        # pin the pgdg repository with higher priority than the OS (500)
        echo "Package: *" | sudo tee /etc/apt/preferences.d/pgdg.pref
        echo "Pin: release o=apt.postgresql.org" | sudo tee -a /etc/apt/preferences.d/pgdg.pref
        echo "Pin-Priority: 800" | sudo tee -a /etc/apt/preferences.d/pgdg.pref

        wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
        sudo apt-get update -y -qq --fix-missing
        echo ""

        echo "******** INSTALL POSTGRES **********"
        sudo apt-get install -y \
          postgresql-$PG_MAJOR_VERSION \
          postgresql-server-dev-$PG_MAJOR_VERSION \
          postgresql-contrib-$PG_MAJOR_VERSION
        echo ""

        echo "******* INSTALL DEPENDENCIES *******"
        sudo apt-get install -y \
          gcc \
          make \
          build-essential \
          pkg-config
        echo ""

        echo "********** READJUST PATH ***********"
        export PATH=$(pg_config --bindir):$PATH
        echo "PATH=$PATH" >> $GITHUB_ENV
        cat $GITHUB_ENV
        echo ""

    - name: Start a postgres ${{ matrix.postgres_major_version }} server
      run: |
        sudo chmod a+rwx /var/run/postgresql/
        pg_ctl -D $DATADIR initdb
        pg_ctl -D $DATADIR -l $LOGFILE start || cat $LOGFILE
        # a sleep is required for pg9.6 (at least)
        sleep 1
        psql -c 'select 1 as ok' postgres

    - name: Build and install pg_stat_kcache for postgres ${{ matrix.postgres_major_version }}
      run: |
        make
        sudo make install

    - name: Enable pg_stat_kcache on postgres ${{ matrix.postgres_major_version }} server
      run: |
        echo "shared_preload_libraries = 'pg_stat_statements,pg_stat_kcache'" >> $DATADIR/postgresql.conf
        pg_ctl -D $DATADIR -l $LOGFILE restart || cat $LOGFILE
        # a sleep is required for pg9.6 (at least)
        sleep 1
        psql -c 'select 1 as ok' postgres

    - name: Run pg_stat_kcache tests for postgres ${{ matrix.postgres_major_version }}
      run: make installcheck || ( errcode=$?; cat regression.diffs && exit $errcode )

    - name: Check extension install vs upgrade for postgres ${{ matrix.pgversion }}
      run: |
        # install dependencies
        sudo apt-get install -y silversearcher-ag

        # install rust
        rustup toolchain install ${{ env.RUST }}

        # install pg_validate_extupgrade
        git clone https://github.com/rjuju/pg_validate_extupgrade.git
        cd pg_validate_extupgrade
        cargo build
        cd ..

        # pgsk depends on pg_stat_statements
        psql -Xc "CREATE EXTENSION pg_stat_statements" postgres

        # get the default extension version
        to_ver=$(ag default_version pg_stat_kcache.control | ag -o "(\d+\.?)+")
        echo "to_ver: ${to_ver}"

        # Check the number of extension scripts containing the default versions
        nb=$(ls *--*--${to_ver}.sql | wc -l)

        # If only one sql script found with the default version, it should be a
        # new major version that is allowed to no provide an upgrade script.
        if [ ${nb} != 1 ]; then

          echo "Could not find ugprade script for version ${to_ver}"
          exit 1
        fi

        # Get the previous version
        from_ver=$(ls *--*--${to_ver}.sql \
          | ag -o 'pg_stat_kcache--\d+\.\d+\.\d+' \
          | ag -o "\d+\.\d+\.\d+")
          echo "from_ver: ${from_ver}"

        # Generate the config file
        cat .github/pg_stat_kcache.toml.template \
          | sed "s/%%FROM_VER%%/${from_ver}/" \
          | sed "s/%%TO_VER%%/${to_ver}/" \
          > pg_stat_kcache.toml

        # Run pg_validate_extupgrade
        echo "Running pg_validate_extupgrade..."
        ./pg_validate_extupgrade/target/debug/pg_validate_extupgrade \
          -d postgres \
          -c ./pg_stat_kcache.toml

    - name: Stop the running postgres ${{ matrix.postgres_major_version }} server
      run: pg_ctl -D $DATADIR stop
