- name: postgresql_ext - install postgis on Linux
  package: name=postgis state=present
  when: ansible_os_family != "Windows"
- name: postgresql_ext - create schema schema1
  become_user: '{{ pg_user }}'
  become: true
  postgresql_schema:
    database: postgres
    name: schema1
    state: present
- name: postgresql_ext - drop extension if exists
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: DROP EXTENSION IF EXISTS postgis
  ignore_errors: true
- name: postgresql_ext - create extension postgis in check_mode
  become_user: '{{ pg_user }}'
  become: true
  postgresql_ext:
    login_db: postgres
    login_port: 5432
    name: postgis
  check_mode: true
  ignore_errors: true
  register: result
- assert:
    that:
    - result is changed
    - result.queries == []
- name: postgresql_ext - check that extension doesn't exist after the previous step
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: SELECT extname FROM pg_extension WHERE extname='postgis'
  ignore_errors: true
  register: result
- assert:
    that:
    - result.rowcount == 0
- name: postgresql_ext - create extension postgis
  become_user: '{{ pg_user }}'
  become: true
  postgresql_ext:
    login_db: postgres
    login_port: 5432
    name: postgis
  ignore_errors: true
  register: result
- assert:
    that:
    - result is changed
    - result.queries == ['CREATE EXTENSION "postgis"']
- name: postgresql_ext - check that extension exists after the previous step
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: SELECT extname FROM pg_extension WHERE extname='postgis'
  ignore_errors: true
  register: result
- assert:
    that:
    - result.rowcount == 1
- name: postgresql_ext - drop extension postgis
  become_user: '{{ pg_user }}'
  become: true
  postgresql_ext:
    db: postgres
    name: postgis
    state: absent
  ignore_errors: true
  register: result
- assert:
    that:
    - result is changed
    - result.queries == ['DROP EXTENSION "postgis"']
- name: postgresql_ext - check that extension doesn't exist after the previous step
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: SELECT extname FROM pg_extension WHERE extname='postgis'
  ignore_errors: true
  register: result
- assert:
    that:
    - result.rowcount == 0
- name: postgresql_ext - create extension postgis
  become_user: '{{ pg_user }}'
  become: true
  postgresql_ext:
    db: postgres
    name: postgis
    schema: schema1
  ignore_errors: true
  register: result
- assert:
    that:
    - result is changed
    - result.queries == ['CREATE EXTENSION "postgis" WITH SCHEMA "schema1"']
- name: postgresql_ext - check that extension exists after the previous step
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: "SELECT extname FROM pg_extension AS e LEFT JOIN pg_catalog.pg_namespace AS n \nON n.oid = e.extnamespace WHERE e.extname='postgis' AND n.nspname='schema1'\n"
  ignore_errors: true
  register: result
- assert:
    that:
    - result.rowcount == 1
- name: postgresql_ext - drop extension postgis cascade
  become_user: '{{ pg_user }}'
  become: true
  postgresql_ext:
    db: postgres
    name: postgis
    state: absent
    cascade: true
  ignore_errors: true
  register: result
- assert:
    that:
    - result is changed
    - result.queries == ['DROP EXTENSION "postgis" CASCADE']
- name: postgresql_ext - check that extension doesn't exist after the previous step
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: SELECT extname FROM pg_extension WHERE extname='postgis'
  ignore_errors: true
  register: result
- assert:
    that:
    - result.rowcount == 0
- name: postgresql_ext - create extension postgis cascade
  become_user: '{{ pg_user }}'
  become: true
  postgresql_ext:
    db: postgres
    name: postgis
    cascade: true
  ignore_errors: true
  register: result
  when: postgres_version_resp.stdout is version('9.6', '<=')
- assert:
    that:
    - result is changed
    - result.queries == ['CREATE EXTENSION "postgis" CASCADE"']
  when: postgres_version_resp.stdout is version('9.6', '<=')
- name: postgresql_ext - check that extension exists after the previous step
  become_user: '{{ pg_user }}'
  become: true
  postgresql_query:
    db: postgres
    query: SELECT extname FROM pg_extension WHERE extname='postgis'
  ignore_errors: true
  register: result
  when: postgres_version_resp.stdout is version('9.6', '<=')
- assert:
    that:
    - result.rowcount == 1
  when: postgres_version_resp.stdout is version('9.6', '<=')