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 4, 2024
1 parent aafa5b2 commit bd4c01f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion database_cleaner-active_record.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "appraisal"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "mysql2"
# spec.add_development_dependency "mysql2"
spec.add_development_dependency "pg"
spec.add_development_dependency "sqlite3"
spec.add_development_dependency "trilogy"
Expand Down
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 Gem::Version.new("7.2") < ::ActiveRecord.version
connection_class.connection
else
connection_class.lease_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 Gem::Version.new("7.2") < ::ActiveRecord.version
connection_class.connection
else
connection_class.lease_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 bd4c01f

Please sign in to comment.