Fix Rails 7.2 JSONB pluck type casting#1209
Open
rajaravivarma-r wants to merge 1 commit intojruby:72-stablefrom
Open
Fix Rails 7.2 JSONB pluck type casting#1209rajaravivarma-r wants to merge 1 commit intojruby:72-stablefrom
rajaravivarma-r wants to merge 1 commit intojruby:72-stablefrom
Conversation
Rails 7.2 started reading AR::Result column types by index during pluck, but PostgreSQLResult only stored types by column name. As a result, JSONB expressions like payload -> 'fields' exposed a named type while column_types[0] stayed nil, and pluck returned raw strings instead of cast JSON values. Store PostgreSQL result types under both the column name and zero-based column index, and add regression coverage for JSONB array/object/scalar extraction across multiple rows in types_test.
Author
|
After upgrading from Rails 7.0 to 7.2 our code started to break because of an incompatibility between Rails 7.2 and activerecord-jdbc-adapter. I investigated the problem with the help of an AI tool and made this PR with its help. It would be nice if I could get this fixed in a activerecord-jdbc-adapter 72.x version. Happy to modify this PR as required by the maintainer. TIA. |
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 7.2 started reading AR::Result column types by index during pluck, but PostgreSQLResult only stored types by column name. As a result, JSONB expressions like payload -> 'fields' exposed a named type while column_types[0] stayed nil, and pluck returned raw strings instead of cast JSON values.
Store PostgreSQL result types under both the column name and zero-based column index, and add regression coverage for JSONB array/object/scalar extraction across multiple rows in types_test.
Behaviour examples
This change restores the behaviour we had in earlier Rails / ActiveRecord-JDBC versions, where
pluckon JSONB SQL fragments returned type-cast Ruby values instead of raw JSON strings.Example 1: JSONB array extraction
Expected behaviour in previous Rails / AR-JDBC versions, and with this change:
Type-wise, that is:
Behaviour seen on Rails 7.2 before this fix:
Example 2: JSONB scalar extraction
Expected behaviour in previous Rails / AR-JDBC versions, and with this change:
Type-wise, that is:
Behaviour seen on Rails 7.2 before this fix:
Internally, the issue was that AR-JDBC exposed the JSONB type under the column name, but Rails 7.2 started reading
AR::Result#column_typesby column index duringpluck. This change stores the type under both the column name and the zero-based column index, so JSONB fragments are cast correctly again.