diff options
-rw-r--r-- | activerecord/lib/active_record/railtie.rb | 4 | ||||
-rw-r--r-- | railties/test/application/initializers/active_record_test.rb | 62 |
2 files changed, 66 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/railtie.rb b/activerecord/lib/active_record/railtie.rb index 96dc258288..13b7c6e214 100644 --- a/activerecord/lib/active_record/railtie.rb +++ b/activerecord/lib/active_record/railtie.rb @@ -72,9 +72,13 @@ module ActiveRecord # 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 diff --git a/railties/test/application/initializers/active_record_test.rb b/railties/test/application/initializers/active_record_test.rb new file mode 100644 index 0000000000..edf78a8a0a --- /dev/null +++ b/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 |