SeededDeletion Strategy for DatabaseCleaner ActiveRecord
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 30/100
Research direction
Review the linked lib/database_cleaner/active_record/seeded_deletion.rb implementation and compare it with the repository's existing ActiveRecord deletion strategy and tests. Determine whether the strategy belongs in this repository or remains an extension, and define done as an agreed integration or documentation decision with appropriate database coverage.
Written by the indexing model from the issue text.
Description
Hello,
Because I had a similar use case as described in https://github.com/DatabaseCleaner/database_cleaner-active_record/issues/31, I've developed a solution for running UI tests with seeded data while only cleaning records created during testing.
The SeededDeletion Strategy preserves seed data while removing test-generated records: database_cleaner-activerecord-seeded_deletion.
How It Works
The SeededDeletion class implements simple logic:
- When start is called, it captures the maximum ID for each table
- When clean is called, it only deletes rows with IDs greater than the captured maximums
This effectively preserves all pre-existing seed data while cleaning up records created during test execution.
Details
Extends the standard Deletion strategy
Currently tested with PostgreSQL (though adaptable to other databases)
Uses SQL to identify tables with ID columns
Handles edge cases like tables without ID columns
Usage Example
DatabaseCleaner[:active_record].strategy = DatabaseCleaner::ActiveRecord::SeededDeletion.new
Future Improvements
The current implementation is functional but has room for optimization:
- Reduce the need to visit each table in separate queries
- Add support for other databases
- Improve performance for larger databases
- If it gets fast enough, the default implementation could add itself in the before/after in typical test frameworks such as minitest/rspec/cypress/etc.
Integration Question
Would the DatabaseCleaner team consider incorporating this strategy into the active_record repository, or should it remain as a standalone extension? I believe this addresses a common use case for teams working with seeded test environments.
Thank you.
- Dominant language
- Ruby
- Stars
- 76
- Forks
- 76
- PR merge metrics
- No merged PRs in 30d
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from DatabaseCleaner/database_cleaner-active_record
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
DatabaseCleaner/database_cleaner-active_record#127 · 3 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 45/100
-
Difficulty 5/5 Over a week Newbie friendliness 25/100
-
Difficulty 4/5 3-5 days Newbie friendliness 30/100
DatabaseCleaner/database_cleaner-active_record#110 · 1 comment ·
All issues in DatabaseCleaner/database_cleaner-active_record
Similar issues
-
security
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
grafana/pyroscope#5729 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
rubygems/rubygems.org#6895 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
samvera/hyku#3359 · 1 comment ·
Maintainers usually reply within 1 day
-
Child factories do not inherit traits from their parentPossibly taken @alassek claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
rom-rb/rom-factory#99 ·