# Test installation of windows package for latest version
name: "Packaging tests: Windows"
"on":
  schedule:
    # run daily 0:00 on main branch
    - cron: '0 0 * * *'
  pull_request:
    paths: .github/workflows/windows-packages.yaml
  push:
    tags:
    - '*'
    branches:
    - release_test
    - trigger/windows_packages
  workflow_dispatch:

jobs:

  build:
    name: Windows package PG${{ matrix.pg }}
    runs-on: windows-2025
    strategy:
      fail-fast: false
      matrix:
        pg: [ "15", "16", "17", "18" ]
    env:
      # PostgreSQL configuration
      PGPORT: 6543
      PGDATA: pgdata
    steps:

    - name: Checkout TimescaleDB source
      uses: actions/checkout@v4

    - name: Get version
      id: version
      env:
        GH_TOKEN: ${{ github.token }}
      run: |
        $version=gh release list --json tagName,isLatest --jq '.[] | select(.isLatest) | .tagName'
        echo "Determined version: "
        echo "version=$version"
        echo "version=$version" >>$env:GITHUB_OUTPUT

    - name: Install PostgreSQL ${{ matrix.pg }}
      run: |
        choco feature disable --name=usePackageExitCodes
        choco feature disable --name=showDownloadProgress
        choco uninstall postgresql --yes
        winget install --id PostgreSQL.PostgreSQL.${{ matrix.pg }} --accept-source-agreements --accept-package-agreements --disable-interactivity
        choco install wget

    - name: Download TimescaleDB
      run: "wget --quiet -O timescaledb.zip 'https://github.com/timescale/timescaledb/releases/download/\
        ${{ steps.version.outputs.version }}/timescaledb-postgresql-${{ matrix.pg}}-windows-amd64.zip'"

    - name: Install TimescaleDB
      run: |
        tar -xf timescaledb.zip
        cd timescaledb
        ./setup.exe -yes-tune -pgconfig C:\Progra~1\PostgreSQL\${{ matrix.pg }}\bin\pg_config

    - name: Create DB
      run: |
        C:\Progra~1\PostgreSQL\${{ matrix.pg }}\bin\initdb -U postgres -A trust
        C:\Progra~1\PostgreSQL\${{ matrix.pg }}\bin\pg_ctl start -o "-cshared_preload_libraries=timescaledb"

    - name: Test creating extension
      run: |
        C:\Progra~1\PostgreSQL\${{ matrix.pg }}\bin\psql -U postgres -d postgres -X `
          -c "CREATE EXTENSION timescaledb" `
          -c "SELECT extname,extversion,version() FROM pg_extension WHERE extname='timescaledb'"
        $installed_version = C:\Progra~1\PostgreSQL\${{ matrix.pg }}\bin\psql -U postgres `
          -d postgres -qtAX -c "SELECT extversion FROM pg_extension WHERE extname='timescaledb'"
        $installed_version = $installed_version.Trim()
        echo "Installed version is '${installed_version}'"
        if ("${installed_version}" -ne "${{ steps.version.outputs.version }}")
        {
          false
        }
