README.textile

Database Cleaner

Database Cleaner is a set of strategies for cleaning your database in Ruby.
The original use case was to ensure a clean state during tests. Each strategy
is a small amount of code but is code that is usually needed in any ruby app
that is testing with a database.

The ActiveRecord :deletion strategy is useful for when the :truncation strategy causes
locks (as reported by some Oracle DB users). The :deletion option has been reported to
be faster than :truncation in some cases as well. In general, the best approach is to use:transaction since it is the fastest.

Database Cleaner also includes a null strategy (that does no cleaning at all) which can be used
with any ORM library. You can also explicitly use it by setting your strategy to nil.

Dependencies

Because database_cleaner supports multiple ORMs, it doesn’t make sense to include all the dependencies
for each one in the gemspec. However, the DataMapper adapter does depend on dm-transactions. Therefore,
if you use DataMapper, you must include dm-transactions in your Gemfile/bundle/gemset manually.

(I should point out the truncation strategy will never truncate your schema_migrations table.)

Some strategies require that you call DatabaseCleaner.start before calling clean
(for example the :transaction one needs to know to open up a transaction). So
you would have:

require 'database_cleaner'
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.start # usually this is called in setup of a test
dirty_the_db
DatabaseCleaner.clean # cleanup of the test

At times you may want to do a single clean with one strategy. For example, you may want
to start the process by truncating all the tables, but then use the faster transaction
strategy the remaining time. To accomplish this you can say:

Minitest Example

DatabaseCleaner.strategy = :transaction
class MiniTest::Spec
before :each do
DatabaseCleaner.start
end
after :each do
DatabaseCleaner.clean
end
end

RSpec Example

RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end

Why?

One of my motivations for writing this library was to have an easy way to
turn on what Rails calls “transactional_fixtures” in my non-rails
ActiveRecord projects. For example, Cucumber ships with a Rails world that
will wrap each scenario in a transaction. This is great, but what if you are
using ActiveRecord in a non-rails project? You used to have to copy-and-paste
the needed code, but with DatabaseCleaner you can now say:

Common Errors

DatabaseCleaner is trying to use the wrong ORM

DatabaseCleaner has an autodetect mechanism where if you do not explicitly define your ORM it will use the first ORM it can detect that is loaded. Since ActiveRecord is the most common ORM used that is the first one checked for. Sometimes other libraries (e.g. ActiveAdmin) will load other ORMs (e.g. ActiveRecord) even though you are using a different ORM. This will result in DatabaseCleaner trying to use the wrong ORM (e.g. ActiveRecord) unless you explicitly define your ORM like so:

STDERR is being flooded when using Postgres

If you are using Postgres and have foreign key constraints, the truncation strategy will cause a lot of extra noise to appear on STDERR (in
the form of “NOTICE truncate cascades” messages). To silence these warnings set the following log level in your postgresql.conf file:

client_min_messages = warning

Debugging

In rare cases DatabaseCleaner will encounter errors that it will log. By default it uses STDOUT set to the ERROR level but you can configure this to use whatever Logger you desire. Here’s an example of using the Rails.logger in env.rb: