A summary of data about the Ruby ecosystem.

https://github.com/DatabaseCleaner/database_cleaner-active_record

Strategies for cleaning databases using ActiveRecord. Can be used to ensure a clean state for testing.
https://github.com/DatabaseCleaner/database_cleaner-active_record

Keywords

activerecord database-cleaner

Keywords from Contributors

testing-tools mvc activejob rubygems rack rspec cucumber crash-reporting multithreading rubocop

Last synced: about 24 hours ago
JSON representation

Repository metadata

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

README.md

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 GitHub Issues.

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.

  • :reset_ids - Only valid for deletion strategy, when set to true resets ids to 1 after each table is cleaned.

  • :truncate_option - Only valid for PostgreSQL. Acceptable values are :restrict and :cascade. Default is :restrict

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:

Development

Check the CONTRIBUTE.md file for instructions running tests with and withour Docker.

COPYRIGHT

See LICENSE for details.


Owner metadata


GitHub Events

Total
Last Year

Committers metadata

Last synced: 6 days ago

Total Commits: 949
Total Committers: 223
Avg Commits per committer: 4.256
Development Distribution Score (DDS): 0.787

Commits in past year: 11
Committers in past year: 5
Avg Commits per committer in past year: 2.2
Development Distribution Score (DDS) in past year: 0.545

Name Email Commits
Ben Mabey b****n@b****m 202
Micah Geisel m****h@b****m 189
Jon Rowe h****o@j****k 74
Ernesto Tagwerker e****b@o****m 71
Jon Rowe j****n@m****k 17
snusnu g****a@g****m 12
Kostas Karachalios v****k@m****m 11
Eike Send m****l@e****e 11
sanemat o****n@g****m 10
Sebastian Skałacki s****e@g****m 9
Sven Fuchs me@s****m 9
stanislaw s****h@g****m 9
Petteri Räty p****u@p****u 8
Peter Goldstein p****n@g****m 8
Dieter Pisarewski d****i@g****m 6
Andreas Bühmann b****n@f****e 5
Timothée Peignier t****r@t****g 5
Judson n****y@g****m 5
Ethan e****n@d****l 5
Corin Langosch i****o@n****m 5
ezro o****r@e****z 5
David Barri j****y@g****m 4
Jan Vlnas g****t@j****z 4
John Ferlito j****f@i****g 4
Rob Hunter r****r@t****m 4
Sirko Sittig s****g@g****m 4
Tom Meier t****m@v****m 4
dsantosmerino d****o@g****m 4
Alessandro a****o@i****u 4
Greg Barnett g****t@u****m 4
and 193 more...

Committer domains:


Issue and Pull Request metadata

Last synced: 2 days ago

Total issues: 66
Total pull requests: 82
Average time to close issues: over 2 years
Average time to close pull requests: 6 months
Total issue authors: 63
Total pull request authors: 47
Average comments per issue: 3.05
Average comments per pull request: 1.56
Merged pull request: 41
Bot issues: 0
Bot pull requests: 1

Past year issues: 5
Past year pull requests: 11
Past year average time to close issues: 3 months
Past year average time to close pull requests: 2 months
Past year issue authors: 5
Past year pull request authors: 4
Past year average comments per issue: 1.0
Past year average comments per pull request: 1.36
Past year merged pull request: 5
Past year bot issues: 0
Past year bot pull requests: 0

More stats: https://issues.ecosyste.ms/repositories/lookup?url=https://github.com/DatabaseCleaner/database_cleaner-active_record

Top Issue Authors

  • tagliala (2)
  • botandrose (2)
  • kaorukobo (2)
  • kramuenke (1)
  • Fryie (1)
  • aosq (1)
  • mikebaldry (1)
  • hansondr (1)
  • joevandyk (1)
  • mathieujobin (1)
  • konalegi (1)
  • scottrobertson (1)
  • tobypinder (1)
  • dmolesUC (1)
  • md5 (1)

Top Pull Request Authors

  • etagwerker (9)
  • botandrose (7)
  • tagliala (4)
  • arielj (3)
  • petergoldstein (3)
  • thegeorgeous (2)
  • tsankaste (2)
  • mark-young-atg (2)
  • nnishimura (2)
  • ghiculescu (2)
  • estepnv (2)
  • olleolleolle (2)
  • eugeneius (2)
  • pat (2)
  • maniSHarma7575 (2)

Top Issue Labels

Top Pull Request Labels

  • dependencies (1)

Package metadata

gem.coop: database_cleaner-active_record

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

  • Homepage: https://github.com/DatabaseCleaner/database_cleaner-active_record
  • Documentation: http://www.rubydoc.info/gems/database_cleaner-active_record/
  • Licenses: MIT
  • Latest release: 2.2.2 (published 5 months ago)
  • Last Synced: 2025-12-19T07:46:23.060Z (2 days ago)
  • Versions: 12
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 109,143,340 Total
  • Docker Downloads: 379,001,112
  • Rankings:
    • Dependent repos count: 0.0%
    • Dependent packages count: 0.0%
    • Average: 0.154%
    • Downloads: 0.233%
    • Docker downloads count: 0.383%
  • Maintainers (2)
rubygems.org: database_cleaner-active_record

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

  • Homepage: https://github.com/DatabaseCleaner/database_cleaner-active_record
  • Documentation: http://www.rubydoc.info/gems/database_cleaner-active_record/
  • Licenses: MIT
  • Latest release: 2.2.2 (published 5 months ago)
  • Last Synced: 2025-12-16T11:01:39.293Z (5 days ago)
  • Versions: 12
  • Dependent Packages: 67
  • Dependent Repositories: 7,389
  • Downloads: 108,867,890 Total
  • Docker Downloads: 379,001,112
  • Rankings:
    • Dependent repos count: 0.375%
    • Downloads: 0.407%
    • Dependent packages count: 0.466%
    • Docker downloads count: 1.626%
    • Average: 2.372%
    • Forks count: 3.897%
    • Stargazers count: 7.461%
  • Maintainers (2)
proxy.golang.org: github.com/databasecleaner/database_cleaner-active_record

proxy.golang.org: github.com/DatabaseCleaner/database_cleaner-active_record


Dependencies

Gemfile rubygems
  • codecov >= 0 development
  • simplecov >= 0 development
  • database_cleaner-core >= 0
  • rails ~> 5.2
database_cleaner-active_record.gemspec rubygems
  • appraisal >= 0 development
  • bundler >= 0 development
  • mysql2 >= 0 development
  • pg >= 0 development
  • rake >= 0 development
  • rspec >= 0 development
  • sqlite3 >= 0 development
  • activerecord >= 5.a
  • database_cleaner-core ~> 2.0.0
.github/workflows/ci.yml actions
  • actions/checkout v3 composite
  • ruby/setup-ruby v1 composite
  • mysql 5.7 docker
  • postgres * docker

Score: 30.760127003463666