database_cleaner-active_record icon indicating copy to clipboard operation
database_cleaner-active_record copied to clipboard

NoMethodError: undefined method `lock' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x0000000105b1e008>

Open sshaw opened this issue 4 months ago • 3 comments

v2.2.2, mysql2 0.5.4, Ruby 2.7, Rails 5.

When I downgrade to 1.8 it works. Maybe a Rails version check is needed in dependency list for 2.X?

     NoMethodError:
       undefined method `lock' for #<ActiveRecord::ConnectionAdapters::Mysql2Adapter:0x0000000105b1e008>
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/database_cleaner-active_record-2.2.2/lib/database_cleaner/active_record/transaction.rb:20:in `block in clean'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/database_cleaner-active_record-2.2.2/lib/database_cleaner/active_record/transaction.rb:19:in `each'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/database_cleaner-active_record-2.2.2/lib/database_cleaner/active_record/transaction.rb:19:in `clean'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/database_cleaner-core-2.0.1/lib/database_cleaner/strategy.rb:32:in `cleaning'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/2.7.0/forwardable.rb:235:in `cleaning'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:34:in `block (2 levels) in cleaning'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/database_cleaner-core-2.0.1/lib/database_cleaner/cleaners.rb:35:in `cleaning'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/2.7.0/forwardable.rb:235:in `cleaning'
     # ./spec/rails_helper.rb:90:in `block (2 levels) in <top (required)>'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example.rb:457:in `instance_exec'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example.rb:457:in `instance_exec'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/hooks.rb:390:in `execute_with'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/hooks.rb:628:in `block (2 levels) in run_around_example_hooks_for'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example.rb:352:in `call'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/hooks.rb:629:in `run_around_example_hooks_for'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/hooks.rb:486:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example.rb:468:in `with_around_example_hooks'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example.rb:511:in `with_around_and_singleton_context_hooks'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example.rb:259:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:646:in `block in run_examples'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:642:in `map'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:642:in `run_examples'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:607:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:608:in `block in run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:608:in `map'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:608:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:608:in `block in run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:608:in `map'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/example_group.rb:608:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:121:in `block (3 levels) in run_specs'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:121:in `map'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:121:in `block (2 levels) in run_specs'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/configuration.rb:2068:in `with_suite_hooks'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:116:in `block in run_specs'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/reporter.rb:74:in `report'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:115:in `run_specs'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:89:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:71:in `run'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/lib/rspec/core/runner.rb:45:in `invoke'
     # /Users/sshaw/.asdf/installs/ruby/2.7.8/lib/ruby/gems/2.7.0/gems/rspec-core-3.11.0/exe/rspec:4:in `<top (required)>'
     # ./bin/rspec:17:in `load'
     # ./bin/rspec:17:in `<main>'

Config

config.use_transactional_fixtures = false  

config.before(:suite) do
  DatabaseCleaner.strategy = :transaction
  DatabaseCleaner.clean_with(:truncation)
end

config.around :each do |ex|
  DatabaseCleaner.strategy = :truncation if ex.metadata[:js]
  DatabaseCleaner.cleaning { ex.run }
  DatabaseCleaner.strategy = :transaction
end

sshaw avatar Aug 15 '25 17:08 sshaw

Thanks for reporting this. I think this is a recent change, so have you tried using v2.2.1?

etagwerker avatar Aug 15 '25 20:08 etagwerker

Hi, 2.2.1 works, yay, thanks. What change in 2.2.2 you think is the cause?

sshaw avatar Aug 17 '25 18:08 sshaw

I'm getting the same issue.

  • Rails 5.0.7.2
  • mysql2 0.5.7

Starts in database_cleaner-active_record version 2.2.0.

Locking to 2.1.0 resolves the issue for me.

RyanSnodgrass avatar Sep 26 '25 01:09 RyanSnodgrass