Skip to content

Commit 79b0876

Browse files
authored
Address deprecation of ActiveRecord::Base.connection in Rails 7.2 (#102)
1 parent 74a913a commit 79b0876

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

lib/database_cleaner/active_record/transaction.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ module DatabaseCleaner
44
module ActiveRecord
55
class Transaction < Base
66
def start
7+
connection = if ::ActiveRecord.version >= Gem::Version.new("7.2")
8+
connection_class.lease_connection
9+
else
10+
connection_class.connection
11+
end
12+
713
# Hack to make sure that the connection is properly set up before cleaning
8-
connection_class.connection.transaction {}
14+
connection.transaction {}
915

10-
connection_class.connection.begin_transaction joinable: false
16+
connection.begin_transaction joinable: false
1117
end
1218

1319

lib/database_cleaner/active_record/truncation.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ def clean
3030
private
3131

3232
def connection
33-
@connection ||= ConnectionWrapper.new(connection_class.connection)
33+
@connection ||= ConnectionWrapper.new(
34+
if ::ActiveRecord.version >= Gem::Version.new("7.2")
35+
connection_class.lease_connection
36+
else
37+
connection_class.connection
38+
end
39+
)
3440
end
3541

3642
def tables_to_truncate(connection)
@@ -246,4 +252,3 @@ def tables_with_schema
246252
private_constant :ConnectionWrapper
247253
end
248254
end
249-

0 commit comments

Comments
 (0)