aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorKevin Buchanan <kevaustinbuch@gmail.com>2015-10-15 14:25:47 -0500
committerKevin Buchanan <kevaustinbuch@gmail.com>2015-11-06 16:14:24 -0600
commit2fe4586974689842dbdf3354678438618089accc (patch)
treedbef2dfcf006ecadf748de995c23dba857ac9d33 /activerecord
parent79d310fbd33c172f7f54d989191008581e028b64 (diff)
downloadrails-2fe4586974689842dbdf3354678438618089accc.tar.gz
rails-2fe4586974689842dbdf3354678438618089accc.tar.bz2
rails-2fe4586974689842dbdf3354678438618089accc.zip
Avoids mutating the original response in connection management middleware
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb7
-rw-r--r--activerecord/test/cases/connection_management_test.rb7
3 files changed, 15 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index c2cfdd5001..c0e3840b40 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,8 @@
+* Change connection management middleware to return a new response with
+ a body proxy, rather than mutating the original.
+
+ *Kevin Buchanan*
+
* Make `db:migrate:status` to render `1_some.rb` format migrate files.
These files are in `db/migrate`:
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
index 758eedc37d..486b7b6d25 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb
@@ -960,12 +960,11 @@ module ActiveRecord
def call(env)
testing = env['rack.test']
- response = @app.call(env)
- response[2] = ::Rack::BodyProxy.new(response[2]) do
+ status, headers, body = @app.call(env)
+ proxy = ::Rack::BodyProxy.new(body) do
ActiveRecord::Base.clear_active_connections! unless testing
end
-
- response
+ [status, headers, proxy]
rescue Exception
ActiveRecord::Base.clear_active_connections! unless testing
raise
diff --git a/activerecord/test/cases/connection_management_test.rb b/activerecord/test/cases/connection_management_test.rb
index dff6ea0fb0..cf8c4c688a 100644
--- a/activerecord/test/cases/connection_management_test.rb
+++ b/activerecord/test/cases/connection_management_test.rb
@@ -94,6 +94,13 @@ module ActiveRecord
assert response_body.respond_to?(:to_path)
assert_equal response_body.to_path, "/path"
end
+
+ test "doesn't mutate the original response" do
+ original_response = [200, {}, 'hi']
+ app = lambda { |_| original_response }
+ response_body = ConnectionManagement.new(app).call(@env)[2]
+ assert_equal original_response.last, 'hi'
+ end
end
end
end