port initialize_database_with_postgresql_patches to rails3

fixes CNVS-9518

test plan:
 * set up your database to point to a host/port that ignores
   connections (*not* a non-existent name, or one that will actively
   refuse it). `nc <host> <port>` that doesn't return immediately,
   but doesn't actually return anything is a good test.
 * add a connect_timeout: 90 to database.yml and run script/server;
   it should take at least 90 seconds before giving you a failure
 * change connect_timeout to 3. it should fail MUCH faster

Change-Id: I97b65dbfbccf80774072e3a6bf2e1795c36e26cf
Reviewed-on: https://gerrit.instructure.com/33821
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Cody Cutrer <cody@instructure.com>
QA-Review: August Thornton <august@instructure.com>
Product-Review: Brian Palmer <brianp@instructure.com>
This commit is contained in:
Brian Palmer 2014-04-24 13:41:13 -06:00
parent d02411f444
commit d9de12fe95
1 changed files with 33 additions and 4 deletions

View File

@ -86,9 +86,11 @@ config.to_prepare do
ActiveSupport::JSON::Encoding.escape_html_entities_in_json = true
end
# this patch is perfectly placed to go in as soon as the PostgreSQLAdapter
# is required for the first time, but before it's actually used
# XXX: Rails3
# This patch is perfectly placed to go in as soon as the PostgreSQLAdapter
# is required for the first time, but before it's actually used.
#
# This patch won't be required in Rails >= 4.0.0, which supports params such as
# connect_timeout.
if CANVAS_RAILS2
Rails::Initializer.class_eval do
def initialize_database_with_postgresql_patches
@ -120,6 +122,33 @@ if CANVAS_RAILS2
end
alias_method_chain :initialize_database, :postgresql_patches
end
else
ActiveRecord::Base::ConnectionSpecification.class_eval do
def initialize_with_postgresql_patches(config, adapter_method)
initialize_without_postgresql_patches(config, adapter_method)
if adapter_method == "postgresql_connection" && !defined?(@@postgresql_patches_applied)
ActiveRecord::Base.class_eval do
def self.postgresql_connection(config) # :nodoc:
config = config.symbolize_keys
config[:user] ||= config.delete(:username) if config.key?(:username)
if config.key?(:database)
config[:dbname] = config[:database]
else
raise ArgumentError, "No database specified. Missing argument: database."
end
conn_params = config.slice(:host, :port, :dbname, :user, :password, :connect_timeout)
# The postgres drivers don't allow the creation of an unconnected PGconn object,
# so just pass a nil connection object for the time being.
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.new(nil, logger, [conn_params], config)
end
end
@@postgresql_patches_applied = true
end
end
alias_method_chain :initialize, :postgresql_patches
end
end
# We need to make sure that safe_yaml is loaded *after* the YAML engine
@ -191,4 +220,4 @@ unless CANVAS_RAILS2
end
config.exceptions_app = ExceptionsApp.new
end
end