Fix ArrayHandlerPatch to support multi-database configurations#128
Open
biancapower wants to merge 1 commit intoGeorgeKaraszi:mainfrom
Open
Fix ArrayHandlerPatch to support multi-database configurations#128biancapower wants to merge 1 commit intoGeorgeKaraszi:mainfrom
biancapower wants to merge 1 commit intoGeorgeKaraszi:mainfrom
Conversation
Resolve connection through the Arel table's owning model class instead of hardcoding ActiveRecord::Base.connection, which raises ConnectionNotDefined in multi-database setups where pools are registered on abstract subclasses.
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.
Problem
ArrayHandlerPatch#callhardcodesActiveRecord::Base.connectionto check the schema cache for PostgreSQL array columns. In Rails 6.1+ multi-database setups whereconnects_tois declared on abstract subclasses (notActiveRecord::Baseitself),ActiveRecord::Basehas no connection pool registered. This raisesActiveRecord::ConnectionNotDefinedon any.where()with array values.Minimal reproduction:
Fix
Resolve the connection through the model that owns the Arel table, via
attribute.relation. Arel::Table stores the owning model as@klass(set via theklass:keyword argument in its constructor). There is no public accessor,so we use
instance_variable_get(:@klass)with a fallback toActiveRecord::Basefor cases where klass is nil (e.g. hand-built Arel tables).This is the minimal change — the rest of the method is untouched.
Notes
ActiveRecord::Basepreserves existing behavior for single-database setups@klasshas been present onArel::Tablesince Rails 5.2 and is used internally by Rails for attribute alias resolution