- name: Postgres - copy package
  copy:
    src: files/postgres/
    dest: /tmp/build/
  when: debpkg_mode

- name: Postgres - add PPA
  apt_repository:
    repo: "deb [ trusted=yes ] file:///tmp/build ./"
    state: present
  when: debpkg_mode

- name: Postgres - install commons
  apt:
    name: postgresql-common
    install_recommends: no
  when: debpkg_mode

- name: Do not create main cluster
  shell:
    cmd: sed -ri 's/#(create_main_cluster) .*$/\1 = false/' /etc/postgresql-common/createcluster.conf
  when: debpkg_mode

- name: Postgres - install server
  apt:
    name: postgresql-{{ postgresql_major }}={{ postgresql_release }}-1.pgdg20.04+1
    install_recommends: no
  when: debpkg_mode

- name: Postgres - remove PPA
  apt_repository:
    repo: "deb [ trusted=yes ] file:///tmp/build ./"
    state: absent
  when: debpkg_mode

- name: Postgres - cleanup package
  file:
    path: /tmp/build
    state: absent
  when: debpkg_mode

- name: install locales
  apt:
    name: locales
    state: present
  become: yes
  when: stage2_nix

- name: configure locales
  command: echo "C.UTF-8 UTF-8" > /etc/locale.gen && echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen
  become: yes
  when: stage2_nix

- name: locale-gen
  command: sudo locale-gen
  when: stage2_nix

- name: update-locale
  command: sudo update-locale
  when: stage2_nix

- name: Create symlink to /usr/lib/postgresql/bin
  shell:
    cmd: ln -s /usr/lib/postgresql/{{ postgresql_major }}/bin /usr/lib/postgresql/bin
  when: debpkg_mode

- name: create ssl-cert group
  group:
    name: ssl-cert
    state: present
  when: nixpkg_mode
# the old method of installing from debian creates this group, but we must create it explicitly
# for the nix built version

- name: create postgres group
  group:
    name: postgres
    state: present
  when: nixpkg_mode

- name: create postgres user
  shell: adduser --system  --home /var/lib/postgresql --no-create-home --shell /bin/bash --group --gecos "PostgreSQL administrator" postgres
  args:
    executable: /bin/bash
  become: yes
  when: nixpkg_mode

- name: add postgres user to postgres group
  shell: usermod -a -G ssl-cert postgres
  args:
    executable: /bin/bash
  become: yes
  when: nixpkg_mode

- name: Create relevant directories
  file:
    path: '{{ item }}'
    recurse: yes
    state: directory
    owner: postgres
    group: postgres
  with_items:
    - '/home/postgres'
    - '/var/log/postgresql'
    - '/var/lib/postgresql'
  when: debpkg_mode or nixpkg_mode

- name: Allow adminapi to write custom config
  file:
    path: '{{ item }}'
    recurse: yes
    state: directory
    owner: postgres
    group: postgres
    mode: 0775
  with_items:
    - '/etc/postgresql'
    - '/etc/postgresql-custom'
  when: debpkg_mode or nixpkg_mode

- name: create placeholder config files
  file:
    path: '/etc/postgresql-custom/{{ item }}'
    state: touch
    owner: postgres
    group: postgres
    mode: 0664
  with_items:
    - 'generated-optimizations.conf'
    - 'custom-overrides.conf'
  when: debpkg_mode or nixpkg_mode

# Move Postgres configuration files into /etc/postgresql
# Add postgresql.conf
- name: import postgresql.conf
  template:
    src: files/postgresql_config/postgresql.conf.j2
    dest: /etc/postgresql/postgresql.conf
    group: postgres
  when: debpkg_mode or nixpkg_mode

# Add pg_hba.conf
- name: import pg_hba.conf
  template:
    src: files/postgresql_config/pg_hba.conf.j2
    dest: /etc/postgresql/pg_hba.conf
    group: postgres
  when: debpkg_mode or nixpkg_mode

# Add pg_ident.conf
- name: import pg_ident.conf
  template:
    src: files/postgresql_config/pg_ident.conf.j2
    dest: /etc/postgresql/pg_ident.conf
    group: postgres
  when: debpkg_mode or nixpkg_mode

# Add custom config for read replicas set up
- name: Move custom read-replica.conf file to /etc/postgresql-custom/read-replica.conf
  template:
    src: "files/postgresql_config/custom_read_replica.conf.j2"
    dest: /etc/postgresql-custom/read-replica.conf
    mode: 0664
    owner: postgres
    group: postgres
  when: debpkg_mode or nixpkg_mode

# Install extensions before init
- name: Install Postgres extensions
  import_tasks: tasks/setup-docker.yml
  when: debpkg_mode or stage2_nix

#stage 2 postgres tasks
- name: stage2 postgres tasks
  import_tasks: tasks/stage2-setup-postgres.yml
  when: stage2_nix

# init DB
- name: Create directory on data volume
  file:
    path: '{{ item }}'
    recurse: yes
    state: directory
    owner: postgres
    group: postgres
    mode: 0750
  with_items:
    - "/data/pgdata"
  when: debpkg_mode or nixpkg_mode

- name: Link database data_dir to data volume directory
  file:
    src: "/data/pgdata"
    path: "/var/lib/postgresql/data"
    state: link
    force: yes
  when: debpkg_mode or nixpkg_mode

- name: Initialize the database
  become: yes
  become_user: postgres
  shell: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access" -o "--username=supabase_admin"
  vars:
    ansible_command_timeout: 60
  when: debpkg_mode

- name: Make sure .bashrc exists
  file:
    path: /var/lib/postgresql/.bashrc
    state: touch
    owner: postgres
    group: postgres
  when: nixpkg_mode

- name: Check psql_version and modify supautils.conf and postgresql.conf if necessary
  block:
    - name: Check if psql_version is psql_orioledb
      set_fact:
        is_psql_oriole: "{{ psql_version in ['psql_orioledb-16', 'psql_orioledb-17'] }}"

    - name: Initialize the database stage2_nix (non-orioledb)
      become: yes
      become_user: postgres
      shell: source /var/lib/postgresql/.bashrc && /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access" -o "--username=supabase_admin"
      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
      vars:
        ansible_command_timeout: 60
      when: stage2_nix and not is_psql_oriole

    - name: Initialize the database stage2_nix (orioledb)
      become: yes
      become_user: postgres
      shell: >
        source /var/lib/postgresql/.bashrc && initdb -D /var/lib/postgresql/data 
        --allow-group-access 
        --username=supabase_admin 
        --locale-provider=icu 
        --encoding=UTF-8 
        --icu-locale=en_US.UTF-8 
      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
      vars:
        ansible_command_timeout: 60
      when: stage2_nix and is_psql_oriole

- name: copy PG systemd unit
  template:
    src: files/postgresql_config/postgresql.service.j2
    dest: /etc/systemd/system/postgresql.service
  when: debpkg_mode or stage2_nix

- name: copy optimizations systemd unit
  template:
    src: files/database-optimizations.service.j2
    dest: /etc/systemd/system/database-optimizations.service
  when: debpkg_mode or stage2_nix

- name: initialize pg required state
  become: yes
  shell: |
    mkdir -p /run/postgresql
    chown -R postgres:postgres /run/postgresql
  when: stage2_nix and qemu_mode is defined

- name: Restart Postgres Database without Systemd
  become: yes
  become_user: postgres
  shell: |
    source /var/lib/postgresql/.bashrc
    /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data start
  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


# Reload
- name: System - systemd reload
  systemd:
    enabled: yes
    name: postgresql
    daemon_reload: yes
  when: debpkg_mode or stage2_nix


- name: Add LOCALE_ARCHIVE to .bashrc
  lineinfile:
    dest: "/var/lib/postgresql/.bashrc"
    line: 'export LOCALE_ARCHIVE=/usr/lib/locale/locale-archive'
    create: yes
  become: yes
  when: nixpkg_mode

- name: Add LANG items to .bashrc
  lineinfile:
    dest: "/var/lib/postgresql/.bashrc"
    line: "{{ item }}"
  loop: 
    - 'export LANG="en_US.UTF-8"'
    - 'export LANGUAGE="en_US.UTF-8"'
    - 'export LC_ALL="en_US.UTF-8"'
    - 'export LANG="en_US.UTF-8"'
    - 'export LC_CTYPE="en_US.UTF-8"'
  become: yes
  when: nixpkg_mode
