Merge pull request #51483 from JoeDupuis/add-date-text-decoder-postgresql-adapter

Add a Date decoder to the pg adapter
This commit is contained in:
Jean Boussier 2024-04-30 13:32:07 +02:00 committed by GitHub
commit 6a38d3ac6c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 17 additions and 0 deletions

View File

@ -1,3 +1,13 @@
* `PostgreSQLAdapter` now decodes columns of type date to `Date` instead of string.
Ex:
```ruby
ActiveRecord::Base.connection
.select_value("select '2024-01-01'::date").class #=> Date
```
*Joé Dupuis*
* Strict loading using `:n_plus_one_only` does not eagerly load child associations.
With this change, child associations are no longer eagerly loaded, to

View File

@ -1159,6 +1159,7 @@ module ActiveRecord
"bool" => PG::TextDecoder::Boolean,
"timestamp" => PG::TextDecoder::TimestampUtc,
"timestamptz" => PG::TextDecoder::TimestampWithTimeZone,
"date" => PG::TextDecoder::Date,
}
known_coder_types = coders_by_name.keys.map { |n| quote(n) }

View File

@ -39,4 +39,10 @@ class PostgresqlDateTest < ActiveRecord::PostgreSQLTestCase
topic = Topic.create!(last_read: date)
assert_equal date, Topic.find(topic.id).last_read
end
def test_date_decoder
date = ActiveRecord::Base.connection.select_value("select '2024-01-01'::date")
assert_equal Date.new(2024, 01, 01), date
assert_equal Date, date.class
end
end