DEV.co
Open-Source Testing · amatsuda

database_rewinder

DatabaseRewinder is a lightweight Ruby gem that cleans up test database state by tracking which tables were modified during each test and only deleting from those tables. It supports Rails, multiple databases, and is designed to be a faster, simpler alternative to DatabaseCleaner.

Source: GitHub — github.com/amatsuda/database_rewinder
826
GitHub stars
92
Forks
Ruby
Primary language
MIT
License (OSI-approved)

Key facts

Objective fields from the source. Values we can't verify are shown as “Unknown” rather than guessed.

FieldValue
Repositoryamatsuda/database_rewinder
Owneramatsuda
Primary languageRuby
LicenseMIT — OSI-approved
Stars826
Forks92
Open issues16
Latest releaseUnknown
Last updated2025-07-23
Sourcehttps://github.com/amatsuda/database_rewinder

What database_rewinder is

DatabaseRewinder hooks into ActiveRecord to track INSERT operations per test, then batches DELETE statements into a single database call to clean only modified tables. It supports Rails 4.2–7.2, Ruby 2.4–3.3, and multiple database connections with optional transactional-test compatibility via a `multiple: false` flag.

Quickstart

Get the database_rewinder source

Clone the repository and explore it locally.

terminalbash
git clone https://github.com/amatsuda/database_rewinder.gitcd database_rewinder# follow the project's README for install & configuration

Need it deployed, integrated, or customized instead? DEV.co ships production installs.

Best use cases

High-table-count Rails test suites

Projects with large schemas benefit most from the selective-delete strategy; DatabaseRewinder skips untouched tables while DatabaseCleaner would scan all of them.

Capybara + JavaScript integration tests

When `use_transactional_tests` cannot work due to headless browser isolation, DatabaseRewinder provides fast cleanup without transaction rollback.

Multi-database Rails applications

Built-in support for multiple connections via `DatabaseRewinder[connection: 'name']` eliminates manual per-database cleanup logic.

Implementation considerations

  • Requires explicit RSpec configuration in `before(:suite)` and `after(:each)` hooks; copy-paste code from README is straightforward but non-standard.
  • MySQL users must choose between disabling `use_transactional_tests` (recommended) or accepting reduced performance with `multiple: false` option.
  • INSERT tracking is automatic but only effective if all test setup goes through INSERT SQL; raw SQL fixtures or bulk-load tools may not be tracked.
  • Verify ActiveRecord and Ruby version compatibility (4.2–7.2 and 2.4–3.3 stated as supported); test in CI before adopting.
  • The `DatabaseCleaner = DatabaseRewinder` compatibility alias allows drop-in replacement but does not guarantee behavioral parity for edge cases.

When to avoid it — and what to weigh

  • You already use transactional tests successfully — If `use_transactional_tests = true` works for your test suite, it is simpler and faster; DatabaseRewinder adds complexity without benefit.
  • You need advanced cleaning strategies — DatabaseRewinder only implements DELETE; if your tests require truncation, sequential resets, or non-SQL cleanup, use DatabaseCleaner or custom solutions.
  • MySQL with transactional tests enabled — The README documents potential timeout and connection issues when combining `use_transactional_tests = true` with MySQL; workarounds reduce performance gains.
  • You require recent security or feature patches — No releases since project creation; latest code is in main branch. Active development status unclear despite recent Git activity.

License & commercial use

MIT License. Permissive; allows commercial use, modification, and distribution with attribution and no warranty.

MIT is a permissive OSI license that explicitly permits commercial use and modification. No proprietary restrictions or commercial license required. Verify your legal review process for third-party dependency acceptance.

DEV.co evaluation signals

Editorial assessment — not user reviews. Directional, with an explicit confidence level.

SignalAssessment
MaintenanceModerate
DocumentationAdequate
License clarityClear
Deployment complexityLow
DEV.co fitGood
Assessment confidenceHigh
Security considerations

No documented security audit or vulnerability history. DELETE-based cleanup is lower-risk than truncation or transaction rollback. Verify that test database credentials and connection pooling do not leak or collide across test workers in parallel CI environments. No explicit statement on handling of sensitive data in test cleanup.

Alternatives to consider

DatabaseCleaner

Industry standard; supports truncate, delete, and transaction strategies; mature release history and extensive adoption. Trade-off: slower on large schemas but more flexible.

Rails transactional tests (use_transactional_tests = true)

Built-in; no dependency; simplest for unit and integration tests that do not require headless browsers. Trade-off: fails with Capybara JS mode and multi-threaded scenarios.

Custom test fixtures + database snapshots

Fine-grained control; supports non-SQL cleanup and complex data graphs. Trade-off: high maintenance burden and slower than specialized cleaners.

Software development agency

Build on database_rewinder with DEV.co software developers

DatabaseRewinder is best suited for Rails projects with large schemas or Capybara integration tests. Evaluate against DatabaseCleaner and transactional tests for your specific test patterns. Contact us for guidance on test infrastructure optimization.

Talk to DEV.co

Related open-source tools

Surfaced by semantic similarity across the DEV.co open-source index.

Related on DEV.co

Explore the category and the services that help you build with it.

database_rewinder FAQ

Is DatabaseRewinder safe to use in production?
No. It is designed exclusively for test databases. Never point it at a production database; it will execute DELETE statements. Always verify connection strings in CI and local configs.
Does it work with non-Rails projects?
No. It requires Rails' ActiveRecord gem and RSpec for hook integration. Standalone Ruby projects or non-Rails ORMs (e.g., Sequel, DataMapper) are not supported.
Why does the README recommend turning off use_transactional_tests?
DatabaseRewinder creates a new DB connection for deletion. With transactional tests enabled, this can cause query timeouts and lock contention, especially on MySQL. Disabling transactional tests avoids the conflict and unlocks DatabaseRewinder's performance.
How much faster is it than DatabaseCleaner?
Unknown; no published benchmarks. README claims 'ultra-fast' but speed depends on schema size, table count, and the cleaning strategy used by DatabaseCleaner (delete vs. truncate). Benchmark both in your test suite.

Work with a software development agency

Adopting database_rewinder is usually one piece of a larger software development effort. As a software development agency, DEV.co provides software development services and web development expertise — pairing senior software developers and web developers with your team to design, build, and operate open-source testing software in production.

Optimize Your Test Suite

DatabaseRewinder is best suited for Rails projects with large schemas or Capybara integration tests. Evaluate against DatabaseCleaner and transactional tests for your specific test patterns. Contact us for guidance on test infrastructure optimization.