---
# task file for postgresql
- name: "Install PostgreSQL dependencies"
  ansible.builtin.package:
    name: "{{ _zabbix_server_pgsql_dependencies | select | list }}"
  environment:
    http_proxy: "{{ zabbix_http_proxy | default(None) | default(omit) }}"
    https_proxy: "{{ zabbix_https_proxy | default(None) | default(omit) }}"
  register: _zabbix_server_dependencies_installed
  until: _zabbix_server_dependencies_installed is succeeded
  become: true
  tags:
    - install
    - database
    - dependencies

- name: "PostgreSQL Database prep"
  when: zabbix_server_database_creation
  become: "{{ zabbix_server_dbhost_run_install }}"
  become_user: postgres
  delegate_to: "{{ zabbix_server_dbhost_run_install | ternary(delegated_dbhost, inventory_hostname) }}"
  vars:
    delegated_dbhost: "{{ (zabbix_server_dbhost == 'localhost') | ternary(inventory_hostname, zabbix_server_dbhost) }}"
  tags:
    - database
  block:
    - name: "PostgreSQL | Create database"
      community.postgresql.postgresql_db:
        login_user: "{{ zabbix_server_pgsql_login_user | default(omit) }}"
        login_password: "{{ zabbix_server_pgsql_login_password | default(omit) }}"
        login_host: "{{ zabbix_server_pgsql_login_host | default(omit) }}"
        port: "{{ zabbix_server_dbport }}"
        login_unix_socket: "{{ zabbix_server_pgsql_login_unix_socket | default(omit) }}"
        name: "{{ zabbix_server_dbname }}"

    - name: "PostgreSQL | Create database user"
      community.postgresql.postgresql_user:
        login_user: "{{ zabbix_server_pgsql_login_user | default(omit) }}"
        login_password: "{{ zabbix_server_pgsql_login_password | default(omit) }}"
        login_host: "{{ zabbix_server_pgsql_login_host | default(omit) }}"
        port: "{{ zabbix_server_dbport }}"
        login_unix_socket: "{{ zabbix_server_pgsql_login_unix_socket | default(omit) }}"
        name: "{{ zabbix_server_dbuser }}"
        password: "{{ ('md5' + (zabbix_server_dbpassword + zabbix_server_dbuser)|hash('md5')) if zabbix_server_dbpassword_hash_method == 'md5' else zabbix_server_dbpassword }}"

    - name: "PostgreSQL | Set database/user permissions"
      community.postgresql.postgresql_privs:
        login_user: "{{ zabbix_server_pgsql_login_user | default(omit) }}"
        login_password: "{{ zabbix_server_pgsql_login_password | default(omit) }}"
        login_host: "{{ zabbix_server_pgsql_login_host | default(omit) }}"
        port: "{{ zabbix_server_dbport }}"
        login_unix_socket: "{{ zabbix_server_pgsql_login_unix_socket | default(omit) }}"
        db: "{{ zabbix_server_dbname }}"
        privs: ALL
        type: schema
        objs: public
        role: "{{ zabbix_server_dbuser }}"

    - name: "PostgreSQL | Create timescaledb extension"
      when: zabbix_server_database_timescaledb
      community.postgresql.postgresql_ext:
        login_user: "{{ zabbix_server_pgsql_login_user | default(omit) }}"
        login_password: "{{ zabbix_server_pgsql_login_password | default(omit) }}"
        login_host: "{{ zabbix_server_pgsql_login_host | default(omit) }}"
        port: "{{ zabbix_server_dbport }}"
        login_unix_socket: "{{ zabbix_server_pgsql_login_unix_socket | default(omit) }}"
        db: "{{ zabbix_server_dbname }}"
        name: timescaledb

- name: "PostgreSQL verify or create schema"
  when: zabbix_server_database_sqlload
  tags:
    - database
  block:
    - name: "PostgreSQL | Get current database version"
      community.postgresql.postgresql_query:
        login_user: "{{ zabbix_server_dbuser }}"
        login_password: "{{ zabbix_server_dbpassword }}"
        login_host: "{{ zabbix_server_dbhost }}"
        port: "{{ zabbix_server_dbport }}"
        db: "{{ zabbix_server_dbname }}"
        query: "SELECT mandatory FROM dbversion"
  rescue:
    - name: "PostgreSQL | Import schema"
      community.postgresql.postgresql_db:
        login_user: "{{ zabbix_server_dbuser }}"
        login_password: "{{ zabbix_server_dbpassword }}"
        login_host: "{{ zabbix_server_dbhost }}"
        port: "{{ zabbix_server_dbport }}"
        db: "{{ zabbix_server_dbname }}"
        state: restore
        target: "{{ zabbix_server_database_file_dir }}postgresql/server.sql.gz"

- name: "Configure Timescale"
  when: zabbix_server_database_timescaledb
  tags:
    - database
  block:
    - name: "PostgreSQL | Check if TimescaleDB has been configured"
      community.postgresql.postgresql_query:
        login_user: "{{ zabbix_server_dbuser }}"
        login_password: "{{ zabbix_server_dbpassword }}"
        login_host: "{{ zabbix_server_dbhost }}"
        port: "{{ zabbix_server_dbport }}"
        db: "{{ zabbix_server_dbname }}"
        query: "SELECT db_extension FROM config WHERE db_extension = 'timescaledb'"
      register: _config_db_extension

    # NOTE: Zabbix 7.0 adds a hypertable for auditlog.
    - name: "PostgreSQL | Check for auditlog hypertable"
      community.postgresql.postgresql_query:
        login_user: "{{ zabbix_server_dbuser }}"
        login_password: "{{ zabbix_server_dbpassword }}"
        login_host: "{{ zabbix_server_dbhost }}"
        port: "{{ zabbix_server_dbport }}"
        db: "{{ zabbix_server_dbname }}"
        query: "SELECT hypertable_name FROM timescaledb_information.hypertables WHERE hypertable_name = 'auditlog'"
      register: _hypertable_auditlog

    - name: "PostgreSQL | Create TimescaleDB hypertables"
      when: (_config_db_extension.query_result | length == 0) or
        (zabbix_server_version is version('7.0', '>=') and _hypertable_auditlog.query_result | length == 0)
      community.postgresql.postgresql_db:
        login_user: "{{ zabbix_server_dbuser }}"
        login_password: "{{ zabbix_server_dbpassword }}"
        login_host: "{{ zabbix_server_dbhost }}"
        port: "{{ zabbix_server_dbport }}"
        db: "{{ zabbix_server_dbname }}"
        state: restore
        target: "{{ is_legacy_version | ternary(legacy_path, modern_path) }}"
      vars:
        is_legacy_version: "{{ zabbix_server_version is version('7.0', '<') }}"
        legacy_path: /usr/share/zabbix-sql-scripts/postgresql/timescaledb.sql
        modern_path: "{{ zabbix_server_database_file_dir }}postgresql/timescaledb/schema.sql" 