Skip to content

Commit 26ca4b6

Browse files
authored
Merge pull request rails#53870 from byroot/stmt-pool-delete-fix
StatementPool#delete don't assume the statement existed
2 parents 9bceca7 + 7c46bbc commit 26ca4b6

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

activerecord/lib/active_record/connection_adapters/statement_pool.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ def reset
5050
end
5151

5252
def delete(key)
53-
dealloc cache[key]
54-
cache.delete(key)
53+
if stmt = cache.delete(key)
54+
dealloc(stmt)
55+
end
56+
stmt
5557
end
5658

5759
private
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# frozen_string_literal: true
2+
3+
require "cases/helper"
4+
5+
module ActiveRecord
6+
module ConnectionAdapters
7+
class StatementPoolTest < ActiveRecord::TestCase
8+
class TestPool < StatementPool
9+
private
10+
def dealloc(stmt)
11+
raise ArgumentError unless stmt
12+
end
13+
end
14+
15+
setup do
16+
@pool = TestPool.new
17+
end
18+
19+
test "#delete doesn't call dealloc if the statement didn't exist" do
20+
stmt = Object.new
21+
sql = "SELECT 1"
22+
@pool[sql] = stmt
23+
assert_same stmt, @pool[sql]
24+
assert_same stmt, @pool.delete(sql)
25+
assert_nil @pool.delete(sql)
26+
end
27+
end
28+
end
29+
end

0 commit comments

Comments
 (0)