Skip to content

Use transactional fixtures instead of DatabaseCleaner - #2543

Merged
tf merged 2 commits into
codevise:masterfrom
tf:transactional-fixtures
Dec 1, 2026
Merged

tf merged 2 commits into
codevise:masterfrom
tf:transactional-fixtures

Conversation

@tf

@tf tf commented Dec 1, 2026

Copy link
Copy Markdown
Member

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. Removing with_lock from AutoGeneratedPermaId makes the new spec fail.

Ran the main (including JS), paged and scrolled suites locally on Rails 7.2.

tf added 2 commits December 1, 2026 11:01
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.
@tf tf added this to the v17.3 milestone Dec 1, 2026
@tf
tf merged commit 750e0df into codevise:master Dec 1, 2026
16 checks passed
@tf
tf deleted the transactional-fixtures branch December 1, 2026 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant