# SPDX-License-Identifier: MIT
---
- name: Test installing and uninstalling all supported versions
  hosts: all
  gather_facts: true
  tasks:
    - name: Skip test if distro does not support multiple versions
      meta: end_host
      when: ansible_facts['distribution'] not in ['CentOS', 'RedHat'] or ansible_facts['distribution_major_version'] not in ["8", "9", "10"]

    - name: Install and cleanup default version
      include_tasks: tasks/install_and_check.yml
      vars:
        __test_check_version: true

    - name: Skip remaining tests if ostree - cannot uninstall/reinstall
      meta: end_host
      when: __postgresql_is_ostree | d(false)

    - name: Save default postgresql_version
      set_fact:
        __default_version: "{{ postgresql_version }}"

    - name: Install and cleanup the other supported versions
      include_tasks: tasks/install_and_check.yml
      loop: "{{ __versions | difference(__unsuppported_versions) }}"
      when: item != __default_version # we already installed it
      vars:
        __test_check_version: true
        postgresql_version: "{{ item }}"
        __versions: "{{ __postgresql_versions_el8 if ansible_facts['distribution_major_version'] == '8' else __postgresql_versions_el9 if ansible_facts['distribution_major_version'] == '9' else __postgresql_versions_el10 if ansible_facts['distribution_major_version'] == '10' else [] }}"
        __unsuppported_versions: "{{ ['16'] if ansible_facts['distribution'] == 'RedHat' and ((ansible_facts['distribution_major_version'] == '8' and ansible_facts['distribution_version'] is version('8.10', '<')) or (ansible_facts['distribution_major_version'] == '9' and ansible_facts['distribution_version'] is version('9.4', '<'))) else [] }}"
