From abe814412ec89d3800401d8b70f2cf449a5794b5 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 14 Jan 2014 16:59:55 +1100 Subject: [PATCH] Source DB config more cleanly, fixes issues with socket in prd --- app/models/global_setting.rb | 13 +++++++++++++ config/application.rb | 17 ++++++++++++++--- config/database.yml | 17 +++++------------ 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/app/models/global_setting.rb b/app/models/global_setting.rb index a94f1eb5276..387468d9b3a 100644 --- a/app/models/global_setting.rb +++ b/app/models/global_setting.rb @@ -16,6 +16,19 @@ class GlobalSetting end end + def self.database_config + hash = {"adapter" => "postgresql"} + %w{pool timeout socket host port username password}.each do |s| + if val = self.send("db_#{s}") + hash[s] = val + end + end + hash["host_names"] = [ hostname ] + hash["database"] = db_name + + {"production" => hash} + end + class BaseProvider def self.coerce(setting) diff --git a/config/application.rb b/config/application.rb index 99e30240210..d17b9eee44b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -8,10 +8,21 @@ require_relative '../lib/discourse_plugin_registry' require_relative '../app/models/global_setting' if defined?(Bundler) - # If you precompile assets before deploying to production, use this line Bundler.require(*Rails.groups(assets: %w(development test profile))) - # If you want your assets lazily compiled in production, use this line - # Bundler.require(:default, :assets, Rails.env) +end + +# PATCH DB configuration +class Rails::Application::Configuration + + def database_configuration_with_global_config + if Rails.env == "production" + GlobalSetting.database_config + else + database_configuration_without_global_config + end + end + + alias_method_chain :database_configuration, :global_config end module Discourse diff --git a/config/database.yml b/config/database.yml index ddf77c2a7ef..8013e58b9da 100644 --- a/config/database.yml +++ b/config/database.yml @@ -33,15 +33,8 @@ profile: host_names: - "localhost" -production: - pool: <%= GlobalSetting.db_pool %> - timeout: <%= GlobalSetting.db_timeout %> - adapter: postgresql - socket: <%= GlobalSetting.db_socket %> - host: <%= GlobalSetting.db_host %> - port: <%= GlobalSetting.db_port %> - database: <%= GlobalSetting.db_name %> - username: <%= GlobalSetting.db_username %> - password: <%= GlobalSetting.db_password %> - host_names: - - <%= GlobalSetting.hostname %> +# You may be surprised production is not here, it is sourced from application.rb using a monkey patch +# This is done for 2 reasons +# +# 1. we need to support blank settings correctly and rendering nothing in yaml/erb is a PITA +# 2. why go from object -> yaml -> object, pointless