{"id":20812387,"url":"https://github.com/databasecleaner/database_cleaner-active_record","last_synced_at":"2025-05-14T19:02:12.984Z","repository":{"id":12439820,"uuid":"58895439","full_name":"DatabaseCleaner/database_cleaner-active_record","owner":"DatabaseCleaner","description":"Strategies for cleaning databases using ActiveRecord.  Can be used to ensure a clean state for testing.","archived":false,"fork":false,"pushed_at":"2025-03-26T17:30:24.000Z","size":1207,"stargazers_count":63,"open_issues_count":39,"forks_count":66,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-03-31T14:00:06.281Z","etag":null,"topics":["activerecord","database-cleaner"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DatabaseCleaner.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-05-16T01:50:39.000Z","updated_at":"2025-01-02T05:07:42.000Z","dependencies_parsed_at":"2024-09-16T20:33:27.742Z","dependency_job_id":"f8706cb7-dfba-47e1-9f58-66c7b8db3cc3","html_url":"https://github.com/DatabaseCleaner/database_cleaner-active_record","commit_stats":{"total_commits":938,"total_committers":223,"mean_commits":4.20627802690583,"dds":0.7846481876332623,"last_synced_commit":"d34716a6d7b46884563d4f81d2ea9e40f288b6f9"},"previous_names":[],"tags_count":53,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DatabaseCleaner%2Fdatabase_cleaner-active_record","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DatabaseCleaner%2Fdatabase_cleaner-active_record/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DatabaseCleaner%2Fdatabase_cleaner-active_record/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DatabaseCleaner%2Fdatabase_cleaner-active_record/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DatabaseCleaner","download_url":"https://codeload.github.com/DatabaseCleaner/database_cleaner-active_record/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247819934,"owners_count":21001394,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","database-cleaner"],"created_at":"2024-11-17T20:53:50.601Z","updated_at":"2025-04-08T10:12:43.423Z","avatar_url":"https://github.com/DatabaseCleaner.png","language":"Ruby","readme":"# Database Cleaner Adapter for ActiveRecord\n\n[![Tests](https://github.com/DatabaseCleaner/database_cleaner-active_record/actions/workflows/ci.yml/badge.svg)](https://github.com/DatabaseCleaner/database_cleaner-active_record/actions/workflows/ci.yml)\n[![Code Climate](https://codeclimate.com/github/DatabaseCleaner/database_cleaner-active_record/badges/gpa.svg)](https://codeclimate.com/github/DatabaseCleaner/database_cleaner-active_record)\n[![codecov](https://codecov.io/gh/DatabaseCleaner/database_cleaner-active_record/branch/master/graph/badge.svg)](https://codecov.io/gh/DatabaseCleaner/database_cleaner-active_record)\n\nClean your ActiveRecord databases with Database Cleaner.\n\nSee https://github.com/DatabaseCleaner/database_cleaner for more information.\n\nFor support or to discuss development please use GitHub Issues.\n\n## Installation\n\n```ruby\n# Gemfile\ngroup :test do\n  gem 'database_cleaner-active_record'\nend\n```\n\n## Supported Strategies\n\nThree strategies are supported:\n\n* Transaction (default)\n* Truncation\n* Deletion\n\n## What strategy is fastest?\n\nFor 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.\n\nOne common approach is to force all processes to use the same database connection ([common ActiveRecord hack](http://blog.plataformatec.com.br/2011/12/three-tips-to-improve-the-performance-of-your-test-suite/)) however this approach has been reported to result in non-deterministic failures.\n\nAnother 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).\n\nAn easier, but slower, solution is to use the `:truncation` or `:deletion` strategy.\n\nSo 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](https://stackoverflow.com/questions/11419536/postgresql-truncation-speed/11423886#11423886).\n\nSome 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.\n\n## Strategy configuration options\n\nThe transaction strategy accepts no options.\n\nThe truncation and deletion strategies may accept the following options:\n\n* `:only` and `:except` may take a list of table names:\n\n```ruby\n# Only truncate the \"users\" table.\nDatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Truncation.new(only: [\"users\"])\n\n# Delete all tables except the \"users\" table.\nDatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::Deletion.new(except: [\"users\"])\n```\n\n* `: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?](#what-strategy-is-fastest))\n\n* `: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`.\n\n* `:reset_ids` - Only valid for deletion strategy, when set to `true` resets ids to 1 after each table is cleaned.\n\n## Adapter configuration options\n\n`#db` defaults to the default ActiveRecord database, but can be specified manually in a few ways:\n\n```ruby\n# ActiveRecord connection key\nDatabaseCleaner[:active_record].db = :logs\n\n# Back to default:\nDatabaseCleaner[:active_record].db = :default\n\n# Multiple databases can be specified:\nDatabaseCleaner[:active_record, db: :default]\nDatabaseCleaner[:active_record, db: :logs]\n```\n\n## Common Errors\n\n### STDERR is being flooded when using Postgres\n\nIf 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).\n\nTo silence these warnings set the following log level in your `postgresql.conf` file:\n\n```\nclient_min_messages = warning\n```\n\nYou can also add this parameter to your database.yml file:\n\n\u003cpre\u003e\ntest:\n  adapter: postgresql\n  # ...\n  min_messages: WARNING\n\u003c/pre\u003e\n\n## COPYRIGHT\n\nSee [LICENSE](LICENSE) for details.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatabasecleaner%2Fdatabase_cleaner-active_record","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdatabasecleaner%2Fdatabase_cleaner-active_record","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdatabasecleaner%2Fdatabase_cleaner-active_record/lists"}