---

- name: Set archive dir name
  set_fact: taiga_archive_dir=back_{{ taiga_version_back }}-front_{{ taiga_version_front }}-events_{{ taiga_version_events }}-protected_{{ taiga_version_protected }}
  tags: taiga

- name: Create the archive dir
  file: path={{ taiga_root_dir }}/archives/{{ taiga_archive_dir }} state=directory
  tags: taiga

- name: Install postgresql client
  package:
    name:
      - postgresql16
  tags: taiga

- name: Stop services during upgrade
  service: name={{ item }} state=stopped
  loop:
    - taiga-back
    - taiga-async
    - taiga-events
    - taiga-protected
  when: taiga_install_mode == 'upgrade'
  tags: taiga

- name: Archive previous version
  synchronize:
    src: "{{ taiga_root_dir }}/{{ item }}"
    dest: "{{ taiga_root_dir }}/archives/{{ taiga_archive_dir }}/"
    recursive: True
    delete: True
    compress: False
  loop:
    - venv
    - app
  delegate_to: "{{ inventory_hostname }}"
  tags: taiga

- name: Dump the database
  command: >
    /usr/pgsql-16/bin/pg_dump
    --clean
    --create
    --host={{ taiga_db_server | quote }}
    --port={{ taiga_db_port | quote }}
    --username={{ taiga_db_user | quote }} {{ taiga_db_name | quote }}
    --file="{{ taiga_root_dir }}/archives/{{ taiga_archive_dir }}/{{ taiga_db_name }}.sql"
  environment:
    - PGPASSWORD: "{{ taiga_db_pass }}"
  tags: taiga

