Skip to content

Commit 6a38d3a

Browse files
authored
Merge pull request rails#51483 from JoeDupuis/add-date-text-decoder-postgresql-adapter
Add a Date decoder to the pg adapter
2 parents f963d4a + 2c88c80 commit 6a38d3a

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

activerecord/CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
* `PostgreSQLAdapter` now decodes columns of type date to `Date` instead of string.
2+
3+
Ex:
4+
```ruby
5+
ActiveRecord::Base.connection
6+
.select_value("select '2024-01-01'::date").class #=> Date
7+
```
8+
9+
*Joé Dupuis*
10+
111
* Strict loading using `:n_plus_one_only` does not eagerly load child associations.
212

313
With this change, child associations are no longer eagerly loaded, to

activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,7 @@ def add_pg_decoders
11591159
"bool" => PG::TextDecoder::Boolean,
11601160
"timestamp" => PG::TextDecoder::TimestampUtc,
11611161
"timestamptz" => PG::TextDecoder::TimestampWithTimeZone,
1162+
"date" => PG::TextDecoder::Date,
11621163
}
11631164

11641165
known_coder_types = coders_by_name.keys.map { |n| quote(n) }

activerecord/test/cases/adapters/postgresql/date_test.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,10 @@ def test_bc_date_year_zero
3939
topic = Topic.create!(last_read: date)
4040
assert_equal date, Topic.find(topic.id).last_read
4141
end
42+
43+
def test_date_decoder
44+
date = ActiveRecord::Base.connection.select_value("select '2024-01-01'::date")
45+
assert_equal Date.new(2024, 01, 01), date
46+
assert_equal Date, date.class
47+
end
4248
end

0 commit comments

Comments
 (0)