Skip to content

Instantly share code, notes, and snippets.

@supernullset
Created January 21, 2013 21:00
Show Gist options
  • Save supernullset/4589331 to your computer and use it in GitHub Desktop.
Save supernullset/4589331 to your computer and use it in GitHub Desktop.
Multi-Database DatabaseCleaner setup
RSpec.configure do |config|
config.before :suite do
# set up a global cleaning strategy
DatabaseCleaner.clean_with(:truncation)
# AR specific strategy
DatabaseCleaner[:active_record].strategy = :truncation
# Explicitly tell DatabaseCleaner what connection is AR
DatabaseCleaner[:active_record, connection: :pediafed_test]
# Mongoid specific strategy
DatabaseCleaner[:mongoid].strategy = :truncation
# Explicitly tell DatabaseCleaner what connection is MongoDB
DatabaseCleaner[:mongoid, connection: :pediafed_test]
end
config.before :each do
DatabaseCleaner.start
end
config.after :each do
DatabaseCleaner.clean
end
end
@supernullset
Copy link
Author

Why?

After discovering a huge problem in my tests as I converted a number of the Pediafed models over to ActiveRecord/Postgres; Don and I discovered that DatabaseCleaner was not aware of how to handle multiple datastores from the get go, and was defaulting to the first one it encountered: AR.

Above is the solution. This is well documented in DatabaseCleaner's Documentation, but I figured I would document it for our future endeavors.

@phlipper
Copy link

Very helpful, thanks for sharing!

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