Create a gist now

Instantly share code, notes, and snippets.

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

This comment has been minimized.

Show comment Hide comment
@fguillen

fguillen Mar 2, 2012

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

fguillen commented Mar 2, 2012

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

@mgreenly

This comment has been minimized.

Show comment Hide comment
@mgreenly

mgreenly Mar 2, 2012

Owner

mgreenly commented Mar 2, 2012

@ggalindezb

This comment has been minimized.

Show comment Hide comment
@ggalindezb

ggalindezb 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

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

This comment has been minimized.

Show comment Hide comment
@jason0415

jason0415 Oct 13, 2017

@ggalindezb Works for me!

@ggalindezb Works for me!

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