# 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. ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported. [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner) Here is an overview of the strategies supported for each library:
ORM | Truncation | Transaction | Deletion |
---|---|---|---|
ActiveRecord | Yes | Yes | Yes |
DataMapper | Yes | Yes | No |
CouchPotato | Yes | No | No |
MongoMapper | Yes | No | No |
Mongoid | Yes | No | No |
Sequel | Yes | Yes | No |
Redis | Yes | No | No |
Ohm | Yes | No | No |
Neo4j | Yes | Yes* | Yes* |
Driver | Truncation | Transaction | Deletion |
---|---|---|---|
Mongo | Yes | No | No |
Moped | Yes | No | No |
ORM | How to access | Notes |
---|---|---|
Active Record | DatabaseCleaner[:active_record] |
Connection specified as :symbol keys, loaded from config/database.yml . You may also pass in the ActiveRecord model under the :model key. |
Data Mapper | DatabaseCleaner[:data_mapper] |
Connection specified as :symbol keys, loaded via Datamapper repositories |
Mongo Mapper | DatabaseCleaner[:mongo_mapper] |
Multiple connections not yet supported |
Mongoid | DatabaseCleaner[:mongoid] |
Multiple databases supported for Mongoid 3. Specify DatabaseCleaner[:mongoid, {:connection => :db_name}] |
Moped | DatabaseCleaner[:moped] |
It is necessary to configure database name with DatabaseCleaner[:moped].db = db_name otherwise name `default` will be used. |
Couch Potato | DatabaseCleaner[:couch_potato] |
Multiple connections not yet supported |
Sequel | DatabaseCleaner[:sequel] |
Multiple databases supported; specify DatabaseCleaner[:sequel, {:connection => Sequel.connect(uri)}] |
Redis | DatabaseCleaner[:redis] |
Connection specified as Redis URI |
Ohm | DatabaseCleaner[:ohm] |
Connection specified as Redis URI |
Neo4j | DatabaseCleaner[:neo4j] |
Database type and path(URI) DatabaseCleaner[:neo4j, connection: {type: :server_db, path: 'http://localhost:7475'}]. |
test: adapter: postgresql # ... min_messages: WARNING### Nothing happens in JRuby with Sequel using transactions Due to an inconsistency in JRuby's implementation of Fibers, Sequel gives a different connection to `DatabaseCleaner.start` than is used for tests run between `.start` and `.clean`. This can be worked around by running your tests in a block like `DatabaseCleaner.cleaning { run_my_tests }` instead, which does not use Fibers. ### Model fails to load with Neo4j using transactions When you are using [neo4j](https://github.com/neo4jrb/neo4j) gem it creates schema and reads indexes upon loading models. These operations can't be done during a transaction. You have to preload your models before DatabaseCleaner starts a transaction. Add to your rails_helper or spec_helper after requiring database_cleaner: ```ruby require 'database_cleaner' Dir["#{Rails.root}/app/models/**/*.rb"].each do |model| load model end ``` ## 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`: ```ruby DatabaseCleaner.logger = Rails.logger ``` ## COPYRIGHT Copyright (c) 2014 Ben Mabey. See LICENSE for details.