#
# Copyright (c) 2020 Bubble, Inc.  All rights reserved. For personal (non-commercial) use, see license: https://getbubblenow.com/bubble-license/
#
- name: Install PostgreSQL
  apt:
    name: [ 'postgresql', 'libpq-dev' ]
    state: present
    update_cache: yes

- name: Add postgres user to bubble-log group
  user:
    name: postgres
    groups: bubble-log
    append: yes

- name: Install PostgreSQL pg_bubble.conf
  copy:
    src: pg_bubble.conf
    dest: /etc/postgresql/12/main/conf.d/
    owner: postgres
    group: postgres
    mode: 0400

- name: Install PostgreSQL pg_sage.conf
  copy:
    src: pg_sage.conf
    dest: /etc/postgresql/12/main/conf.d/
    owner: postgres
    group: postgres
    mode: 0400
  when: install_type == 'sage'

- name: Install notifications tables' cleaner cron
  cron:
    name: "Cleaning notifications tables"
    special_time: "hourly"
    user: "postgres"
    job: "HOUR_AGO=$(date -d '1 month ago' +\"%s000\") && psql -d bubble -c \"DELETE FROM sent_notification WHERE mtime < ${HOUR_AGO}\" -c \"DELETE FROM received_notification WHERE mtime < ${HOUR_AGO}\""

- name: Link to postgres log dir from bubble's log dir
  file:
    src: /var/log/postgresql
    dest: /var/log/bubble/postgresql
    owner: root
    group: postgres
    state: link
