Skip to content

Instantly share code, notes, and snippets.

@mgreenly
Created July 27, 2011 13:11
Show Gist options
  • Save mgreenly/1109325 to your computer and use it in GitHub Desktop.
Save mgreenly/1109325 to your computer and use it in GitHub Desktop.
database cleaner multiple connections single orm outside of rails
RSpec.configure do |config|
config.before(:suite) do
ActiveRecord::Base.establish_connection database['one']
DatabaseCleaner.strategy = :deletion
ActiveRecord::Base.establish_connection config.database['two']
DatabaseCleaner.strategy = :deletion
end
config.before(:each) do
ActiveRecord::Base.establish_connection database['one']
DatabaseCleaner.start
ActiveRecord::Base.establish_connection database['two']
DatabaseCleaner.start
end
config.after(:each) do
ActiveRecord::Base.establish_connection database['one']
DatabaseCleaner.clean
ActiveRecord::Base.establish_connection database['two']
DatabaseCleaner.clean
end
@Ununuk
Copy link

Ununuk commented Feb 26, 2019

@mgreenly it works for me. Stack:
rails 5.2.1
database_cleaner 1.7.0
rspec 3.7.1

Thank you! 👍

@kuldeepaggarwal
Copy link

@luongm Worked like a charm!!!

@david-pm
Copy link

david-pm commented Mar 11, 2020

@luongm solution is what worked for me, as well:
rails 6.0.2.1
database_cleaner 1.8.3
rspec 3.9.0

Worth mentioning:

Calling `DatabaseCleaner.add_cleaner` is deprecated, and will be removed in database_cleaner 2.0. Use `DatabaseCleaner.[]`, instead.

So, I did:

    DatabaseCleaner.[] :active_record, model: ActiveRecord::Base
    DatabaseCleaner.[] :active_record, model: SomeSpecificModel

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment