aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-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