Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address deprecation of ActiveRecord::Base.connection in Rails 7.2 #102

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

Loading