#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

<%
DATABASE_OPTIONS = %w[
  url
  database
  encoding
  pool
  username
  password
  sslmode
  sslcompression
  sslcert
  sslkey
  sslpassword
  sslrootcert
  sslcrl
  application_name
  statement_timeout
].freeze

DATABASE_VARIABLES = %w[
  statement_timeout
]

DATABASE_DEFAULTS = {
  # Fallback to unprefixed DATABASE_URL if present
  'url' => ENV['DATABASE_URL'],
  'statement_timeout' => ENV.fetch("POSTGRES_STATEMENT_TIMEOUT", "90s"),
  'encoding' => 'unicode',
  'application_name' => 'openproject',
}.freeze
%>

production:
  adapter: postgresql
  <% DATABASE_OPTIONS.each do |option| %>
  <% env_name = "OPENPROJECT_DB_#{option.upcase}" %>
  <% value = ENV.fetch(env_name, DATABASE_DEFAULTS[option]) %>
  <% unless value.nil? %>
  <%= option %>: <%= value %>
  <% end %>
  <% end %>
  variables:
    <% DATABASE_VARIABLES.each do |option| %>
    <% env_name = "OPENPROJECT_DB_#{option.upcase}" %>
    <% value = ENV.fetch(env_name, DATABASE_DEFAULTS[option]) %>
    <% unless value.nil? %>
    <%= option %>: <%= value %>
    <% end %>
    <% end %>
