# Database Cleaner [![Build Status](https://travis-ci.org/DatabaseCleaner/database_cleaner.svg?branch=master)](https://travis-ci.org/DatabaseCleaner/database_cleaner) [![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/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. ## Gem Setup ```ruby # Gemfile group :test do gem 'database_cleaner' end ``` ## Supported Databases, Libraries and Strategies ActiveRecord, DataMapper, Sequel, MongoMapper, Mongoid, CouchPotato, Ohm and Redis are supported. 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 | Yes |
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 ``` ## Safeguards DatabaseCleaner comes with safeguards against: * Running in production (checking for `ENV`, `RACK_ENV`, and `RAILS_ENV`) * Running against a remote database (checking for a `DATABASE_URL` that does not include `localhost`, `.local` or `127.0.0.1`) Both safeguards can be disabled separately as follows. Using environment variables: ``` export DATABASE_CLEANER_ALLOW_PRODUCTION=true export DATABASE_CLEANER_ALLOW_REMOTE_DATABASE_URL=true ``` In Ruby: ```ruby DatabaseCleaner.allow_production = true DatabaseCleaner.allow_remote_database_url = true ``` In Ruby, a URL whitelist can be specified. When specified, DatabaseCleaner will only allow `DATABASE_URL` to be equal to one of the values specified in the url whitelist like so: ```ruby DatabaseCleaner.url_whitelist = ['postgres://postgres@localhost', 'postgres://foo@bar'] ``` ## 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.