---
- name: Assert fail on EL 7 with version = 2022 and EL 9 with version != 2022
  include_tasks: assert_fail_on_unsupported_ver.yml

- name: Set up MSSQL and input sql files with pre_input
  include_role:
    name: linux-system-roles.mssql
  vars:
    mssql_pre_input_sql_file:
      - create_example_db.j2
      - create_example_db.j2
    mssql_password: "p@55w0rD"
    mssql_edition: Evaluation
    # Enable FTS in the beginning so that it works at the end
    # It takes some time for mssql-server to configure it after RPM is installed
    mssql_install_fts: true
    # noqa var-naming[no-role-prefix]
    __mssql_test_db_name: ExampleDB1

- name: Assert the latest script invocation resulted in no changes
  assert:
    that:
      - >-
        'The ExampleDB1 database already exists, skipping' in
        __mssql_sqlcmd_input.stdout
      - >-
        'The Inventory table already exists, skipping' in
        __mssql_sqlcmd_input.stdout

- name: Input sql files with post_input into custom storage directory
  include_role:
    name: linux-system-roles.mssql
  vars:
    mssql_datadir: /tmp/mssql_data
    mssql_logdir: /tmp/mssql_log
    mssql_datadir_mode: '0700'
    mssql_logdir_mode: '0700'
    mssql_post_input_sql_file:
      - sql_script.sql
      - sql_script.sql
    mssql_password: "p@55w0rD"

- name: Assert the latest script invocation resulted in no changes
  assert:
    that:
      - >-
        'The MyLogin login already exists, skipping'
        in __mssql_sqlcmd_input.stdout
      - >-
        'The MyUser user already exists, skipping'
        in __mssql_sqlcmd_input.stdout

- name: Verify custom storage
  include_tasks: verify_settings.yml
  vars:
    __verify_mssql_datadir: /tmp/mssql_data
    __verify_mssql_logdir: /tmp/mssql_log
    __verify_mssql_datadir_mode: '0700'
    __verify_mssql_logdir_mode: '0700'

- name: Set up MSSQL and input sql content with pre_input
  include_role:
    name: linux-system-roles.mssql
  vars:
    mssql_pre_input_sql_content:
      - "{{ lookup('template', 'create_example_db.j2') }}"
      - "{{ lookup('template', 'create_example_db.j2') }}"
    __mssql_test_db_name: ExampleDB2
    mssql_password: "p@55w0rD"
    mssql_edition: Evaluation

- name: Assert the latest script invocation resulted in no changes
  assert:
    that: >-
      'The ExampleDB2 database already exists, skipping' in
      __mssql_sqlcmd_input.stdout

- name: Set up MSSQL and input sql content with post_input
  include_role:
    name: linux-system-roles.mssql
  vars:
    mssql_post_input_sql_content:
      - "{{ lookup('file', 'sql_script.sql') }}"
    mssql_password: "p@55w0rD"

- name: Assert the latest script invocation resulted in no changes
  assert:
    that: >-
      'The MyLogin login already exists, skipping'
      in __mssql_sqlcmd_input.stdout

- name: Verify the failure when the mssql_password var is not specified
  block:
    - name: Input the sql file without the mssql_password variable
      include_role:
        name: linux-system-roles.mssql
      vars:
        mssql_pre_input_sql_file: sql_script.sql

    - name: Unreachable task
      fail:
        msg: The above task must fail

  rescue:
    - name: Assert that the role failed with mssql_password not defined
      assert:
        that: >-
          'You must define the mssql_password variable' in
          ansible_failed_result.msg

- name: Verify the failure when the mssql_password var is not specified
  block:
    - name: Input the sql file without the mssql_password variable
      include_role:
        name: linux-system-roles.mssql
      vars:
        mssql_post_input_sql_file: sql_script.sql

    - name: Unreachable task
      fail:
        msg: The above task must fail

  rescue:
    - name: Assert that the role failed with mssql_password not defined
      assert:
        that: >-
          'You must define the mssql_password variable' in
          ansible_failed_result.msg

- name: Verify that FTS was enabled in the beginning of this test playbook
  include_role:
    name: linux-system-roles.mssql
  vars:
    mssql_post_input_sql_file: verify_fts.sql
    mssql_password: "p@55w0rD"

- name: Assert the latest script invocation resulted in no changes
  assert:
    that:
      - >-
        'Full-Text Search is enabled'
        in __mssql_sqlcmd_input.stdout
