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

Creates more Cleaners than needed if multiple connections to DB

Open troex opened this issue 2 years ago • 1 comments

I have next code to init cleaners:

  ::Sequel::DATABASES.each do |db|
    DatabaseCleaner[:sequel, db: db]
  end
::Sequel::DATABASES.count
> 3
DatabaseCleaner.cleaners.count
> 4
DatabaseCleaner.cleaners.values.first.start
RuntimeError: As you have more than one active sequel database you have to specify the one to use manually!
from /home/troex/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/database_cleaner-sequel-2.0.2/lib/database_cleaner/sequel/base.rb:8:in `db'
> DatabaseCleaner.cleaners
=> {[:sequel, {}]=>
  #<DatabaseCleaner::Cleaner:0x00007f54e5051cc0
   @db=:default,
   @orm=:sequel,
   @strategy=#<DatabaseCleaner::Sequel::Transaction:0x00007f54e5051568 @db=:default>>,
 [:sequel, {:db=>#<Sequel::Postgres::Database: "postgres://localhost/attribution-tracking-partition-test" {:max_connections=>4}>}]=>
  #<DatabaseCleaner::Cleaner:0x00007f54e5054df8
   @db=#<Sequel::Postgres::Database: "postgres://localhost/attribution-tracking-partition-test" {:max_connections=>4}>,
   @orm=:sequel,
   @strategy=#<DatabaseCleaner::NullStrategy:0x00007f54e5054ce0>>,
 [:sequel, {:db=>#<Sequel::Postgres::Database: "postgres://localhost/attribution-test" {:max_connections=>4}>}]=>
  #<DatabaseCleaner::Cleaner:0x00007f54e5054ab0
   @db=#<Sequel::Postgres::Database: "postgres://localhost/attribution-test" {:max_connections=>4}>,
   @orm=:sequel,
   @strategy=#<DatabaseCleaner::NullStrategy:0x00007f54e5054a88>>,
 [:sequel, {:db=>#<Sequel::Postgres::Database: "postgres://localhost/attribution-reporting-test" {:max_connections=>4}>}]=>
  #<DatabaseCleaner::Cleaner:0x00007f54e50548f8
   @db=#<Sequel::Postgres::Database: "postgres://localhost/attribution-reporting-test" {:max_connections=>4}>,
   @orm=:sequel,
   @strategy=#<DatabaseCleaner::NullStrategy:0x00007f54e50548d0>>}

The first cleaner fails because it's not configured, I think it's the issue because of by default it trys to bind for first database, but it does not if there is more than one database in Sequel it should not set at all.

https://github.com/DatabaseCleaner/database_cleaner-sequel/blob/main/lib/database_cleaner/sequel.rb#L7 https://github.com/DatabaseCleaner/database_cleaner-sequel/blob/main/lib/database_cleaner/sequel/base.rb#L8

as a workaround have to use next code in rspec:

  conf.before(:each) do
    DatabaseCleaner.cleaners.values.each do |cleaner|
      next if cleaner.db == :default
      cleaner.start
    end
  end

  conf.append_after(:each) do
    DatabaseCleaner.cleaners.values.each do |cleaner|
      next if cleaner.db == :default
      cleaner.clean
    end
  end

troex avatar Aug 09 '22 20:08 troex

the simpler workaround seams just to remove default cleaner after setting up needed cleaners:

DatabaseCleaner.cleaners.delete([:sequel, {}])

troex avatar Aug 09 '22 20:08 troex