aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorPiotr Sarnacki <drogus@gmail.com>2013-06-20 22:47:23 -0700
committerPiotr Sarnacki <drogus@gmail.com>2013-06-20 22:47:23 -0700
commit5ac22989d3f18bd3bf1011ca5ffd0a045e394d2c (patch)
tree8fb3c5251eb47916fbc3d72e13395d79945875be /activerecord
parent3c0ef057f1af4d39379ba572139aa104c3673779 (diff)
parent0f3aadae3b639ce56dba9c1bcf8a3c3646ccc93b (diff)
downloadrails-5ac22989d3f18bd3bf1011ca5ffd0a045e394d2c.tar.gz
rails-5ac22989d3f18bd3bf1011ca5ffd0a045e394d2c.tar.bz2
rails-5ac22989d3f18bd3bf1011ca5ffd0a045e394d2c.zip
Merge pull request #11014 from senny/10936_inspect_does_not_crash_without_connection
`inspect` for AR model classes does not initiate a new connection.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md11
-rw-r--r--activerecord/lib/active_record/core.rb2
-rw-r--r--activerecord/test/cases/invalid_connection_test.rb20
3 files changed, 33 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index 128d1b3e7f..b24222b700 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,14 @@
+* `inspect` on Active Record model classes does not initiate a
+ new connection. This means that calling `inspect`, when the
+ database is missing, will no longer raise an exception.
+ Fixes #10936.
+
+ Example:
+
+ Author.inspect # => "Author(no database connection)"
+
+ *Yves Senn*
+
* Handle single quotes in PostgreSQL default column values.
Fixes #10881.
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb
index ba053700f2..f0141aaaab 100644
--- a/activerecord/lib/active_record/core.rb
+++ b/activerecord/lib/active_record/core.rb
@@ -123,6 +123,8 @@ module ActiveRecord
super
elsif abstract_class?
"#{super}(abstract)"
+ elsif !connected?
+ "#{super}(no database connection)"
elsif table_exists?
attr_list = columns.map { |c| "#{c.name}: #{c.type}" } * ', '
"#{super}(#{attr_list})"
diff --git a/activerecord/test/cases/invalid_connection_test.rb b/activerecord/test/cases/invalid_connection_test.rb
new file mode 100644
index 0000000000..f6fe7f0d7d
--- /dev/null
+++ b/activerecord/test/cases/invalid_connection_test.rb
@@ -0,0 +1,20 @@
+require "cases/helper"
+require "models/bird"
+
+class TestAdapterWithInvalidConnection < ActiveRecord::TestCase
+ self.use_transactional_fixtures = false
+
+ def setup
+ @spec = ActiveRecord::Base.connection_config
+ non_existing_spec = {adapter: @spec[:adapter], database: "i_do_not_exist"}
+ ActiveRecord::Base.establish_connection(non_existing_spec)
+ end
+
+ def teardown
+ ActiveRecord::Base.establish_connection(@spec)
+ end
+
+ test "inspect on Model class does not raise" do
+ assert_equal "Bird(no database connection)", Bird.inspect
+ end
+end