run:

  - replace:
      filename: "/etc/postgresql/12/main/postgresql.conf"
      from: /#?fullpage_writes *=.*/
      to: "fullpage_writes = off"

  - replace:
      filename: "/etc/postgresql/12/main/postgresql.conf"
      from: /#?fsync *=.*/
      to: "fsync = off"

  - exec:
      background: true
      # use fast shutdown for pg
      stop_signal: INT
      cmd: HOME=/var/lib/postgresql USER=postgres exec chpst -u postgres:postgres:ssl-cert -U postgres:postgres:ssl-cert /usr/lib/postgresql/12/bin/postmaster -D /etc/postgresql/12/main

  - exec:
      background: true
      cmd: exec chpst -u redis -U redis /usr/bin/redis-server /etc/redis/redis.conf

  # give db a few secs to start up
  - exec: "sleep 5"

  - exec: su postgres -c 'createdb discourse_development' || true
  - exec: su postgres -c 'psql discourse_development -c "grant all privileges on database discourse_development to discourse;"' || true
  - exec: su postgres -c 'psql discourse_development -c "alter schema public owner to discourse;"'
  - exec: su postgres -c 'psql discourse_development -c "create extension if not exists hstore;"'
  - exec: su postgres -c 'psql discourse_development -c "create extension if not exists pg_trgm;"'

  - exec: su postgres -c 'createdb discourse_test' || true
  - exec: su postgres -c 'psql discourse_test -c "grant all privileges on database discourse_test to discourse;"' || true
  - exec: su postgres -c 'psql discourse_test -c "alter schema public owner to discourse;"'
  - exec: su postgres -c 'psql discourse_test -c "create extension if not exists hstore;"'
  - exec: su postgres -c 'psql discourse_test -c "create extension if not exists pg_trgm;"'

  - exec: su postgres -c 'createdb discourse_test_multisite' || true
  - exec: su postgres -c 'psql discourse_test_multisite -c "grant all privileges on database discourse_test_multisite to discourse;"' || true
  - exec: su postgres -c 'psql discourse_test_multisite -c "alter schema public owner to discourse;"'
  - exec: su postgres -c 'psql discourse_test_multisite -c "create extension if not exists hstore;"'
  - exec: su postgres -c 'psql discourse_test_multisite -c "create extension if not exists pg_trgm;"'

  - exec: cd tmp && git clone https://github.com/discourse/discourse.git --depth=1 && cd /tmp/discourse && sudo -u discourse bundle install
  - exec: chown -R discourse /tmp/discourse
  - exec: cd /tmp/discourse && sudo -u discourse bundle exec rake db:migrate
  - exec: cd /tmp/discourse && sudo -u discourse RAILS_ENV=test bundle exec rake db:migrate
  - exec: rm -fr /tmp/discourse
