Created
July 27, 2011 13:11
-
-
Save mgreenly/1109325 to your computer and use it in GitHub Desktop.
database cleaner multiple connections single orm outside of rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Author
mgreenly
commented
Mar 2, 2012
via email
Glad I could help. I seem to remember that problem burning up a few hours
when I first ran into it.
On Fri, Mar 2, 2012 at 7:02 AM, Fernando Guillen < ***@***.*** > wrote:
After two hours of trial and error.. this gist has saved my morning
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1109325
##
Michael Greenly
http://logic-refinery.com
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
@ggalindezb Works for me!
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
@mgreenly it works for me. Stack:
rails 5.2.1
database_cleaner 1.7.0
rspec 3.7.1
Thank you! 👍
@luongm Worked like a charm!!!
@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