Skip to content

fix: avoid eager DB connection when loading Journaled::Outbox::Event#71

Merged
smudge merged 3 commits intomasterfrom
fix-load-order
Apr 20, 2026
Merged

fix: avoid eager DB connection when loading Journaled::Outbox::Event#71
smudge merged 3 commits intomasterfrom
fix-load-order

Conversation

@smudge
Copy link
Copy Markdown
Member

@smudge smudge commented Apr 20, 2026

Problem

Journaled::Outbox::Event uses attribute :event_data, :json in its class body. In Rails 7.2+, passing a symbol to attribute triggers ActiveRecord::Type.adapter_name_from(klass)klass.connection_db_config at class load time, requiring an active connection pool to resolve the adapter-specific type implementation.

This prevents the class from being loaded without a database connection — for example, during tapioca gems shim generation, where tooling tries to eager-load autoloaded constants to discover their shapes.

Fix

Replace the symbol with a concrete type object:

# before
attribute :event_data, :json

# after
attribute :event_data, ActiveRecord::Type::Json.new

ActiveRecord::Type::Json is a portable base type (available since Rails 5.2) that serializes/deserializes JSON without needing to know the adapter. Behavior on PostgreSQL is identical.

Test plan

  • New spec asserts attribute_types['event_data'] is an ActiveRecord::Type::Json instance, serving as a regression guard against re-introducing symbol-based type resolution
  • Existing outbox event specs (PostgreSQL only) continue to pass

/no-platform

🤖 Generated with Claude Code

@smudge smudge requested a review from a team as a code owner April 20, 2026 15:12
Comment on lines +7 to +9
it 'uses a concrete type object so the class can be loaded without a database connection' do
expect(described_class.attribute_types['event_data']).to be_a(ActiveRecord::Type::Json)
end
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it possible to directly test that db connection isn't required at class load time?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was finding it tricky to reproduce the load order issue within the gem's own spec, but lemme take another stab at this before I merge anything.

effron
effron previously approved these changes Apr 20, 2026
Copy link
Copy Markdown
Contributor

@effron effron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domainLGTM!

Passing a symbol to `attribute` (e.g. `attribute :event_data, :json`)
causes Rails 7.2+ to call `ActiveRecord::Type.adapter_name_from` at
class load time, which requires an active connection pool to resolve the
adapter-specific type. This prevents the class from being autoloaded
without a database connection (e.g. during tapioca gem shim generation).

Replace with a concrete type object (`ActiveRecord::Type::Json.new`),
which bypasses adapter resolution entirely and has identical behavior
on PostgreSQL.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@effron I can confirm that this test reproduces the failure when I revert the changes to outbox/event.rb.

Copy link
Copy Markdown
Contributor

@effron effron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

domainLGTM

@smudge smudge merged commit cf9b622 into master Apr 20, 2026
29 checks passed
@smudge smudge deleted the fix-load-order branch April 20, 2026 19:31
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.

2 participants