Skip to content

Creates more Cleaners than needed if multiple connections to DB #28

Description

@troex

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions