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

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

Open
troex opened this issue Aug 9, 2022 · 1 comment
Open

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

troex opened this issue Aug 9, 2022 · 1 comment

Comments

@troex
Copy link

troex commented Aug 9, 2022

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
Copy link
Author

troex commented Aug 9, 2022

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant