Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@fguillen
Copy link

fguillen commented Mar 2, 2012

After two hours of trial and error.. this gist has saved my morning

@mgreenly
Copy link
Author

mgreenly commented Mar 2, 2012

@ggalindezb
Copy link

ggalindezb commented Mar 14, 2016

Thanks you so much! It didn't work out of the box for me, but it did after a bit of tweaking. BTW, this still works in recent (4.2.5.1) versions of Rails/DBCleaner.

# Double db config
other_db = Rails.configuration.database_configuration["other_#{Rails.env}"]
current_db = Rails.configuration.database_configuration[Rails.env]

# Truncate the db, track the transaction for the current test
config.before(:suite) do
  ActiveRecord::Base.establish_connection other_db
  DatabaseCleaner.strategy = :deletion
  ActiveRecord::Base.establish_connection current_db
  DatabaseCleaner.strategy = :deletion
end

config.before(:each) do
  ActiveRecord::Base.establish_connection other_db
  DatabaseCleaner.start
  ActiveRecord::Base.establish_connection current_db
  DatabaseCleaner.start
end

config.after(:each) do
  ActiveRecord::Base.establish_connection other_db
  DatabaseCleaner.clean_with(:deletion)
  ActiveRecord::Base.establish_connection current_db
  DatabaseCleaner.clean_with(:deletion)
end

@jason0415
Copy link

jason0415 commented Oct 13, 2017

@ggalindezb Works for me!

@luongm
Copy link

luongm commented Dec 7, 2018

this does not work for me (version 1.5.0). I have to do the following

DatabaseCleaner.add_cleaner :active_record, model: ActiveRecord::Base
DatabaseCleaner.add_cleaner :active_record, model: Notification

ActiveRecord::Base is using the default database.yml while Notification is connecting to a different database

@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

kuldeepaggarwal commented Mar 1, 2019

@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