name: Test Update and Downgrade
"on":
  push:
    branches:
      - main
      - ?.*.x
  pull_request:
  workflow_dispatch:

jobs:
  update_test:
    name: Update test PG${{ matrix.pg }}
    runs-on: 'ubuntu-latest'
    strategy:
      matrix:
        pg: [15, 16, 17, 18]
      fail-fast: false
    env:
      PG_VERSION: ${{ matrix.pg }}
      POSTGRES_HOST_AUTH_METHOD: trust
    steps:
    - name: Checkout TimescaleDB
      uses: actions/checkout@v4

    - name: Install Dependencies
      run: |
        sudo apt-get update
        sudo apt-get install gnupg systemd-coredump gdb postgresql-common libkrb5-dev
        yes | sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
        echo "deb https://packagecloud.io/timescale/timescaledb/ubuntu/ $(lsb_release -c -s) main" | sudo tee /etc/apt/sources.list.d/timescaledb.list
        wget --quiet -O - https://packagecloud.io/timescale/timescaledb/gpgkey | sudo apt-key add -
        sudo apt-get update
        sudo apt-get install postgresql-${{ matrix.pg }} postgresql-server-dev-${{ matrix.pg }}
        sudo apt-get install -y --no-install-recommends timescaledb-2-postgresql-${{ matrix.pg }}
        git fetch --tags

    - name: Update tests PG${{ matrix.pg }}
      run: |
        PATH="/usr/lib/postgresql/${{ matrix.pg }}/bin:$PATH"
        ./scripts/test_updates.sh

    - name: Downgrade tests PG${{ matrix.pg }}
      if: always()
      run: |
        PATH="/usr/lib/postgresql/${{ matrix.pg }}/bin:$PATH"
        ./scripts/test_downgrade.sh

    - name: Update diff
      if: failure()
      run: |
        find update_test -name "*.diff" | xargs -IFILE sh -c "echo '\nFILE\n';cat FILE"

    - name: Postgres Errors
      if: failure()
      run: |
        find update_test -name postgres.log -exec grep ERROR {} \;

    - name: Check for coredumps
      if: always()
      id: collectlogs
      run: |
        # wait for in progress coredumps
        sleep 10
        if coredumpctl list; then
          echo "coredumps=true" >>$GITHUB_OUTPUT
          false
        fi

    - name: Stack trace
      if: always() && steps.collectlogs.outputs.coredumps == 'true'
      run: |
        sudo coredumpctl gdb <<<"
          set verbose on
          set trace-commands on
          show debug-file-directory
          printf "'"'"query = '%s'\n\n"'"'", (char *) debug_query_string
          frame function ExceptionalCondition
          printf "'"'"condition = '%s'\n"'"'", (char *) conditionName
          up 1
          l
          info args
          info locals
          bt full
        " 2>&1 | tee stacktrace.log
        ./scripts/bundle_coredumps.sh
        false

    - name: Upload Coredumps
      if: always() && steps.collectlogs.outputs.coredumps == 'true'
      uses: actions/upload-artifact@v4
      with:
        name: Coredumps update-test ${{ matrix.os }} PG${{ matrix.pg }}
        path: coredumps

    - name: Upload Artifacts
      if: always()
      uses: actions/upload-artifact@v4
      with:
        name: Update test PG${{ matrix.pg }}
        path: update_test
