database_rewinder is a minimalist's tiny and ultra-fast database cleaner.
- Cleans up tables via DELETE SQL. No other strategies are implemented ATM
- Supports multiple databases
- Runs extremely fast 💨
database_rewinder memorizes every table name into which INSERT
SQL was performed during each test case.
Then it executes DELETE
SQL only against these tables when cleaning.
So, the more number of tables you have in your database, the more benefit you will get.
This strategy was originally devised and implemented by Shingo Morita (@eudoxa) at COOKPAD Inc.
-
ActiveRecord 3.2, 4.0, 4.1, 4.2
-
Ruby 2.0, 2.1, 2.2, 2.3
Add this line to your Gemfile's :test
group:
gem 'database_rewinder'
And then execute:
$ bundle
Do clean
in after(:each)
. And do clean_all
or clean_with
in before(:suite)
if you'd like to.
RSpec.configure do |config|
config.before :suite do
DatabaseRewinder.clean_all
# or
# DatabaseRewinder.clean_with :any_arg_that_would_be_actually_ignored_anyway
end
config.after :each do
DatabaseRewinder.clean
end
end
You can configure multiple DB connections to tell DatabaseRewinder to cleanup all of them after each test.
In order to add another connection, use DatabaseRewinder[]
method.
RSpec.configure do |config|
config.before(:suite) do
# simply give the DB connection names that are written in config/database.yml
DatabaseRewinder['test']
DatabaseRewinder['another_test_db']
# you could give the DB name with connection: key if you like
DatabaseRewinder[connection: 'yet_another_test_db']
# or with a meaningless something first, then {connection: DB_NAME} as the second argument (DatabaseCleaner compatible)
DatabaseRewinder[:active_record, connection: 'an_active_record_db']
DatabaseRewinder.clean_all
end
config.after(:each) do
DatabaseRewinder.clean
end
end
database_rewinder is designed to be almost compatible with database_cleaner. So the following code will probably let your existing app work under database_rewinder without making any change on your configuration.
DatabaseCleaner = DatabaseRewinder
Send me your pull requests.