- name: install pg_prove
  apt:
    pkg:
      - libtap-parser-sourcehandler-pgtap-perl
  when: debpkg_mode

# - name: Temporarily disable PG Sodium references in config
#   become: yes
#   become_user: postgres
#   shell:
#     cmd: sed -i.bak -e "s/pg_net,\ pgsodium,\ timescaledb/pg_net,\ timescaledb/g" -e "s/pgsodium.getkey_script=/#pgsodium.getkey_script=/g" /etc/postgresql/postgresql.conf
#   when: debpkg_mode or stage2_nix

- name: Temporarily disable PG Sodium references in config
  become: yes
  become_user: postgres
  shell:
    cmd: >
      sed -i.bak
      -e 's/\(shared_preload_libraries = '\''.*\)pgsodium,\(.*'\''\)/\1\2/'
      -e 's/pgsodium.getkey_script=/#pgsodium.getkey_script=/'
      /etc/postgresql/postgresql.conf
  when: debpkg_mode or stage2_nix

- name: Start Postgres Database to load all extensions.
  become: yes
  become_user: postgres
  shell:
    cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data start "-o -c config_file=/etc/postgresql/postgresql.conf"
  when: debpkg_mode

- name: Stop Postgres Database in stage 2
  become: yes
  become_user: postgres
  shell: source /var/lib/postgresql/.bashrc &&  /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data stop
  args:
    executable: /bin/bash
  environment:
    LANG: en_US.UTF-8
    LANGUAGE: en_US.UTF-8
    LC_ALL: en_US.UTF-8
    LC_CTYPE: en_US.UTF-8
    LOCALE_ARCHIVE: /usr/lib/locale/locale-archive
  when: stage2_nix

- name: Start Postgres Database to load all extensions.
  become: yes
  become_user: postgres
  shell: source /var/lib/postgresql/.bashrc &&  /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data start "-o -c config_file=/etc/postgresql/postgresql.conf"
  args:
    executable: /bin/bash
  environment:
    LANG: en_US.UTF-8
    LANGUAGE: en_US.UTF-8
    LC_ALL: en_US.UTF-8
    LC_CTYPE: en_US.UTF-8
    LOCALE_ARCHIVE: /usr/lib/locale/locale-archive
  when: stage2_nix


- name: Check psql_version and modify migrations if oriole-xx
  block:
    - name: Check if psql_version is psql_orioledb-xx
      set_fact:
        is_psql_oriole: "{{ psql_version in ['psql_orioledb-16', 'psql_orioledb-17'] }}"

    - name: Remove specified extensions from SQL file
      ansible.builtin.command:
        cmd: >
          sed -i '/\\ir.*\(timescaledb\|postgis\|pgrouting\|plv8\).*\.sql/d' /tmp/migrations/tests/extensions/test.sql
      when: is_psql_oriole
      become: yes

    - name: Remove specified extension files from extensions directory
      ansible.builtin.find:
        paths: /tmp/migrations/tests/extensions
        patterns: 
          - '*timescaledb*.sql'
          - '*plv8*.sql'
          - '*postgis*.sql'
          - '*pgrouting*.sql'
      register: files_to_remove
      when: is_psql_oriole

    - name: Delete matched extension files
      ansible.builtin.file:
        path: "{{ item.path }}"
        state: absent
      loop: "{{ files_to_remove.files }}"
      when: is_psql_oriole
      become: yes

- name: Run Unit tests (with filename unit-test-*) on Postgres Database
  shell: /usr/bin/pg_prove -U postgres -h localhost -d postgres -v /tmp/unit-tests/unit-test-*.sql
  register: retval
  failed_when: retval.rc != 0
  when: debpkg_mode or stage2_nix

- name: Run migrations tests
  shell: /usr/bin/pg_prove -U supabase_admin -h localhost -d postgres -v tests/test.sql
  register: retval
  failed_when: retval.rc != 0
  when: debpkg_mode or stage2_nix
  args:
    chdir: /tmp/migrations

- name: Re-enable PG Sodium references in config
  become: yes
  become_user: postgres
  shell:
    cmd: mv /etc/postgresql/postgresql.conf.bak /etc/postgresql/postgresql.conf
  when: debpkg_mode or stage2_nix

- name: Reset db stats
  shell: /usr/lib/postgresql/bin/psql --no-password --no-psqlrc -d postgres -h localhost -U supabase_admin -c 'SELECT pg_stat_statements_reset(); SELECT pg_stat_reset();'
  when: debpkg_mode or stage2_nix

- name: remove pg_prove
  apt:
    pkg:
      - libtap-parser-sourcehandler-pgtap-perl
    state: absent
    autoremove: yes
  when: debpkg_mode

- name: Stop Postgres Database
  become: yes
  become_user: postgres
  shell:
    cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data stop
  when: debpkg_mode or stage2_nix
