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.
- Host: GitHub
- URL: https://github.com/DatabaseCleaner/database_cleaner-active_record
- Owner: DatabaseCleaner
- License: mit
- Created: 2016-05-16T01:50:39.000Z (over 9 years ago)
- Default Branch: main
- Last Pushed: 2025-07-30T18:00:03.000Z (5 months ago)
- Last Synced: 2025-11-07T12:11:19.630Z (about 1 month ago)
- Topics: activerecord, database-cleaner
- Language: Ruby
- Homepage:
- Size: 1.17 MB
- Stars: 70
- Watchers: 4
- Forks: 73
- Open Issues: 35
- Releases: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
README.md
Database Cleaner Adapter for ActiveRecord
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:
:onlyand:exceptmay 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 totruethis 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 tofalse. (Also, see the section on What strategy is fastest?) -
:cache_tables- When set totruethe 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 tofalseif (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 totrue. -
:reset_ids- Only valid for deletion strategy, when set totrueresets ids to 1 after each table is cleaned. -
:truncate_option- Only valid for PostgreSQL. Acceptable values are:restrictand: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
- Name: DatabaseCleaner
- Login: DatabaseCleaner
- Email:
- Kind: organization
- Description: A collection of projects to clean your databases using adapters and ORMs in Ruby
- Website:
- Location:
- Twitter:
- Company:
- Icon url: https://avatars.githubusercontent.com/u/7924760?v=4
- Repositories: 14
- Last ynced at: 2024-04-21T03:09:33.932Z
- Profile URL: https://github.com/DatabaseCleaner
GitHub Events
Total
- Issues event: 9
- Watch event: 11
- Delete event: 1
- Issue comment event: 26
- Push event: 14
- Pull request review event: 7
- Pull request review comment event: 2
- Pull request event: 13
- Fork event: 4
- Create event: 3
Last Year
- Issues event: 8
- Watch event: 7
- Delete event: 1
- Issue comment event: 25
- Push event: 14
- Pull request event: 12
- Pull request review event: 7
- Pull request review comment event: 2
- Fork event: 3
- Create event: 3
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 | 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:
- alise.lv: 2
- sidereel.com: 2
- easy.cz: 2
- harte-lyne.ca: 1
- reebosak.net: 1
- capterra.com: 1
- swiftype.com: 1
- rim.com: 1
- dryblis.com: 1
- mail.ru: 1
- continuity.net: 1
- 21croissants.com: 1
- fresha.com: 1
- selectrehab.com: 1
- livingsocial.com: 1
- radarservices.com: 1
- twilio.com: 1
- digitaria.com: 1
- aentos.es: 1
- patch.com: 1
- assetricity.com: 1
- delorum.com: 1
- tanda.co: 1
- korrelate.com: 1
- hisme.net: 1
- umn.edu: 1
- panorama9.com: 1
- joshsoftware.com: 1
- benmabey.com: 1
- botandrose.com: 1
- jonrowe.co.uk: 1
- ombulabs.com: 1
- mischievousmonkey.co.uk: 1
- me.com: 1
- eike.se: 1
- svenfuchs.com: 1
- petteriraty.eu: 1
- fidor.de: 1
- tryphon.org: 1
- netskin.com: 1
- jan.vlnas.cz: 1
- inodes.org: 1
- thoughtworks.com: 1
- venombytes.com: 1
- innovativetravel.eu: 1
- ubermind.com: 1
- everquote.com: 1
- leadfeeder.com: 1
- anicholson.net: 1
- yahoo.com.tw: 1
- on-site.com: 1
- skroutz.gr: 1
- joshualane.com: 1
- topagentnetwork.com: 1
- redhat.com: 1
- tech-angels.com: 1
- rhnh.net: 1
- bitfission.com: 1
- incrementalism.net: 1
- jaredbeck.com: 1
- united-signals.com: 1
- emanuel.industries: 1
- ironin.pl: 1
- riseup.net: 1
- chrismar035.com: 1
- whiskeyandgrits.net: 1
- dylanegan.com: 1
- codegourmet.de: 1
- greenhouse.io: 1
- lucasmourelle.com.ar: 1
- shevtsov.me: 1
- vuxu.org: 1
- lap.fi: 1
- knapo.net: 1
- nichol.ca: 1
- thinkpixellab.com: 1
- onemedical.com: 1
- airbnb.com: 1
- bluescripts.net: 1
- wearestac.com: 1
- friendsoftheweb.com: 1
- tanga.com: 1
- thekompanee.com: 1
- shopify.com: 1
- bionicpandagames.com: 1
- amc.org.au: 1
- glnetworks.de: 1
- plugintheworld.com: 1
- pamediakopes.gr: 1
- gametime.co: 1
- ownlocal.com: 1
- logi.cl: 1
- ntrglobal.com: 1
- sansan.com: 1
- hitwise.com: 1
- ombushop.com: 1
- leadtune.com: 1
- detailedbalance.net: 1
- reinaris.nl: 1
- sikachu.com: 1
- zendesk.com: 1
- jetthoughts.com: 1
- freelancing-gods.com: 1
- orien.io: 1
- platogo.com: 1
- papkovskiy.com: 1
- mikeshop.net: 1
- mattwynne.net: 1
- schellingpoint.com: 1
- alumni.nd.edu: 1
- navapbc.com: 1
- clabs.org: 1
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
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
- Total packages: 4
-
Total downloads:
- rubygems: 218,011,230 total
- Total docker downloads: 758,002,224
- Total dependent packages: 67 (may contain duplicates)
- Total dependent repositories: 7,389 (may contain duplicates)
- Total versions: 118
- Total maintainers: 2
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
- Homepage:
- Documentation: https://pkg.go.dev/github.com/databasecleaner/database_cleaner-active_record#section-documentation
- Licenses: mit
- Latest release: v2.2.2+incompatible (published 5 months ago)
- Last Synced: 2025-12-16T11:01:40.175Z (5 days ago)
- Versions: 47
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Forks count: 3.562%
- Stargazers count: 5.765%
- Average: 7.426%
- Dependent packages count: 9.576%
- Dependent repos count: 10.802%
proxy.golang.org: github.com/DatabaseCleaner/database_cleaner-active_record
- Homepage:
- Documentation: https://pkg.go.dev/github.com/DatabaseCleaner/database_cleaner-active_record#section-documentation
- Licenses: mit
- Latest release: v2.2.2+incompatible (published 5 months ago)
- Last Synced: 2025-12-16T11:01:38.421Z (5 days ago)
- Versions: 47
- Dependent Packages: 0
- Dependent Repositories: 0
-
Rankings:
- Forks count: 3.562%
- Stargazers count: 5.815%
- Average: 7.438%
- Dependent packages count: 9.576%
- Dependent repos count: 10.802%
Dependencies
- codecov >= 0 development
- simplecov >= 0 development
- database_cleaner-core >= 0
- rails ~> 5.2
- 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
- actions/checkout v3 composite
- ruby/setup-ruby v1 composite
- mysql 5.7 docker
- postgres * docker
Score: 30.760127003463666