Skip to content

Commit

Permalink
Address deprecation of ActiveRecord::Base.connection in Rails 7.2 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuay03 committed Jun 5, 2024
1 parent 74a913a commit 79b0876
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 8 additions & 2 deletions lib/database_cleaner/active_record/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ module DatabaseCleaner
module ActiveRecord
class Transaction < Base
def start
connection = if ::ActiveRecord.version >= Gem::Version.new("7.2")
connection_class.lease_connection
else
connection_class.connection
end

# Hack to make sure that the connection is properly set up before cleaning
connection_class.connection.transaction {}
connection.transaction {}

connection_class.connection.begin_transaction joinable: false
connection.begin_transaction joinable: false
end


Expand Down
9 changes: 7 additions & 2 deletions lib/database_cleaner/active_record/truncation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ def clean
private

def connection
@connection ||= ConnectionWrapper.new(connection_class.connection)
@connection ||= ConnectionWrapper.new(
if ::ActiveRecord.version >= Gem::Version.new("7.2")
connection_class.lease_connection
else
connection_class.connection
end
)
end

def tables_to_truncate(connection)
Expand Down Expand Up @@ -246,4 +252,3 @@ def tables_with_schema
private_constant :ConnectionWrapper
end
end

0 comments on commit 79b0876

Please sign in to comment.