---
# With __mssql_version provide the version to upgrade to
# With __mssql_prev_version provide the current version of SQL Server
- name: Upgrade to a new version
  vars:
    mssql_upgrade: true
    mssql_version: "{{ __mssql_version }}"
  block:
    - name: Upgrade to {{ __mssql_version }}
      include_role:
        name: linux-system-roles.mssql

    - name: Upgrade again to test idempotency - should report not changed
      include_role:
        name: linux-system-roles.mssql
  rescue:
    - name: Assert that upgrading EL 7 to 2022 fails
      when:
        - __mssql_version == 2022
        - ansible_distribution in ['CentOS', 'RedHat']
        - ansible_distribution_version is version('8', '<')
      assert:
        that: >-
          'SQL Server 2022 does not support EL 7 hosts'
          in ansible_failed_result.msg
  always:
    - name: Clean up EL 7 after the role invocation
      when:
        - __mssql_version == 2022
        - ansible_distribution in ['CentOS', 'RedHat']
        - ansible_distribution_version is version('8', '<')
      include_tasks: tasks/cleanup.yml

    # Putting end_host into a rescue block results in a failed task
    - name: End EL 7 host
      when:
        - __mssql_version == 2022
        - ansible_distribution in ['CentOS', 'RedHat']
        - ansible_distribution_version is version('8', '<')
      meta: end_host

- name: >-
    Verify the failure when mssql_version < current version
  block:
    - name: >-
        Upgrade to new version on mssql
        new version {{ __mssql_prev_version }}
      include_role:
        name: linux-system-roles.mssql
      vars:
        mssql_version: "{{ __mssql_prev_version }}"

    - name: Unreachable task
      fail:
        msg: The above task must fail
  rescue:
    # Verify the error message
    # Verify that the error is returned only once in ansible_failed_result
    - name: Assert that the role failed with the correct message
      assert:
        that:
          - >-
            'You set mssql_version to {{ __mssql_prev_version }}, but your SQL'
            in ansible_failed_result.results
            | selectattr('msg', 'defined')
            | map(attribute='msg')
            | join(' ')
          - ansible_failed_result.results | selectattr('msg', 'defined')
            | list
            | length == 1
