Repository navigation
Use transactional fixtures instead of DatabaseCleaner - #2543
Merged
Merged
Conversation
Rails releases a thread's connection at the end of each executor wrap, such as a request or an inline job, if its current transaction is not joinable. DatabaseCleaner opens its transaction with joinable: false, so the example's connection returned to the pool after every request. When another connection was checked in after it, the example continued on a connection without a transaction: records created before the request became invisible, and records created after it were committed and leaked into later examples. Rails' transactional tests pin the connection instead. Requests cannot release it, and the app server threads of JS specs share it, so JS specs no longer need truncation either. The paged suite never started a DatabaseCleaner transaction, so its non-JS specs committed their records until the next JS spec truncated the tables.
The threaded spec dates back to perma ids computed via MAX(perma_id) under an advisory lock, where only concurrent transactions could expose a clash. Since perma ids come from a counter on the entry, the clash happens when an entry instance with a stale counter increments it. Two instances loaded before creating files reproduce that deterministically and without truncating the database afterwards.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rails releases a thread's connection at the end of each executor wrap, such as a request or an inline job, if its current transaction is not joinable. DatabaseCleaner's transaction strategy opens its transaction with
joinable: false, so the example's connection returned to the pool after every request. When another connection was checked in after it (on Rails 8.1, for example, by the pool reaper's keepalive), the example continued on a connection without a transaction: records created before the request became invisible, and records created after it were committed and leaked into later examples. DatabaseCleaner/database_cleaner-active_record#134 describes the same problem.Rails' transactional tests pin the connection instead, so requests cannot release it, and the app server threads of JS specs share it. This lets all three suites drop DatabaseCleaner, including the truncation after JS specs. The paged suite never started a DatabaseCleaner transaction, so its non-JS specs committed their records until the next JS spec truncated the tables.
The threaded perma id spec dates back to perma ids computed via
MAX(perma_id)under an advisory lock and required truncation. With the counter on the entry, clashes come from incrementing a stale entry instance, which two instances loaded up front reproduce deterministically. Removingwith_lockfromAutoGeneratedPermaIdmakes the new spec fail.Ran the main (including JS), paged and scrolled suites locally on Rails 7.2.