All Projects → DatabaseCleaner → database_cleaner-active_record

DatabaseCleaner / database_cleaner-active_record

Licence: MIT license
Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing.

Programming Languages

ruby
36898 projects - #4 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to database cleaner-active record

n1 loader
Loader to solve N+1 issues for good. Highly recommended for GraphQL API.
Stars: ✭ 182 (+420%)
Mutual labels:  activerecord
sinatra-bootstrap
My opinionated Sinatra base application
Stars: ✭ 14 (-60%)
Mutual labels:  activerecord
activerecord-crate-adapter
Ruby on Rails ActiveRecord adapter for CrateDB
Stars: ✭ 27 (-22.86%)
Mutual labels:  activerecord
activerecord-migrations
A gem to simplify activerecord migrations in non-rails projects.
Stars: ✭ 15 (-57.14%)
Mutual labels:  activerecord
activerecord-debug errors
An extension of activerecord to display useful debug logs on errors
Stars: ✭ 23 (-34.29%)
Mutual labels:  activerecord
blade-jdbc
🐜 move to https://github.com/biezhi/anima
Stars: ✭ 36 (+2.86%)
Mutual labels:  activerecord
Occams Record
The missing high-efficiency query API for ActiveRecord
Stars: ✭ 240 (+585.71%)
Mutual labels:  activerecord
ebisu connection
EbisuConnection allows access to replica servers
Stars: ✭ 14 (-60%)
Mutual labels:  activerecord
attribute-depends-calculator
Automatically calculate a collection of depends attribute of ActiveRecord
Stars: ✭ 41 (+17.14%)
Mutual labels:  activerecord
departure
Percona's pt-online-schema-change runner for ActiveRecord migrations.
Stars: ✭ 86 (+145.71%)
Mutual labels:  activerecord
sql-builder
A simple SQL builder for generate SQL for non-ActiveRecord supports databases
Stars: ✭ 34 (-2.86%)
Mutual labels:  activerecord
yii2-activerecord-inheritance
ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework
Stars: ✭ 18 (-48.57%)
Mutual labels:  activerecord
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (-11.43%)
Mutual labels:  activerecord
query-objects-example
Example of rails app using Query Objects
Stars: ✭ 37 (+5.71%)
Mutual labels:  activerecord
no querying views
No more querying views in your Rails apps
Stars: ✭ 60 (+71.43%)
Mutual labels:  activerecord
Scenic
Scenic is maintained by Derek Prior, Caleb Hearth, and you, our contributors.
Stars: ✭ 2,856 (+8060%)
Mutual labels:  activerecord
active snapshot
Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
Stars: ✭ 67 (+91.43%)
Mutual labels:  activerecord
duck record
Used for creating virtual models like ActiveType or ModelAttribute does.
Stars: ✭ 23 (-34.29%)
Mutual labels:  activerecord
sinatra-dev-cheatsheet
A quick-and-dirty cheat sheet for creating HTML/CSS websites, and developing using Sinatra and ActiveRecord.
Stars: ✭ 44 (+25.71%)
Mutual labels:  activerecord
active dynamic
Gem that allows to add attributes to your active models dynamically
Stars: ✭ 61 (+74.29%)
Mutual labels:  activerecord

Database Cleaner Adapter for ActiveRecord

Tests Code Climate codecov

Clean your ActiveRecord databases with Database Cleaner.

See https://github.com/DatabaseCleaner/database_cleaner for more information.

For support or to discuss development please use the Google Group.

Installation

# Gemfile
group :test do
  gem 'database_cleaner-active_record'
end

Supported Strategies

Three strategies are supported:

  • Transaction (default)
  • Truncation
  • Deletion

What strategy is fastest?

For the SQL libraries the fastest option will be to use :transaction as transactions are simply rolled back. If you can use this strategy you should. However, if you wind up needing to use multiple database connections in your tests (i.e. your tests run in a different process than your application) then using this strategy becomes a bit more difficult. You can get around the problem a number of ways.

One common approach is to force all processes to use the same database connection (common ActiveRecord hack) however this approach has been reported to result in non-deterministic failures.

Another approach is to have the transactions rolled back in the application's process and relax the isolation level of the database (so the tests can read the uncommitted transactions).

An easier, but slower, solution is to use the :truncation or :deletion strategy.

So what is fastest out of :deletion and :truncation? Well, it depends on your table structure and what percentage of tables you populate in an average test. The reasoning is out of the scope of this README but here is a good SO answer on this topic for Postgres.

Some people report much faster speeds with :deletion while others say :truncation is faster for them. The best approach therefore is it try all options on your test suite and see what is faster.

Strategy configuration options

The transaction strategy accepts no options.

The truncation and deletion strategies may accept the following options:

  • :only and :except may take a list of table names:
# Only truncate the "users" table.
DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Truncation.new(only: ["users"])

# Delete all tables except the "users" table.
DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Deletion.new(except: ["users"])
  • :pre_count - When set to true this will check each table for existing rows before truncating or deleting it. This can speed up test suites when many of the tables are never populated. Defaults to false. (Also, see the section on What strategy is fastest?)

  • :cache_tables - When set to true the list of tables to truncate or delete from will only be read from the DB once, otherwise it will be read before each cleanup run. Set this to false if (1) you create and drop tables in your tests, or (2) you change Postgres schemas (ActiveRecord::Base.connection.schema_search_path) in your tests (for example, in a multitenancy setup with each tenant in a different Postgres schema). Defaults to true.

Adapter configuration options

#db defaults to the default ActiveRecord database, but can be specified manually in a few ways:

# ActiveRecord connection key
DatabaseCleaner[:active_record].db = :logs

# Back to default:
DatabaseCleaner[:active_record].db = :default

# Multiple databases can be specified:
DatabaseCleaner[:active_record, db: :default]
DatabaseCleaner[:active_record, db: :logs]

Common Errors

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

You can also add this parameter to your database.yml file:

test:
  adapter: postgresql
  # ...
  min_messages: WARNING  

COPYRIGHT

See LICENSE for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].