# Test installation of our homebrew tap for latest version
name: "Packaging tests: Homebrew"
"on":
  schedule:
    # run daily 20:00 on main branch
    - cron: '0 20 * * *'
  pull_request:
    paths: .github/workflows/homebrew.yaml
  push:
    tags:
    - '*'
    branches:
    - release_test
    - trigger/package_test
    - trigger/homebrew_test
  workflow_dispatch:

jobs:
  homebrew:
    name: Homebrew
    runs-on: macos-latest
    strategy:
      fail-fast: false
      matrix:
        license: [ "TSL", "Apache"]
        include:
          - license: Apache
            install_options: "--with-oss-only"

    steps:

    - name: Setup
      run: |
        brew install postgresql@17
        echo "/opt/homebrew/opt/postgresql@17/bin" >> $GITHUB_PATH
        brew tap timescale/tap
        brew info timescaledb

    - name: Install timescaledb
      run: |
        brew uninstall cmake
        brew install timescaledb ${{ matrix.install_options }}
        timescaledb-tune --quiet --yes
        timescaledb_move.sh
        brew services start postgresql@17

    # checkout code to get version information
    - uses: actions/checkout@v4

    - name: Test Installation
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        psql -X -c "CREATE EXTENSION timescaledb;" postgres
        psql -X -c "SELECT extname,extversion,version() FROM pg_extension WHERE extname='timescaledb';" postgres
        version=$(gh release list --json tagName,isLatest --jq '.[] | select(.isLatest) | .tagName')
        installed_version=$(psql -X -t \
            -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb';" \
            postgres | sed -e 's! !!g')
        if [ "$version" != "$installed_version" ];then
          echo "Installed version \"${installed_version}\" does not match expected version \"${version}\"."
          false
        fi

