Skip to content

Commit 2c22251

Browse files
committed
release_connection after cleaning
For Rails >= 7.2, this library gets a connection via `#lease_connection`. But it never releases the connection. Release the connection back to the pool after `#clean`-ing. Rails >= 7.2 does connection health checks / reconnect on connection checkout. If the connection is never checked back into the pool, it will never get "repaired", which makes testing database connection issues tricky.
1 parent 912acbe commit 2c22251

8 files changed

Lines changed: 43 additions & 7 deletions

File tree

.github/workflows/ci.yml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,17 @@ jobs:
99
strategy:
1010
fail-fast: false
1111
matrix:
12-
ruby: ['3.3', '3.2', '3.1']
13-
rails: ['6.1', '7.0', '7.1', '7.2']
12+
ruby: ['3.4', '3.3', '3.2', '3.1']
13+
rails: ['6.1', '7.0', '7.1', '7.2', '8.0']
1414
channel: ['stable']
1515

1616
include:
1717
- ruby: 'ruby-head'
1818
rails: 'edge'
1919
channel: 'experimental'
20+
- ruby: 'ruby-head'
21+
rails: '8.0'
22+
channel: 'experimental'
2023
- ruby: 'ruby-head'
2124
rails: '7.2'
2225
channel: 'experimental'
@@ -35,14 +38,18 @@ jobs:
3538
channel: 'experimental'
3639

3740
exclude:
38-
- ruby: '3.3'
39-
rails: '7.0' # TODO: works on 7-0-stable branch, remove after a 7.0.x patch release
41+
- ruby: '3.4'
42+
rails: '6.1'
43+
4044
- ruby: '3.3'
4145
rails: '6.1'
4246

4347
- ruby: '3.2'
4448
rails: '6.1'
4549

50+
- ruby: '3.1'
51+
rails: '8.0'
52+
4653
continue-on-error: ${{ matrix.channel != 'stable' }}
4754

4855
env: # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps

database_cleaner-active_record.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
1818
spec.executables = []
1919
spec.require_paths = ["lib"]
2020

21-
spec.add_dependency "database_cleaner-core", "~>2.0.0"
21+
spec.add_dependency "database_cleaner-core", "~> 2.1.0"
2222
spec.add_dependency "activerecord", ">= 5.a"
2323

2424
spec.add_development_dependency "bundler"

gemfiles/rails_8.0.gemfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# This file was generated by Appraisal
2+
3+
source "https://rubygems.org"
4+
5+
gem "database_cleaner-core", git: "https://github.com/DatabaseCleaner/database_cleaner"
6+
gem "rails", "~> 8.0"
7+
8+
group :test do
9+
gem "simplecov", require: false
10+
gem "codecov", require: false
11+
end
12+
13+
gemspec path: "../"

lib/database_cleaner/active_record/deletion.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def clean
99
delete_tables(connection, tables_to_clean(connection))
1010
end
1111
end
12+
13+
connection_class.connection_pool.release_connection
1214
end
1315

1416
private

lib/database_cleaner/active_record/transaction.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def clean
2222
connection.rollback_transaction
2323
end
2424
end
25+
26+
connection_class.connection_pool.release_connection
2527
end
2628
end
2729
end

lib/database_cleaner/active_record/truncation.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def clean
2626
connection.truncate_tables(tables_to_clean(connection), { truncate_option: @truncate_option })
2727
end
2828
end
29+
30+
connection_class.connection_pool.release_connection
2931
end
3032

3133
private

spec/database_cleaner/active_record/truncation_spec.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@
106106
it "specifies cascade when truncating" do
107107
User.create!({name: 1})
108108

109-
expect(strategy.send(:connection)).to receive(:truncate_tables).with(['users', 'agents'], {truncate_option: :cascade})
109+
if helper.db == :postgres
110+
expect(strategy.send(:connection)).to receive(:truncate_tables).with(match_array(%w[users agents user_profiles]), {truncate_option: :cascade})
111+
else
112+
expect(strategy.send(:connection)).to receive(:truncate_tables).with(match_array(%w[users agents]), {truncate_option: :cascade})
113+
end
110114
strategy.clean
111115
end
112116
end
@@ -117,7 +121,11 @@
117121
it "specifies restrict when truncating" do
118122
User.create!
119123

120-
expect(strategy.send(:connection)).to receive(:truncate_tables).with(['users', 'agents'], {truncate_option: :restrict})
124+
if helper.db == :postgres
125+
expect(strategy.send(:connection)).to receive(:truncate_tables).with(match_array(%w[users agents user_profiles]), {truncate_option: :restrict})
126+
else
127+
expect(strategy.send(:connection)).to receive(:truncate_tables).with(match_array(%w[users agents]), {truncate_option: :restrict})
128+
end
121129
strategy.clean
122130
end
123131
end

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require "bundler/setup"
22

3+
require "logger" # Fix for Rails 7.0 tests
4+
35
if ENV['COVERAGE'] == 'true'
46
require "simplecov"
57

0 commit comments

Comments
 (0)