Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #5727 from hone/ar_database_url_logging

ActiveRecord configuration logging
  • Loading branch information...
commit c4d31028f1c479c549b944610a52c2855d2e40e1 2 parents 8e315b0 + d46ad34
@tenderlove tenderlove authored
View
4 activerecord/lib/active_record/railtie.rb
@@ -72,9 +72,13 @@ class Railtie < Rails::Railtie
# and then establishes the connection.
initializer "active_record.initialize_database" do |app|
ActiveSupport.on_load(:active_record) do
+ db_connection_type = "DATABASE_URL"
unless ENV['DATABASE_URL']
+ db_connection_type = "database.yml"
self.configurations = app.config.database_configuration
end
+ Rails.logger.info "Connecting to database specified by #{db_connection_type}"
+
establish_connection
end
end
View
62 railties/test/application/initializers/active_record_test.rb
@@ -0,0 +1,62 @@
+require "isolation/abstract_unit"
+require "rack/test"
+
+module ApplicationTests
+ class ActiveRecordTest < Test::Unit::TestCase
+ include ActiveSupport::Testing::Isolation
+ include Rack::Test::Methods
+
+ def setup
+ @database_url = ENV['DATABASE_URL']
+ ENV.delete('DATABASE_URL')
+ build_app
+ boot_rails
+ end
+
+ def teardown
+ teardown_app
+ ENV['DATABASE_URL'] = @database_url
+ end
+
+ test "blows up when no DATABASE_URL env var or database.yml" do
+ FileUtils.rm_rf("#{app_path}/config/database.yml")
+ boot_rails
+ simple_controller
+
+ get '/foo'
+ assert last_response.body.include?("We're sorry, but something went wrong (500)")
+ end
+
+ test "uses DATABASE_URL env var when config/database.yml doesn't exist" do
+ database_path = "/db/foo.sqlite3"
+ FileUtils.rm_rf("#{app_path}/config/database.yml")
+ ENV['DATABASE_URL'] = "sqlite3://#{database_path}"
+ simple_controller
+
+ get '/foo'
+ assert_equal 'foo', last_response.body
+
+ # clean up
+ FileUtils.rm("#{app_path}/#{database_path}")
+ end
+
+ test "DATABASE_URL env var takes precedence over config/database.yml" do
+ database_path = "/db/foo.sqlite3"
+ ENV['DATABASE_URL'] = "sqlite3://#{database_path}"
+ simple_controller
+
+ get '/foo'
+ assert File.read("#{app_path}/log/production.log").include?("DATABASE_URL")
+
+ # clean up
+ FileUtils.rm("#{app_path}/#{database_path}")
+ end
+
+ test "logs the use of config/database.yml" do
+ simple_controller
+
+ get '/foo'
+ assert File.read("#{app_path}/log/production.log").include?("database.yml")
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.