- name: install postresql
  package: state=present pkg={{ item }}
  with_items:
  - "postgresql-server"
  - "postgresql-contrib"
  - "pspg"

- name: See if PostgreSQL is initialized
  stat: path=/var/lib/pgsql/data/PG_VERSION
  register: postgres_initialized

- name: init postgresql
  shell: "postgresql-setup initdb"
  when: not postgres_initialized.stat.exists

- name: copy pg_hba.conf
  copy: src="pg/pg_hba.conf" dest=/var/lib/pgsql/data/pg_hba.conf owner=postgres group=postgres mode=0600
  notify:
  - restart postgresql
  tags:
  - config

- name: Ensure postgres has a place to backup to
  file: dest=/backups state=directory owner=postgres
  tags:
  - config

# TODO: I think we missing user creation, check it we do it somewhere else ...

- name: Copy over backup scriplet
  copy: src="{{ files }}/../roles/postgresql_server/files/backup-database" dest=/usr/local/bin/backup-database mode=0755
  tags:
  - config

- name: Set up some cronjobs to backup databases as configured
  template: >
    src="{{ files }}/../roles/postgresql_server/templates/cron-backup-database"
    dest="/etc/cron.d/cron-backup-database-{{ item }}"
  with_items:
  - "{{ dbs_to_backup }}"
  when: dbs_to_backup != []
  tags:
  - config

- name: enable Pg service
  service: state=started enabled=yes name=postgresql

- name: Create db
  postgresql_db: name="coprdb" encoding='UTF-8'
  become: yes
  become_user: postgres

- name: Create db user
  postgresql_user: db="coprdb" name="copr-fe" password="{{ copr_database_password }}" role_attr_flags=SUPERUSER,NOCREATEDB,NOCREATEROLE
  become: yes
  become_user: postgres

- name: set shared_buffers for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^shared_buffers ='
    line: 'shared_buffers = 1024MB'
  notify: restart postgresql
  tags:
  - config

- name: set effective_cache_size for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^effective_cache_size ='
    line: 'effective_cache_size = 2048MB'
  notify: restart postgresql
  tags:
  - config

- name: set work_mem for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^work_mem ='
    line: 'work_mem = 4MB'
  notify: restart postgresql
  tags:
  - config

- name: set maintenance_work_mem for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^maintenance_work_mem ='
    line: 'maintenance_work_mem = 500MB'
  notify: restart postgresql
  tags:
  - config

- name: set checkpoint_completion_target for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^checkpoint_completion_target ='
    line: 'checkpoint_completion_target = 0.9'
  notify: restart postgresql
  tags:
  - config

- name: set log_min_duration_statement for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^log_min_duration_statement ='
    line: 'log_min_duration_statement = 500'
  notify: restart postgresql
  tags:
  - config

- name: set max_connections for PostgreSQL
  lineinfile:
    path: /var/lib/pgsql/data/postgresql.conf
    regexp: '^max_connections ='
    line: 'max_connections = 200'
  notify: restart postgresql
  tags:
  - config

- name: install psqlrc file
  copy:
    content: |
      \pset linestyle unicode
      \pset border 2

      -- Switch pagers with :x and :xx commands
      \set x '\\setenv PAGER less'
      \set xx '\\setenv PAGER \'pspg -bX --no-mouse\''
      :xx
    dest: "{{ copr_fe_homedir }}/.psqlrc"
    owner: copr-fe
    group: copr-fe
    mode: 0600

- name: install pgpass file
  copy:
    content: |
      localhost:*:coprdb:copr-fe:{{ copr_database_password }}
    dest: "{{ copr_fe_homedir }}/.pgpass"
    owner: copr-fe
    group: copr-fe
    mode: 0400

- stat: path="{{ copr_fe_homedir }}/.psql_history"
  register: history_file

- name: install pghistory file
  file: path="{{ copr_fe_homedir }}/.psql_history" state=touch
        owner=copr-fe group=copr-fe mode=0600
  when: not history_file.stat.exists
