# Set up Postgres, create the database, and populate it.

- name: Install dependencies
  dnf: name={{ item }} state=present
  with_items:
    - postgresql-server
    - python3-psycopg2
    # For the ansible module
    - python-psycopg2

- name: Set up postgresql database
  command: postgresql-setup --initdb
  args:
      creates: /var/lib/pgsql/data/base

- name: Set up postgresql access rules to allow local access
  copy:
      src: pg_hba.conf
      dest: /var/lib/pgsql/data/pg_hba.conf
      owner: postgres
      group: postgres
      mode: 0600
  notify: restart postgresql

- name: Start and enable postgresql
  service: name=postgresql state=started enabled=yes

- name: Set up the DB user
  postgresql_user:
      name: hubs
      password: "{{ hubs_db_password }}"
      role_attr_flags: NOSUPERUSER,NOCREATEROLE,NOCREATEDB
  become: true
  become_user: postgres

- name: Create the database
  postgresql_db:
      name: hubs
      owner: hubs
  register: db_creation
  become: true
  become_user: postgres

- name: Populate the Fedora Hubs database
  command: "python3 {{ hubs_code_dir }}/populate.py"
  args:
    chdir: "{{ hubs_code_dir }}"
  environment:
    HUBS_CONFIG: "{{ hubs_conf_dir }}/hubs.py"
  become: true
  become_user: "{{ main_user }}"
  when: db_creation|succeeded and db_creation is changed and hubs_dev_mode
