{"id":13521391,"url":"https://github.com/amatsuda/database_rewinder","last_synced_at":"2025-05-14T14:08:48.054Z","repository":{"id":10591170,"uuid":"12803348","full_name":"amatsuda/database_rewinder","owner":"amatsuda","description":"minimalist's tiny and ultra-fast database cleaner","archived":false,"fork":false,"pushed_at":"2025-04-29T09:05:45.000Z","size":270,"stargazers_count":825,"open_issues_count":12,"forks_count":91,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-04-29T10:23:29.268Z","etag":null,"topics":["database","mysql","postgresql","rails","sqlite3","testing"],"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/amatsuda.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2013-09-13T06:51:54.000Z","updated_at":"2025-04-29T09:05:49.000Z","dependencies_parsed_at":"2023-02-12T11:01:22.622Z","dependency_job_id":"f869be2e-e3cd-4e64-9b3c-f95cb38048ed","html_url":"https://github.com/amatsuda/database_rewinder","commit_stats":{"total_commits":376,"total_committers":34,"mean_commits":"11.058823529411764","dds":"0.17021276595744683","last_synced_commit":"bc9a4b705e14ecc42579a4c2b67d27fa8bc5ec61"},"previous_names":[],"tags_count":37,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Fdatabase_rewinder","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Fdatabase_rewinder/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Fdatabase_rewinder/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amatsuda%2Fdatabase_rewinder/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amatsuda","download_url":"https://codeload.github.com/amatsuda/database_rewinder/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254159980,"owners_count":22024566,"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":["database","mysql","postgresql","rails","sqlite3","testing"],"created_at":"2024-08-01T06:00:33.782Z","updated_at":"2025-05-14T14:08:48.037Z","avatar_url":"https://github.com/amatsuda.png","language":"Ruby","readme":"# DatabaseRewinder\n\n[![Build Status](https://github.com/amatsuda/database_rewinder/actions/workflows/main.yml/badge.svg)](https://github.com/amatsuda/database_rewinder/actions)\n\ndatabase\\_rewinder is a minimalist's tiny and ultra-fast database cleaner.\n\n## Features\n\n* Cleans up tables via DELETE SQL. No other strategies are implemented ATM\n* Supports multiple databases\n* Runs extremely fast :dash:\n\n## Why is it fast?\n\ndatabase\\_rewinder memorizes every table name into which `INSERT` SQL was performed during each test case.\nThen it executes `DELETE` SQL only against these tables when cleaning.\nSo, the more number of tables you have in your database, the more benefit you will get.\nAlso, database\\_rewinder joins all `DELETE` SQL statements and casts it in one DB server call.\n\n### Credit\n\nThis strategy was originally devised and implemented by Shingo Morita (@eudoxa) at COOKPAD Inc.\n\n## Supported versions\n\n* ActiveRecord 4.2, 5.0, 5.1, 5.2, 6.0, 6.1, 7.0, 7.1, 7.2 (edge)\n\n* Ruby 2.4, 2.5, 2.6, 2.7, 3.0, 3.1, 3.2, 3.3 (trunk)\n\n## Installation\n\nAdd this line to your Gemfile's `:test` group:\n\n    gem 'database_rewinder'\n\nAnd then execute:\n\n    $ bundle\n\n## Usage\n\n### Basic configuration\n\nDo `clean` in `after(:each)`. And do `clean_all` or `clean_with` in `before(:suite)` if you'd like to.\n\n```ruby\nRSpec.configure do |config|\n  config.before(:suite) do\n    DatabaseRewinder.clean_all\n    # or\n    # DatabaseRewinder.clean_with :any_arg_that_would_be_actually_ignored_anyway\n  end\n\n  config.after(:each) do\n    DatabaseRewinder.clean\n  end\nend\n```\n\n### Dealing with multiple DBs\n\nYou can configure multiple DB connections to tell DatabaseRewinder to cleanup all of them after each test.\nIn order to add another connection, use `DatabaseRewinder[]` method.\n\n```ruby\nRSpec.configure do |config|\n  config.before(:suite) do\n    # simply give the DB connection names that are written in config/database.yml\n    DatabaseRewinder['test']\n    DatabaseRewinder['another_test_db']\n\n    # you could give the DB name with connection: key if you like\n    DatabaseRewinder[connection: 'yet_another_test_db']\n\n    # or with a meaningless something first, then {connection: DB_NAME} as the second argument (DatabaseCleaner compatible)\n    DatabaseRewinder[:active_record, connection: 'an_active_record_db']\n\n    DatabaseRewinder.clean_all\n  end\n\n  config.after(:each) do\n    DatabaseRewinder.clean\n  end\nend\n```\n\n### MySQL + use\\_transactional\\_tests Specific Problems\n\ndatabase\\_rewinder tries to create a new DB connection for deletion when you're running tests on MySQL.\nYou would occasionally hit some weird errors (e.g. query execution timeout) because of this, especially when your tests are run with the `use_transactional_tests` option enabled (which is Rails' default).\n\n#### 1. Properly understand what `use_transactional_tests` means, and consider turning it off\n\n`use_transactional_tests` is the option that surrounds each of your test case with a DB transaction to roll back all your test data after each test run.\nSo far as this works properly, you won't really need to use database\\_rewinder.\nHowever, this simple mechanism doesn't work well when you're running integration tests with capybara + js mode.\nIn cases of this situation, bundle database\\_rewinder and add the following configuration.\n\n```ruby\nRSpec.configure do |config|\n  config.use_transactional_tests = false\n\n  ...\nend\n```\n\n#### 2. Cleaning with `multiple: false` option\nIf you're really sure you need to keep using transactional tests + database\\_rewinder for some reason, then explicitly pass in `multiple: false` option to `DatabaseRewinder.clean_all` and `DatabaseRewinder.clean` invocations as follows. Note that you won't be able to get full performance merit that database\\_rewinder provides though.\n\n```ruby\nRSpec.configure do |config|\n  config.before :suite do\n    DatabaseRewinder.clean_all multiple: false\n  end\n\n  config.after :each do\n    DatabaseRewinder.clean multiple: false\n  end\nend\n```\n\n### Pro Tip\n\ndatabase\\_rewinder is designed to be almost compatible with database\\_cleaner.\nSo the following code will probably let your existing app work under database\\_rewinder without making any change on your configuration.\n\n```ruby\nDatabaseCleaner = DatabaseRewinder\n```\n\n## Contributing\n\nSend me your pull requests.\n","funding_links":[],"categories":["Ruby","Tools"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famatsuda%2Fdatabase_rewinder","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famatsuda%2Fdatabase_rewinder","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famatsuda%2Fdatabase_rewinder/lists"}