Skip to content

Fix deprecated ActiveRecord connection checkout - #490

Open
seahal wants to merge 1 commit into
voormedia:masterfrom
seahal:fix/scoped-connections
Open

seahal wants to merge 1 commit into
voormedia:masterfrom
seahal:fix/scoped-connections

Conversation

@seahal

@seahal seahal commented Oct 21, 2026

Copy link
Copy Markdown

Graphviz generation fails and tbls omits indexes with restricted permanent connection checkout

Thank you for maintaining rails-erd.

I encountered a connection checkout compatibility issue while generating diagrams with ActiveRecord 7.2.4 and have prepared a small fix with regression tests.

Problem

With ActiveRecord.permanent_connection_checkout = :disallowed, Graphviz PDF generation raises ActiveRecord::ActiveRecordError in RailsERD::Domain::Attribute#native_type.

The tbls generator encounters the same restriction while reading indexes. Its existing rescue StandardError returns an empty array, causing index metadata to be silently omitted.

The issue also occurs with :deprecated when ActiveRecord.deprecator.behavior = :raise.

An existing connection lease can mask the problem, so reproduction requires running the affected operations without a previously leased connection.

Root cause

Both Attribute#native_type and Diagram::Tbls#indexes_payload call Model.connection, which may perform an implicit permanent connection checkout.

ActiveRecord 7.2 introduced configuration to deprecate or disallow this behavior.

Proposed fix

Use model.connection_pool.with_connection at the two metadata access sites.

This scopes connection usage to the operation and avoids version-specific branches. The pool API is also available in ActiveRecord 7.0.

Verification

I reproduced the problem using independent SQLite processes on ActiveRecord 7.2.4, testing all six combinations of:

  • Checkout mode: true, :deprecated, :disallowed
  • Deprecation behavior: :raise, logging collector

Before the fix:

  • :deprecated with :raise prevented Graphviz generation and caused tbls indexes to be omitted.
  • Both :disallowed configurations produced the same failures.
  • :deprecated with logging allowed generation but emitted a deprecation warning.

After the fix, Graphviz generated PDFs with valid signatures and tbls preserved the unique index in all six configurations, without connection deprecation warnings.

Three regression tests also confirmed the original failure: one assertion failure and two errors without the fix on ActiveRecord 7.2.4.

The repaired implementation passes the full test suite on the locally tested ActiveRecord versions.

References

Would you be open to a pull request for this approach? I'm happy to adapt the implementation or tests to the project's conventions.

Since ActiveRecord 7.2, `Model.connection` performs an implicit permanent
checkout. Applications that set `ActiveRecord.permanent_connection_checkout`
to `:deprecated` get a deprecation warning (an exception when deprecations
raise), and `:disallowed` always raises. Diagram generation then aborted in
`Attribute#native_type`, and the tbls generator silently emitted no indexes
because its `rescue StandardError` swallowed the error.

Use `Model.connection_pool.with_connection` instead. It exists on every
supported ActiveRecord version, so no version branching is needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant